Stacks in MIPS. What is the purpose of 4 in addi $sp,$sp,-4?

。_饼干妹妹 提交于 2020-02-03 10:38:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!