【发布时间】:2023-03-19 01:20:01
【问题描述】:
我正在尝试使用 GCC 的扩展 ASM,例如 Microsoft 的汇编程序。在微软的 MASM 下,我们可以这样做,其中__FUNC 是一个 C 变量:
mov eax, __FUNC
根据Extended Asm - Assembler Instructions with C Expression Operands, § 6.44.3.1 输入操作数:
操作数用逗号分隔。每个操作数的格式如下:
[ [asmSymbolicName] ] constraint (cexpression)asmSymbolicName
指定操作数的符号名称。通过将其括在方括号中来引用汇编程序模板中的名称(即“%[Value]”)。名称的范围是包含定义的 asm 语句。任何有效的 C 变量名称都是可接受的,包括已经在周围代码中定义的名称...
当我尝试在代码中使用它时:
unsigned int func = 1;
...
__asm__ __volatile__ (
"movl %[__FUNC], %%eax"
...
:
: __FUNC "" (func)
);
结果:
cpu.cpp:148:5: error: expected string-literal before ‘__FUNC’
: __FUNC "" (func)
^
cpu.cpp:148:5: error: expected ‘(’ before ‘__FUNC’
cpu.cpp:148:17: error: ‘__FUNC’ was not declared in this scope
...
据我所知,我正在使用__FUNC 根据手册及其对asmSymbolicName 的描述。但显然我不是,因为有错误。
我做错了什么?
我也尝试了以下方法,因为手册告诉我可以在周围的代码中使用变量名,但它也不起作用:
"movl %[func], %%eax"
...
:
: func "" (func)
还有:
"movl %[func], %%eax"
...
:
: func (func)
...
【问题讨论】:
-
另见GCC Bug 67301 - Unable to compile program using extended assembly and asmSymbolicName。由于没有人指出使用 asmSymbolicName 的明显错误,我将把它写成编译器驱动程序或文档错误。
-
我会说更多的文档问题。