go to previous page   go to home page   go to next page hear noise

Answer:

Yes. Just be sure to synchronize the pushes and pops so the the correct values go into the correct registers.


Pushing and Popping Registers

Here is a rule: if a subroutine is expected to alter any of the S registers, it must first push their values onto the stack. Just before returning to the caller it must pop these values from the stack back into the registers they came from.

Here is an example program fragment. Subroutine subB calls subC which uses two S registers.

subB:
         sub    $sp,$sp,4    # push $ra
         sw     $ra,($sp)

         . . . .

         jal    subC         # call subC
         nop

         . . . .
         
         lw     $ra,($sp)    # pop return address
         add    $sp,$sp,4
         jr     $ra          # return to caller
         nop

# subC expects to use $s0 and $s1  
# subC does not call another subroutine
#       
subC:             
         sub    $sp,$sp,4    # push $s0
         sw     $s0,($sp)
         sub    $sp,$sp,4    # push $s1
         sw     $s1,($sp)

         . . . .             # statements using $s0 and $s1

         lw     ,($sp)   # pop 
         
         add    $sp,$sp,4
         
         lw     ,($sp)   # pop 
         
         add    $sp,$sp,4

         jr     $ra          # return to caller
         nop

QUESTION 5:

Fill in the blanks so that subB sees its S registers when it regains control.