Here is my assembly code for A9,
这是A9的汇编代码,
ldr x1, = 0x400020 // Const value may be address also
ldr w0, = 0x200018 // Const value may be address also
str w0, [x1]
The below one is expected output ?
以下是预期产量?
*((u32 *)0x400020) = 0x200018;
When i cross checked with it by compiler it given differnet result as mov and movs insted of ldr. How to create ldr in c?
当我通过编译器与它交叉检查时,它给出了不同的结果,因为mov和movs是ldr的insted。如何在c中创建ldr?
1 个解决方案
#1
2
When i cross checked with it by compiler it given differnet result as mov and movs
当我通过编译器与它交叉检查时,它给出了不同的结果作为mov和movs
It sounds to me like you compiled the C code with a compiler targetting AArch32, but the assembly code you've shown looks like it was written for AArch64. Here's what I get when I compile with ARM64 GCC 5.4 and optimization level O3
(comments added by me):
听起来像你用编译器目标AArch32编译C代码,但你显示的汇编代码看起来像是为AArch64编写的。这是我使用ARM64 GCC 5.4和优化级别O3编译时得到的结果(我添加了评论):
mov x0, 32 @ x0 = 0x20
mov w1, 24 @ w1 = 0x18
movk x0, 0x40, lsl 16 @ x0[31:16] = 0x40
movk w1, 0x20, lsl 16 @ w1[31:16] = 0x20
str w1, [x0]
How to create ldr in c?
如何在c中创建ldr?
I can't see any good reason why you'd want the compiler to generate an LDR
in this case.LDR reg,=value
is a pseudo-instruction that allows you to load immediates that cannot be encoded directly in the instruction word. The assembler achieves this by placing the value (e.g. 0x200018
) in a literal pool, and then replacing ldr w0, =0x200018
with a PC-relative load from that literal pool (i.e. something like ldr w0,[pc,#offset_to_value]
). Accessing memory is slow, so the compiler generated another sequence of instructions for you that achieves the same thing in a more efficient manner.
在这种情况下,我看不出有什么理由要求编译器生成LDR。 LDR reg,= value是一个伪指令,允许您加载不能直接在指令字中编码的immediates。汇编程序通过将值(例如0x200018)放在文字池中,然后将ldr w0,= 0x200018替换为来自该文字池的PC相对负载(即类似于ldr w0,[pc,#offset_to_value])来实现此目的。访问内存很慢,因此编译器会为您生成另一系列指令,以更有效的方式实现相同的操作。
Pseudo-instructions are mainly a convenience for humans writing assembly code, making the code easier for them or their colleagues to read/write/maintain. Unlike a human being, a compiler doesn't get fatigued by repeating the same task over and over, and therefore doesn't have as much need for conveniences like that.
伪指令主要是为人们编写汇编代码提供便利,使代码更易于他们或他们的同事进行读/写/维护。与人类不同,编译器不会因反复重复相同的任务而感到疲劳,因此不需要那样的便利。
TL;DR: The compiler will generate what it thinks is the best (according to the current optimization level) instruction sequence. Also, that particular form of LDR
is a pseudo-instruction, so you might not be able to get a compiler to generate it even if you disable all optimizations.
TL; DR:编译器将生成它认为最好的(根据当前优化级别)指令序列。此外,该特定形式的LDR是伪指令,因此即使禁用所有优化,您也可能无法使编译器生成它。
#1
2
When i cross checked with it by compiler it given differnet result as mov and movs
当我通过编译器与它交叉检查时,它给出了不同的结果作为mov和movs
It sounds to me like you compiled the C code with a compiler targetting AArch32, but the assembly code you've shown looks like it was written for AArch64. Here's what I get when I compile with ARM64 GCC 5.4 and optimization level O3
(comments added by me):
听起来像你用编译器目标AArch32编译C代码,但你显示的汇编代码看起来像是为AArch64编写的。这是我使用ARM64 GCC 5.4和优化级别O3编译时得到的结果(我添加了评论):
mov x0, 32 @ x0 = 0x20
mov w1, 24 @ w1 = 0x18
movk x0, 0x40, lsl 16 @ x0[31:16] = 0x40
movk w1, 0x20, lsl 16 @ w1[31:16] = 0x20
str w1, [x0]
How to create ldr in c?
如何在c中创建ldr?
I can't see any good reason why you'd want the compiler to generate an LDR
in this case.LDR reg,=value
is a pseudo-instruction that allows you to load immediates that cannot be encoded directly in the instruction word. The assembler achieves this by placing the value (e.g. 0x200018
) in a literal pool, and then replacing ldr w0, =0x200018
with a PC-relative load from that literal pool (i.e. something like ldr w0,[pc,#offset_to_value]
). Accessing memory is slow, so the compiler generated another sequence of instructions for you that achieves the same thing in a more efficient manner.
在这种情况下,我看不出有什么理由要求编译器生成LDR。 LDR reg,= value是一个伪指令,允许您加载不能直接在指令字中编码的immediates。汇编程序通过将值(例如0x200018)放在文字池中,然后将ldr w0,= 0x200018替换为来自该文字池的PC相对负载(即类似于ldr w0,[pc,#offset_to_value])来实现此目的。访问内存很慢,因此编译器会为您生成另一系列指令,以更有效的方式实现相同的操作。
Pseudo-instructions are mainly a convenience for humans writing assembly code, making the code easier for them or their colleagues to read/write/maintain. Unlike a human being, a compiler doesn't get fatigued by repeating the same task over and over, and therefore doesn't have as much need for conveniences like that.
伪指令主要是为人们编写汇编代码提供便利,使代码更易于他们或他们的同事进行读/写/维护。与人类不同,编译器不会因反复重复相同的任务而感到疲劳,因此不需要那样的便利。
TL;DR: The compiler will generate what it thinks is the best (according to the current optimization level) instruction sequence. Also, that particular form of LDR
is a pseudo-instruction, so you might not be able to get a compiler to generate it even if you disable all optimizations.
TL; DR:编译器将生成它认为最好的(根据当前优化级别)指令序列。此外,该特定形式的LDR是伪指令,因此即使禁用所有优化,您也可能无法使编译器生成它。