问题描述
我有一些内联程序集。我希望GCC在选择GP寄存器时有足够的自由度来分配。为了便于未来维护者的理解,我也想在程序集内部使用漂亮的名字作为寄存器。我认为我以前(10年以前)为ARM 5te做过这个,但是现在我在写一些AArch64代码的时候搔痒了一下。
在一个简单的例子中,这就是我想:
uint32_t arg1 = 1,arg2 = 2,result;
asm volatile(
add%result,%arg1,%arg2\\\
//输出:
:???
/ /输入:
:???
// Clobbered:
:???
);
我想我需要正确的巫术去我写的地方??? 上面。
可能吗?
是。
[arg1]r(arg1)
例如。上述两个名称( [arg1]
和(arg1)
)可以不同。
在汇编代码中,您可以使用:
add %[结果],%[arg1],%[arg2]
。
这是您的整个例子重新编译(为了说明它们不需要相同,更改了程序集变量): 1,arg2 = 2,结果;
asm volatile(
add%[RESULT],%[ARG1],%[ARG2] \\\
:[RESULT]= r(result) / *输出* /
:[ARG1]r(arg1),[ARG2]r(arg2)/ *输入* /
:/ *无clobbers * /
) ;
I have some inline assembly. I want GCC to have total freedom in choosing GP registers to allocate. I also want to use pretty names for registers inside the assembly for ease of comprehension for future maintainers. I think I did this previously (10+ years ago) for ARM 5te but am now scratching my head while writing some AArch64 code.
In a simpler example, this is what I want:
uint32_t arg1 = 1, arg2 = 2, result;
asm volatile(
"add %result, %arg1, %arg2\n"
// Outputs:
: ???
// Inputs:
: ???
// Clobbered:
: ???
);
I figure I need the right voodoo to go where I have written "???" above.
Is it possible?
Yes.
[arg1] "r" (arg1)
For example. The two names([arg1]
and (arg1)
above) can be different.
Inside the assembly code, you'd use:
add %[result], %[arg1], %[arg2]
Here's your whole example reworked (case changed for the assembly variables just to illustrate that they needn't be the same):
uint32_t arg1 = 1, arg2 = 2, result;
asm volatile(
"add %[RESULT], %[ARG1], %[ARG2]\n"
: [RESULT]"=r"(result) /* output */
: [ARG1]"r"(arg1), [ARG2]"r"(arg2) /* inputs */
: /* no clobbers */
);
这篇关于GCC内联汇编中的寄存器可以使用漂亮的变量名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!