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


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

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

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

         jr     $ra          # return to subB 

The registers are popped in the opposite order that they were pushed.

Stack-based Linkage Convention

The Simple Linkage Convention can be extended into a Stack-based Linkage Convention. This is not an official convention. However it could be used for a small assembly language project because it is not very complicated and does nearly everything you need. If you want to link assembly language routines to "C" programs or to use routines from program libraries you need to use the full, official, linkage rules. (But on the SPIM simulator you can't do that, anyway.) Here are our much simpler rules:

Subroutine Call (done by the caller):

  1. Push onto the stack any registers $t0-$t9 that contain values that must be saved. The subroutine might change these registers.
  2. Put argument values into $a0-$a3.
  3. Call the subroutine using jal.

Subroutine Prolog (done by the subroutine at its beginning):

  1. If this subroutine might call other subroutines, push $ra onto the stack.
  2. Push onto the stack any registers $s0-$s7 that this subroutine might alter.

Subroutine Body:

  1. The subroutine may alter any T or A register, or any S register that it saved in the prolog (step 5).
  2. If the subroutine calls another subroutine, then it does so by following these rules.

Subroutine Epilog (done by the subroutine just before it returns to the caller):

  1. Put returned values in $v0-$v1
  2. Pop from the stack (in reverse order) any registers $s0-$s7 that were pushed in the prolog (step 5).
  3. If it was pushed in the prolog (step 4), pop the return address from the stack into $ra.
  4. Return to the caller using jr $ra.

Regaining Control from a subroutine (done by the caller):

  1. Pop from the stack (in reverse order) any registers $t0-$t9 that were previously pushed (step 1).


Why do you think there are both T and S registers? Why not just have S registers and make it a rule that a subroutine must save (and later restore) each one that it uses?