MIPS 中的堆栈.addi $sp,$sp,-4 中的 4 的目的是什么? [英] Stacks in MIPS. What is the purpose of 4 in addi $sp,$sp,-4?

查看:232
本文介绍了MIPS 中的堆栈.addi $sp,$sp,-4 中的 4 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用过程的 MIPS 代码,但我不明白堆栈 (sp) 和帧指针 (fp) 的用途是什么.

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).

例如我研究的代码如下.它打印您输入的数字之前的所有数字,例如3. 3,2,1,0 并显示它使用了多少堆栈大小.在本例中,它使用了 16.我将 -4 更改为 -8 并且出现错误.但是,我注意到其他一些使用 -8 的代码.

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.

问题是我错过了什么?

proc:
    addi $sp,$sp,-4     # 
    sw   $ra,0($sp)     # push $ra  

推荐答案

在调用函数之前将返回地址压入堆栈.这就是代码的作用.-4 表示通过将堆栈指针向上移动来分配"堆栈上的 4 个字节,以便可以写入返回地址.

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.

在我看到代码之前,我无法得出 -8 的作用.但它可能会为函数的参数保留空间.

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.

这篇关于MIPS 中的堆栈.addi $sp,$sp,-4 中的 4 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆