MIPS中的sw和lw是否在堆栈指针之下或之上存储一个值? [英] Does sw and lw in MIPS store a value below or above the stack pointer?

查看:79
本文介绍了MIPS中的sw和lw是否在堆栈指针之下或之上存储一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授讲了一段视频,像这样:

My professor gave a video that looks like this:

在右下角,他在位置124处写了 $ ra ,而 $ sp 在128处,这意味着第一个 sw $ ra,4($ sp)指令将 $ ra 值存储在比 $ sp 个字节的位置.但是我的书却有所不同:

In the lower right, he wrote $ra at location 124 while the $sp is at 128 which implies that the first sw $ra, 4($sp) instruction stores the $ra value at a location 4 bytes less than the $sp. But my book does it differently:

该图像表示lw指令将其存储在比 $ sp 大,正数更大的位置.那哪个是对的?lw和sw偏移量数字是指高于还是低于 $ sp 的数字?

The image implies that the lw instruction stores it at locations larger, more positive numbers than the $sp. So which is right? Does lw and sw offset numbers refer to numbers higher or lower than the $sp?

推荐答案

您正确地看到第一个阶乘存储在堆栈指针上方,该指针未分配,并且必须由调用方分配.

You are right in observing that the first factorial is storing above the stack pointer, stack storage that it did not allocate, and must have been allocated by the caller.

这在某种程度上是非标准用法,但在技术上是合法的,因为MIPS调用约定要求将任何堆栈帧的前4个堆栈位置都赋予 callee .该功能仅分配2个字的框架,并且根据调用约定(允许被叫方使用该框架的前4个字),它应最少分配4个字的框架.

This is somewhat non-standard usage, but technically legal, since the MIPS calling convention requires giving the top 4 stack locations of any stack frame to the callee.  The function is only allocating a 2-word frame, and according to the calling convention (which allows the callee to use the top 4 words of the frame) it should be allocating minimally a 4-word frame.

尽管如此,由于阶乘函数除了自身之外不会再调用其他函数,因此这是合法的,并且符合调用约定-从某种意义上说,它的工作是确保一个函数可以调用另一个函数.

Still, since the factorial function calls no other except itself, this is ~legal, and in compliance with the calling convention — in the sense that its job is to ensure that one function can call another.

(请注意,在RISC V(开源MIPS后续版本)中,不存在供被调用方使用的4字堆栈框架的要求,因此在此行不通.)

(Note that in RISC V (the open source MIPS follow-on) this requirement of 4-words stack frame for callee to use is not present so similar would not work there.)

第二个示例更为传统,但是,它也没有分配标准大小的框架,该框架为被呼叫者提供了前4个单词.仍然在技术上也不是必需的,并且它对原始调用方(例如 main )的依赖性较小,从而提供了适当的堆栈帧(向被调用方提供了4个单词).

The second example is more traditional, however, it also does not allocate a standard sized frame — one that gives the top 4-words to the callee.  Still it is also not technically necessary, and less reliant on the original caller (e.g. main) providing a proper stack frame (one with 4 words given to the callee).

让我们进一步观察到,第一个代码示例将 $ ra $ a0 存储在堆栈中,这是我们希望保存的寄存器-而后面的示例存储 $ s0 (我们希望将它们保存下来,因为它们是专用的非易失性),但还有 $ t0 $ t1 似乎不是这些是专用的临时工.

Let's further observe that the first code sample stores $ra and $a0 on the stack, which are registers that we expect to be saved — whereas the latter example stores $s0 (which we would expect to be saved as these are dedicated non-volatile), but also $t0 and $t1 which seems non standard as these are dedicated temporaries.

这篇关于MIPS中的sw和lw是否在堆栈指针之下或之上存储一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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