a = a + 1;
lw $t0,12($fp) # get a addiu $t0,$t0,1 # a + 1 sw $t0,12($fp) # a =
A real-world linkage convention allows many types of objects to go into a stack frame. The following rules assume that the stack only stores 32-bit values. There are other features of real-world linkage that the following does not have.
Calling a Subroutine (done by the caller):
- Push any registers
$t0-$t9that contain values that must be saved. Push the registers in numerical order.
- Put argument values into
- Call the subroutine using
Subroutine Prolog (done by the subroutine):
- Push the caller's frame pointer
- Push any of the registers
$s0-$s7that the subroutine might alter.
- Initialize the frame pointer:
$fp = $sp - space_for_variables. The "space for variables" is four times the number of local variables. (Remember that subtracting from
$spgrows the stack, and that our variables are always four bytes wide).
- Initialize the stack pointer:
$sp = $fp.
- At this point the stack looks like the picture.
- The subroutine may alter any
Vregister, or any
Sregister that it saved in the prolog.
- The subroutine refers to local variables as
- The subroutine may push and pop values on the stack using
- If the subroutine calls another subroutine, then it does so by following these rules.
Subroutine Epilog (done at the end of the subroutine):
- Put return values in
$sp = $fp + space_for_variables.
- Pop into
$s0-$s7any values for them that were previously saved in the frame (in step6).
- Pop the caller's frame pointer into
- Return to the caller using
Regaining Control from a Subroutine (done by the caller):
- Pop any registers
$t0-$t9that the caller previously pushed.
When the caller gets control back, are its frame pointer and stack pointer the same as when it called the subroutine?