Stack-Based Linkage Convention (Cont.)


The following are the rules of stack-based linkage convention:
  1. 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.

  2. 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.

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

  4. 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 II).
    3. If it was pushed in the prolog (Step II), pop the return address from the stack into $ra.
    4. Return to the caller using jr $ra.

  5. 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 I).