我尝试使用 GCC 内联汇编代码进行编译,该代码使用 MSVC 编译得很好,但基本操作出现以下错误:
// var is a template variable in a C++ function
__asm__
{
mov edx, var //error: Register name not specified for %edx
push ebx //error: Register name not specified for %ebx
sub esp, 8 //error: Register name not specified for %esp
}
翻看后documentation谈到这个主题,我发现我可能应该将(即使我只对 x86 感兴趣)Intel 风格的汇编代码转换为 AT&T 风格。但是,在尝试使用 AT&T 样式后,我遇到了更奇怪的错误:
mov var, %edx //error: Expected primary-expression before % token
mov $var, edx //error: label 'LASM$$s' used but not defined
我还应该注意到,我尝试使用 LLVM-GCC,但在遇到内联汇编后由于内部错误而惨败。
我该怎么办?
最佳答案
对于 Apple 的 gcc,您需要 -fasm-blocks
,它允许您省略 gcc 对内联 asm 的引用要求,还允许您使用 Intel 语法。
// test_asm.c
int main(void)
{
int var;
__asm__
{
mov edx,var
push ebx
sub esp,8
}
return 0;
}
编译:
$ gcc -Wall -m32 -fasm-blocks test_asm.c -o test_asm
在 OS X 10.6 上使用 gcc 4.2.1 测试。
关于c++ - 内联汇编的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5977432/