ARM 中的 SP(堆栈)和 LR 是什么? [英] What are SP (stack) and LR in ARM?

查看:73
本文介绍了ARM 中的 SP(堆栈)和 LR 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am reading definitions over and over again and I still not getting what are SP and LR in ARM? I understand PC (it shows next instruction's address), SP and LR probably are similar, but I just don't get what it is. Could you please help me?

如果你能用例子来解释它,那就太好了.

edit: if you could explain it with examples, it would be superb.

终于弄明白LR是干什么用的了,还是没弄明白SP是干什么用的.

edit: finally figured out what LR is for, still not getting what SP is for.

推荐答案

LR 是 链接注册用于保存函数调用的返回地址.

LR is link register used to hold the return address for a function call.

SP 是堆栈指针.堆栈通常用于保存自动"堆栈.跨函数调用的变量和上下文/参数.从概念上讲,您可以想到堆栈".作为你堆"的地方你的数据.你一直在堆叠"一个数据在另一个之上,堆栈指针告诉你如何高"你的堆栈"的数据是.您可以从顶部"删除数据.堆栈"的并缩短它.

SP is stack pointer. The stack is generally used to hold "automatic" variables and context/parameters across function calls. Conceptually you can think of the "stack" as a place where you "pile" your data. You keep "stacking" one piece of data over the other and the stack pointer tells you how "high" your "stack" of data is. You can remove data from the "top" of the "stack" and make it shorter.

来自 ARM 架构参考:

From the ARM architecture reference:

SP,堆栈指针

寄存器 R13 用作指向活动堆栈的指针.

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

在 Thumb 代码中,大多数指令无法访问 SP.唯一的可以访问 SP 的指令是那些旨在将 SP 用作堆栈指针.将 SP 用于堆栈以外的任何目的不推荐使用指针.注意 将 SP 用于任何目的而不是作为堆栈指针很可能会破坏操作的要求系统、调试器和其他软件系统,导致它们故障.

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,链接寄存器

寄存器 R14 用于存储子程序的返回地址.在其他时候,LR可以用于其他目的.

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

当 BL 或 BLX 指令执行子程序调用时,LR 设置为子程序返回地址.要执行子程序返回,复制 LR回到程序计数器.这通常通过以下两种方式之一完成方式,在使用BL或BLX指令进入子程序后:

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:

• 使用 BX LR 指令返回.

• Return with a BX LR instruction.

• 在子程序入口,将 LR 存储到具有以下形式的指令的堆栈:PUSH {,LR} 和使用匹配指令返回:POP {,PC} ...

• 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} ...

这个链接给出了一个例子一个简单的子程序.

这里是一个例子寄存器如何在调用之前保存在堆栈中,然后弹出以恢复其内容.

这篇关于ARM 中的 SP(堆栈)和 LR 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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