assembly - ARM中的SP(栈)和LR是什么?

标签 assembly stack arm

我一遍又一遍地阅读定义,但我仍然不明白ARM中的SP和LR是什么?我了解PC(它显示下一条指令的地址),SP和LR可能类似,但我只是不明白它是什么。你能帮我一下吗?

编辑:如果你能用例子来解释它,那就太好了。

编辑:终于弄清楚了LR的用途,但仍然不明白SP的用途。

最佳答案

LR 是 link register用于保存函数调用的返回地址。

SP是堆栈指针。堆栈通常用于保存函数调用之间的“自动”变量和上下文/参数。从概念上讲,您可以将“堆栈”视为“堆积”数据的地方。您不断将一条数据“堆叠”在另一条数据上,堆栈指针会告诉您数据“堆栈”的“高度”。您可以从“堆栈”的“顶部”删除数据并使其更短。

来自 ARM 架构引用:

SP, the Stack Pointer

Register R13 is used as a pointer to the active stack.

In Thumb code, most instructions cannot access SP. The only instructions that can access SP are those designed to use SP as a stack pointer. The use of SP for any purpose other than as a stack pointer is deprecated. Note Using SP for any purpose other than as a stack pointer is likely to break the requirements of operating systems, debuggers, and other software systems, causing them to malfunction.

LR, the Link Register

Register R14 is used to store the return address from a subroutine. At other times, LR can be used for other purposes.

When a BL or BLX instruction performs a subroutine call, LR is set to the subroutine return address. To perform a subroutine return, copy LR back to the program counter. This is typically done in one of two ways, after entering the subroutine with a BL or BLX instruction:

• Return with a BX LR instruction.

• On subroutine entry, store LR to the stack with an instruction of the form: PUSH {,LR} and use a matching instruction to return: POP {,PC} ...

This link gives an example of a trivial subroutine.

Here is an example of how registers are saved on the stack prior to a call and then popped back to restore their content.

关于assembly - ARM中的SP(栈)和LR是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8236959/

相关文章:

c++ - 无法在GST-launch-1.0的Qemu上在armv7上播放WAV音频

arm - 在 ZedBoard 上运行“ARM TrustZone Secure/Normal world”示例

assembly - Linux内核0.01引导加载程序使用rep movw,而不是rep movsw?这实际上是重复普通的 MOV 吗?

performance - 移位操作码真的比移动快 3 倍吗?英特尔 x86

c++ - 具有任意基数的基本任意精度算术的最佳性能

c - 用 C 将项目插入堆栈

linux - 程序集 'Segmentation Fault' 尝试打印十进制数时

C:如果参数从右向左压入,为什么会出现下面的情况? (64位操作系统,32位程序)

c - 可变参数在多次转发时如何在堆栈上表示?

assembly - Assembly 中的偏移是什么以及如何使用它们?