简体   繁体   English

ARM 组件中的偏移量是多少? 它是如何工作的?

[英]What is offset in ARM Assembly? How does it work?

I am learning ARM Assembly Language.我正在学习 ARM 汇编语言。 I do is write a program in c and compile it to look at the Assembly file.我所做的是在 c 中编写一个程序并编译它以查看程序集文件。 Below is such a compiled snippet from a program and it has this ldr r0, [pc, #28] .下面是这样一个程序的编译片段,它有这个ldr r0, [pc, #28] Here I need help in understanding this PC thing.在这里,我需要帮助来理解这个 PC 的东西。 I know it means the program counter but I can not understand the actual purpose of the instruction.我知道这意味着程序计数器,但我无法理解指令的实际目的。

1000037c &ltmain>:
1000037c:    b570         push  {r4, r5, r6, lr}
.
.
10000388:    f000 f812    bl    100003b0
1000038c:    4807         ldr   r0, [pc, #28]
1000038e:    626c         str   r4, [r5, #36]
.
.

In the C program, it must have been an integer (>256) maybe, I don't fully understand this concept.在 C 程序中,它一定是 integer (>256) 也许,我不完全理解这个概念。 Thanks in advance.提前致谢。

What you're looking at is a pre-indexed load instruction, using the value of the pc register as the base address.您正在查看的是预索引加载指令,使用pc寄存器的值作为基地址。

Any time your code needs to load a "large" immediate value into a register, you're likely to see something like this in tandem with a data declaration somewhere below it.每当您的代码需要将“大”立即值加载到寄存器中时,您可能会在其下方某处看到类似的数据声明。 You haven't shown it in your snippet, but scroll down a little bit in the assembly and you'll probably see something like:您尚未在代码段中显示它,但在程序集中向下滚动一点,您可能会看到如下内容:

.word 0x12345678

Or even possibly the value of a label甚至可能是 label 的值

_some_label: ...
...
.word _some_label

As long as the .word value is close enough in memory to where the pc is currently executing, you can load the data using a relative offset.只要 memory 中的.word值与pc当前执行的位置足够接近,就可以使用相对偏移量加载数据。 In your example, there should be a data word at offset 32 from where that load instruction is placed (adding 8 as per the ARM ISA for PC relative addressing).在您的示例中,应该有一个数据字位于该加载指令所在的偏移量 32 处(根据 ARM ISA 为 PC 相对寻址添加 8)。

In general this approach for loading immediate values can be tricky when you're writing assembly by hand (since you need to keep track of the exact relative offsets from where your instructions are placed), but compilers can do this very reliably, which is why you often see this in compiler-generated assembly.通常,当您手动编写汇编时,这种加载立即值的方法可能会很棘手(因为您需要跟踪放置指令的确切相对偏移量),但编译器可以非常可靠地做到这一点,这就是为什么您经常在编译器生成的程序集中看到这一点。

This question also serves as a good reference for this, and I highly recommend you check it out. 这个问题也可以作为一个很好的参考,我强烈建议你检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM