a Write a sequence to store s0 s1 at the beginning of a proc

(a) Write a sequence to store s0, s1 at the beginning of a procedure onto the stack. (b) Write a sequence to restore the registers from the stack back onto the same registers

Solution

Stack:

A Stack is a data structure used to store the objects. The object which is stored first is retrieved last.

It basically performs two operations which are PUSH and POP.

PUSH:

PUSH operation is used to store the object on to the stack.

POP:

POP operation is used to retrieve the object from the stack.

Stack behaviour:

a)

Consider the two registers as S0 and S1, the procedure to store the registers in the same sequence is shown below:

To store the registers on to the stack, PUSH operation is used which is shown below:

push:    
   addi $sp, $sp, -4 # Decrement stack pointer by 4
   sw $s0, 0($sp) # Save $s0 to stack
   addi $sp, $sp, -4 # Decrement stack pointer by 4
   sw $s1, 0($sp) # Save $s1 to stack

In the above assembly language code, In PUSH label the two registers S0 and S1 are stored on to the stack.

b)

To restore the registers from the stack, POP operation is used which is shown below:

pop:

   lw $s1, 0($sp) # Copy from stack to $s1
   addi $sp, $sp, 4 # Increment stack pointer by 4
lw $s0, 0($sp) # Copy from stack to $s0
   addi $sp, $sp, 4 # Increment stack pointer by 4

In PUSH label the two registers S0 and S1 are restored back from to the stack.

(a) Write a sequence to store s0, s1 at the beginning of a procedure onto the stack. (b) Write a sequence to restore the registers from the stack back onto the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site