Stack-Based Linkage Convention (Cont.)
The following are the rules of stack-based linkage convention:
- Subroutine call (done by the caller):
- Push onto the stack any registers
$t0-$t9 that contain values that must be saved.
The subroutine might change these registers.
- Put argument values into
$a0-$a3.
- Call the subroutine using
jal.
- Subroutine prolog (done by the subroutine at its beginning):
- If this subroutine might call other subroutines, push
$ra onto the stack.
- Push onto the stack any registers
$s0-$s7 that this subroutine might alter.
- Subroutine body:
- The subroutine may alter any T or A register, or any S register that it saved in the prolog (Step II).
- 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):
- Put returned values in
$v0-$v1.
- Pop from the stack (in reverse order) any registers
$s0-$s7 that were pushed in the prolog (Step II).
- If it was pushed in the prolog (Step II), pop the return address from the stack into
$ra.
- Return to the caller using “
jr $ra”.
- Regaining control from a subroutine (done by the caller):
- Pop from the stack (in reverse order) any registers
$t0-$t9 that were previously pushed (Step I).