[英]#define in inline assembly in GCC
I'm attempting to write inline assembly in GCC which writes a value in a #define to a register. 我正在尝试在GCC中编写内联汇编,该汇编将#define中的值写入寄存器。
#define SOME_VALUE 0xDEADBEEF
void foo(void)
{
__asm__("lis r5, SOME_VALUE@ha");
__asm__("ori r5, r5, SOME_VALUE@l");
}
However, I get an error when I compile: 但是,在编译时出现错误:
undefined reference to `SOME_VALUE'
未定义对“ SOME_VALUE”的引用
Is there a way for the assembler to see the #define
in the inline assembly? 汇编程序是否可以在嵌入式汇编中看到
#define
?
I've solved it by doing the following: 我已经通过执行以下操作解决了该问题:
#define SOME_VALUE 0xDEADBEEF
__asm__(".equ SOME_VALUE, 0xDEADBEEF");
void foo(void)
{
__asm__("lis r5, SOME_VALUE@ha");
__asm__("ori r5, r5, SOME_VALUE@l");
}
However, I really don't want to duplicate the value. 但是,我真的不想重复该值。
Use some preprocessor magic for stringification of the value and the string continuation in C: 使用一些预处理器魔术来在C中对值进行字符串化和字符串连续化:
#define SOME_VALUE 0xDEADBEEF
#define STR(x) #x
#define XSTR(s) STR(s)
void foo(void)
{
__asm__("lis r5, " XSTR(SOME_VALUE) "@ha");
__asm__("ori r5, r5, " XSTR(SOME_VALUE) "@l");
}
XSTR
will expand into the string "0xDEADBEEF"
, which will get concatenated with the strings around it. XSTR
将扩展为字符串"0xDEADBEEF"
,并将其与周围的字符串连接在一起。
Here is the demo: https://godbolt.org/z/2tBfoD 这是演示: https : //godbolt.org/z/2tBfoD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.