Program | ---> | $a0 $a1 $a2 $a3 |
||
$ra |
---> | Procedure . use $a0 - $a3 . . set $v0, $v1 return to program | ||
$v0 $v1 |
<--- | |||
Program | <--- |
jal
(jump and link): Used to go to a procedure. First puts
the program counter in $ra
, then jumps to the beginning of
the procedure.
jr $ra
(jump return): Jumps to the address stored in
$ra
(the instruction in the calling procedure to return
to).
$a0, ..., $a3
are for procedure arguments
$ra
is for the return location
$v0, $v1
are for return values
The conventions/rules of MIPS are that subfunctions may change the values of some registers, but not all of them.
$a0 — $a3
),
the return value registers ($v0 & $v1
),
and
the "temporary" registers ($t0 — $t9
).
These are the "unsaved" registers.
Thus, if the calling code has values in those
registers that it will use again after the subfunction call,
it needs to save copies of them somewhere (called the
Stack) before calling the subfunction,
and restore the values after the subfunction returns.
$s0 — $s7
) and
the return address register ($ra
) will have the same
values after a subfunction call as they did before the call. This
means that if the subfunction needs to, or chooses to, modify the
values in those registers, it must save copies of them to the
Stack before changing them,
and restore those values before returning to the calling code.