2. Instructions: Language of the Computer > 2-4. Supporting procedures in computer HW
Supporting procedures in computer HW
Updated at 2022.09.26
Procedures do calling / returning with other procedures
Use registers for passing arguments, return address, and return values
But, what if different procedures share the same registers?
Use stacks to store all the information for operating each procedure
Stacks are kept in memory
$sp
points the top-of-stack in memoryThe values of some registers must be preserved on call
It must be possible to recover the values on those registers after calling / returning is completed
If a callee uses preserved registers, the callee must
If a caller needs to keep the values in non-preserved registers, the caller must
jal PROCEDURE_LABEL
$ra
PROCEDURE_LABEL
)jr $ra
$ra
(other registers can also be used as an operand)Push
addi $sp, $sp, -4
sw $t0, 0($sp)
$t0
into the top-of-stackPop
lw $t0, 0($sp)
addi $sp, $sp, 4
$t0