问题
I am studying a MIPS code that uses procedure but I can not understand what is the purpose of stack (sp) and frame pointer (fp).
For example the code I study have this below. It prints the all the numbers before the number you entered, e.g. 3. 3,2,1,0 and shows how much stack size it used. In this example it used 16. I changed the -4
to -8
and I got an error.
However, I noticed some other codes that uses -8
.
The question is what am I missing?
proc:
addi $sp,$sp,-4 #
sw $ra,0($sp) # push $ra
回答1:
Push the return address onto the stack before calling the function. That is what the code does. -4 means "allocating" 4 bytes on the stack by moving the stack pointer up, so that the return address can be written.
The explanation above assumes the normal usage of the registers.
I cannot conclude on what the -8 does until I see the code. But it is likely reserving space for the argument to the function.
来源:https://stackoverflow.com/questions/11202736/stacks-in-mips-what-is-the-purpose-of-4-in-addi-sp-sp-4