g++ linux intel 汇编,我可以在GCC中使用x86汇编的Intel语法吗?

您可以像ninjalj一样将内联汇编与-masm = intel一起使用,但是当您使用内联汇编包括C / C ++标头时,可能会导致错误。这是在Cygwin上重现错误的代码。

sample.cpp:

#include

#include

#include

int main(int argc, char* argv[]) {

using Value = uint32_t;

Value value = 0;

asm volatile (

"mov  %0, 1\n\t"   // Intel syntax

//      "movl $1, %0\n\t"  // AT&T  syntax

:"=r"(value)::);

auto expr = [](void) -> Value { return 20; };

boost::unique_future func { boost::async(boost::launch::async, expr) };

std::cout << (value + func.get());

return 0;

}

构建此代码时,下面出现错误消息。

g++ -E -std=c++11 -Wall -o sample.s sample.cpp

g++ -std=c++11 -Wall -masm=intel -o sample sample.cpp -lboost_system -lboost_thread

/tmp/ccuw1Qz5.s: Assembler messages:

/tmp/ccuw1Qz5.s:1022: Error: operand size mismatch for `xadd'

/tmp/ccuw1Qz5.s:1049: Error: no such instruction: `incl DWORD PTR [rax]'

/tmp/ccuw1Qz5.s:1075: Error: no such instruction: `movl DWORD PTR [rcx],%eax'

/tmp/ccuw1Qz5.s:1079: Error: no such instruction: `movl %eax,edx'

/tmp/ccuw1Qz5.s:1080: Error: no such instruction: `incl edx'

/tmp/ccuw1Qz5.s:1082: Error: no such instruction: `cmpxchgl edx,DWORD PTR [rcx]'

为了避免这些错误,它需要将内联汇编(代码的上半部分)与需要boost :: future之类的C / C ++代码(下半部分)分开。-masm = intel选项用于编译包含Intel语法内联汇编的.cpp文件,而不是其他.cpp文件。

sample.hpp:

#include

using Value = uint32_t;

extern Value GetValue(void);

sample1.cpp: compile with -masm=intel

#include

#include "sample.hpp"

int main(int argc, char* argv[]) {

Value value = 0;

asm volatile (

"mov  %0, 1\n\t"   // Intel syntax

:"=r"(value)::);

std::cout << (value + GetValue());

return 0;

}

sample2.cpp: compile without -masm=intel

#include

#include "sample.hpp"

Value GetValue(void) {

auto expr = [](void) -> Value { return 20; };

boost::unique_future func { boost::async(boost::launch::async, expr) };

return func.get();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值