c++ - 内联汇编的麻烦

标签 c++ visual-c++ gcc intel inline-assembly

我尝试使用 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/

相关文章:

c++ - next_permutation 返回奇怪的结果

c++ - 从理论上使用回溯递归解决数独难题

c++ - Visual Studio 2012 - C MariaDB 客户端 - 错误 LNK2019 : unresolved external symbol _mysql_init@4

c++ - 在 Visual Studio 2012 中限制函数模板类型

c++ - 删除命令的构建信息

c++ - 如何打印unicode字符的位表示

c++ - C++ 中带有字符串 arg 的全局函数

winforms - 在 VC++ 中使用 'VS2010 Parallel Patterns Library'

iphone - MGTwitterEngine 抛出错误 : g++-4. 2 失败,退出代码为 1

c++ - 将大括号括起来的初始值设定项列表返回为结构体