A Protocol for Calling Produres in MIPS
Suppose that procedure A needs to call procedure B. Here is what must happen.
1. A puts onto the stack any values of registers (except $s0-$s7) that will be
needed after the call to B. This includes the $ra register, which holds the
return address for A. A also puts onto the stack any arguments of B beyond the
fourth argument.
2. A sets $a0-$a3 to the first four arguments.
3. A calls B (jal B). This sets $ra to the return address for B.
4. B puts onto the stack the values of any registers $s0-$s7 that it
needs to use.
5. B does its work.
6. B sets $v0,$v1 to any values that need to be returned.
7. B restores the values of any registers $s0-$s7 that it used and sets $sp
to what it was after step 1.
8. B returns (jr $ra).
9. A restores the register values it saved on the stack and sets $sp to what
is was before step 1.
Note: if B needs to return more than what will fit into $v0, $v1,
the extra can be left on the stack when it returns, and A will have to know
that this extra is on the stack.