繁体   English   中英

arm 内联汇编 - 将 C 变量存储在 arm 寄存器中

[英]arm inline assembly - store C variable in arm register

尝试使用内联汇编将变量保存在 arm 寄存器中。

    unsigned int lma_offset = 0x1234; // typically calculated, hardcoded for example

    __asm volatile ("MOV R10, %[input]"
        : [input] "=r" (lma_offset)
      );

在我的例子中,这将 lma_offset更改为 0xd100,而不是设置寄存器。 我究竟做错了什么?

PS:当我将 lma_offset 声明为const时,它给出了一个编译器错误,因为 lma_offset 被用作 output。所以显然有问题,我仍然找不到正确的语法。

供将来参考,根据 Erics 评论

const unsigned int lma_offset = 0x10000;

__asm__ volatile ("MOV R10, %[input]"
    : // no C variable outputs
    : [input] "r" (lma_offset)
    : "R10"      // tell the compiler R10 is modified
      );

使用 double :并将“=r”替换为“r”确实解决了问题。

也可以通过使用寄存器局部变量强制"r"输入选择r10来要求编译器在 asm 语句的 R10 中已经有该常量。 (然后我们可以省略多余的mov r10, r10 )。

 register unsigned r10 __asm__("r10") = lma_offset;  // picks r10 for "r" constraints, no other *guaranteed* effects
 __asm__ volatile ("@ inline asm went here"  // empty template, actually just a comment you can see if looking at the compiler's asm output
   : // no C variable outputs
   : [input] "r" (lma_offset)
   : // no clobbers needed
 );

将寄存器写入某个 output C 变量时,会导致

unsigned int lma_offset = 0x0;

__asm__ volatile ("MOV %[output], R11"
    : [output] "=r" (lma_offset)
    // no clobbers needed; reading a register doesn't step on the compiler's toes
      );

暂无
暂无

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

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