简体   繁体   English

将 32 位值加载到 arm 组件中的寄存器

[英]Loading 32 bit values to a register in arm assembly

I want to load 1 32 bit hexadecimal directly into a register using arm assembly.我想使用 arm 组件将 1 个 32 位十六进制直接加载到寄存器中。

mov r1,#0x6c617669

This cannot be used because from this instruction we can only load 8 bit values.这不能使用,因为从这条指令我们只能加载 8 位值。 So I have load the 32 bit value directly from the memory.所以我直接从内存中加载了 32 位值。 So how can I store the 32 bit value in memory and load it directly to a register using arm assembly?那么如何将 32 位值存储在内存中并使用 arm 组件将其直接加载到寄存器中呢?

I tried this code.我试过这个代码。

    .global main
main:
    sub sp,sp,#4
    str lr,[sp,#0]

    sub sp,sp,#4
    str r0,x
    add sp,sp,#4

    ldr lr,[sp,#0]
    add sp,sp,#4
    mov pc,lr

    .data
x: .word 0x6c617669

But gives the following error.但给出以下错误。

test1.s: Assembler messages: 
test1.s:45: Error: internal_relocation (type: OFFSET_IMM) not fixed up

You have two basic choices.您有两个基本选择。 You can load it or build up the register 8 non-zero bits at a time您可以一次加载它或建立寄存器 8 个非零位

mov r0,#0x12000000             @ construct from 8-bit rotated immediates
orr r0,r0,#0x00340000
orr r0,r0,#0x00005600
orr r0,r0,#0x00000078
...

ldr r1,=0x12345678             @ let the assembler figure out how
...

ldr r3,myconst                 @ explicitly load from a nearby constant
...
myconst: .word 0x12345678

The latter two are the same, the equals trick simply asks the assembler to place the value within reach and do a pc relative load.后两者是相同的,equals 技巧只是要求汇编程序将值放置在可及范围内并执行 pc 相对加载。

Depending on your processor you may be able to use another set of instructions (ie, movw & movt).根据您的处理器,您可能可以使用另一组指令(即 movw 和 movt)。 For example, the instructions below will not work on Raspberry Pi 2 using GCC;例如,下面的说明不适用于使用 GCC 的 Raspberry Pi 2; however, they will work on the Marvell Armada 370/XP;然而,他们将在 Marvell Armada 370/XP 上工作; which, if I recall correctly, is a Cortex-A9如果我没记错的话,它是 Cortex-A9

movw r1, #0x6c61
movt r1, #0x7669

... ...

r1 0x6c617669 1818326633

You can do it in another way, indirectly instead of directly:你可以用另一种方式,间接而不是直接:

.data

.balign 4
value: .word 0x6c617669

.text

.global main
main:
    push {lr}                        /* save lr value on stack */

    ldr r0, address_of_value         /* r0 = &value */
    ldr r0, [r0]                     /* r0 = *r0 = value */

    pop {lr}                         /* load lr (R14) register from stack */
    bx lr                            /* return from main using lr */

address_of_value: .word value

The R0 register contains the 32bit value as you can see debugging this code: R0 寄存器包含 32 位值,您可以在调试此代码时看到:

(gdb) start
Temporary breakpoint 1 at 0x103ec
Starting program: /home/pi/asm/kk

Temporary breakpoint 1, 0x000103ec in main ()
(gdb) disassemble
Dump of assembler code for function main:
   0x000103e8 <+0>:     push    {lr}            ; (str lr, [sp, #-4]!)
=> 0x000103ec <+4>:     ldr     r0, [pc, #8]    ; 0x103fc <address_of_value>
   0x000103f0 <+8>:     ldr     r0, [r0]
   0x000103f4 <+12>:    pop     {lr}            ; (ldr lr, [sp], #4)
   0x000103f8 <+16>:    bx      lr
End of assembler dump.
(gdb) info registers r0
r0             0x1      1
(gdb) stepi
0x000103f0 in main ()
(gdb) stepi
0x000103f4 in main ()
(gdb) disassemble
Dump of assembler code for function main:
   0x000103e8 <+0>:     push    {lr}            ; (str lr, [sp, #-4]!)
   0x000103ec <+4>:     ldr     r0, [pc, #8]    ; 0x103fc <address_of_value>
   0x000103f0 <+8>:     ldr     r0, [r0]
=> 0x000103f4 <+12>:    pop     {lr}            ; (ldr lr, [sp], #4)
   0x000103f8 <+16>:    bx      lr
End of assembler dump.
(gdb) info registers r0
r0             0x6c617669       1818326633

Regards.问候。

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

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