为什么在 ARM 汇编中使用 LDR 而不是 MOV(反之亦然)?

编程入门 行业动态 更新时间:2024-10-09 11:26:17
本文介绍了为什么在 ARM 汇编中使用 LDR 而不是 MOV(反之亦然)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在浏览本教程: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(反之亦然)?

本文发布于:2023-11-29 04:19:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   ARM   LDR   MOV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!