gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:28:57 30 4
gpt4 key购买 nike

我一遍又一遍地阅读定义,但我仍然不明白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 onlyinstructions that can access SP are those designed to use SP as astack pointer. The use of SP for any purpose other than as a stackpointer is deprecated. Note Using SP for any purpose other than as astack pointer is likely to break the requirements of operatingsystems, debuggers, and other software systems, causing them tomalfunction.

LR, the Link Register

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

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

• Return with a BX LR instruction.

• On subroutine entry, store LR tothe stack with an instruction of the form: PUSH {,LR} anduse 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com