You can; however, this is dangerous
You will be better of restoring the sp right after you dont need the space.
Cause otherwise at the epilogue point when you re-reference sp to restore ra and s registers, SP is not where it should be.
See example below:
Lets say hypothetically sp is at 100.
foo():
addi sp, sp, (-12). SP = 88
sw ra, 0 (sp) (RA is saved at 88)
sw s0, 4 (sp) (S0 is saved at 92)
sw s1, 8 (sp) (S1 is saved at 96)
add sp,sp, (-4) SP = 84
sw t0, 0 (sp)
....
lw t0,0(sp)
NOW LETS SAY YOU DONT DO THE addi sp,sp,4
SP still at 84
Epilogue would have to be modified
lw ra, 4(sp) ---- Offset relative to SP is now 4
lw s0, 8(sp)
lw s1, 12 (sp)
You see that the offsets of the lw in epilogue no longer match the sw in the prologue.
This is because SP moved in the middle and you did not remove during deallocation.
Two solutions
- Either try avoid manipulating SP in the middle OR
- Always deallocate space
add sp,sp,-4 # allocate
sw t0,0(sp)
Do Dangerous things
lw t0,0(sp)
addi sp,sp,4 a # Deallocate