我一遍又一遍地阅读定义,但我仍然不明白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} ...
关于assembly - ARM中的SP(栈)和LR是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8236959/