Assume we are passing arguments to a subroutine using the stack frame as follows:
addi $sp, $sp, -8
sw $s0, 0($sp)
jal sub
lw $s1, 4($sp)
addi $sp, $sp, 8
sub: lw $t0, 0($sp)
... do stuff ...
sw $t1, 4($sp)
jr $ra
I understand the concepts of passing parameters through the stack and returning to the caller using the $ra register.
What is not so clear to me is this:
addi $sp, $sp, 8
This restores space in the stack frame. Can someone help me understand:
- What happens if I don't do this?
- Does the assembler "cares" if I don't restore the space?
- Is this in a way similar to memory management in c++? (i.e.: deleting pointers, destructors, etc)