为什么在 ARM 汇编中使用 LDR 而不是 MOV(反之亦然)? [英] Why use LDR over MOV (or vice versa) in ARM assembly?

查看:27
本文介绍了为什么在 ARM 汇编中使用 LDR 而不是 MOV(反之亦然)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览本教程:http://www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/os/ok01.html

第一行汇编是:

ldr r0,=0x20200000

第二个是:

mov r1,#1

我认为 ldr 用于将值从内存加载到寄存器中.但似乎 = 意味着 0x20200000 是一个值而不是内存地址.这两行似乎都在加载绝对值.

I thought ldr was for loading values from memory into registers. But it seems the = means the 0x20200000 is a value not a memory address. Both lines seem to be loading the absolute values.

推荐答案

这是一个技巧/捷径.比如说

It is a trick/shortcut. say for example

ldr r0,=main

会发生什么情况是汇编器会在指令附近但在指令路径之外分配一个数据字

what would happen is the assembler would allocate a data word, near the instruction but outside the instruction path

ldr r0,main_addr
...
b somewhere
main_addr: .data main

现在将该技巧扩展到常量/立即数,尤其是那些不能放入立即移动指令的:

Now expand that trick to constants/immediates, esp those that cannot fit into a move immediate instruction:

top:
add r1,r2,r3
ldr r0,=0x12345678
eor r1,r2,r3
eor r1,r2,r3
b top

组装然后拆卸

00000000 <top>:
   0:   e0821003    add r1, r2, r3
   4:   e59f0008    ldr r0, [pc, #8]    ; 14 <top+0x14>
   8:   e0221003    eor r1, r2, r3
   c:   e0221003    eor r1, r2, r3
  10:   eafffffa    b   0 <top>
  14:   12345678    eorsne  r5, r4, #125829120  ; 0x7800000

您会看到汇编器已为您添加了数据字并将 ldr 更改为您的 pc 相对.

and you see the assembler has added the data word for you and changed the ldr into a pc relative for you.

现在,如果您使用适合 mov 指令的立即数,那么可能取决于汇编程序,当然是我使用的 gnu,它对我来说将它变成了 mov

now if you use an immediate that does fit in a mov instruction, then depending on the assembler perhaps, certainly with the gnu as I am using, it turned it into a mov for me

top:
add r1,r2,r3
ldr r0,=0x12345678
ldr r5,=1
mov r6,#1
eor r1,r2,r3
eor r1,r2,r3
b top


00000000 <top>:
   0:   e0821003    add r1, r2, r3
   4:   e59f0010    ldr r0, [pc, #16]   ; 1c <top+0x1c>
   8:   e3a05001    mov r5, #1
   c:   e3a06001    mov r6, #1
  10:   e0221003    eor r1, r2, r3
  14:   e0221003    eor r1, r2, r3
  18:   eafffff8    b   0 <top>
  1c:   12345678    eorsne  r5, r4, #125829120  ; 0x7800000

所以它基本上是一个打字快捷方式,理解你让汇编程序有能力找到一个粘贴常量的地方,它通常做得很好,有时会抱怨,不确定我是否见过它失败它安全.有时您需要在代码中使用 .ltorg 或 .pool 来鼓励汇编器找到位置.

So it is basically a typing shortcut, understand that you are giving the assembler the power to find a place to stick the constant, which it usually does a good job, sometimes complains, not sure if I have seen it fail to do it safely. Sometimes you need a .ltorg or .pool in the code to encourage the assembler to find a place.

这篇关于为什么在 ARM 汇编中使用 LDR 而不是 MOV(反之亦然)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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