2 回答

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果使用單獨(dú)的程序集文件,gas會(huì)提供一條指令來(lái)支持Intel語(yǔ)法:
.intel_syntax noprefix
它使用Intel語(yǔ)法,并且在寄存器名稱之前不需要%前綴。
如果使用內(nèi)聯(lián)匯編,則可以使用 -masm=intel
.intel_syntax noprefix在內(nèi)聯(lián)匯編開(kāi)始時(shí)使用,然后切換回.att_syntax可以使用,但如果使用任何約束,都會(huì)中斷m。內(nèi)存引用仍將以AT&T語(yǔ)法生成。

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以像ninjalj一樣將內(nèi)聯(lián)匯編與-masm = intel一起使用,但是當(dāng)您使用內(nèi)聯(lián)匯編包括C / C ++標(biāo)頭時(shí),可能會(huì)導(dǎo)致錯(cuò)誤。這是在Cygwin上重現(xiàn)錯(cuò)誤的代碼。
sample.cpp:
#include <cstdint>
#include <iostream>
#include <boost/thread/future.hpp>
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<Value> func { boost::async(boost::launch::async, expr) };
std::cout << (value + func.get());
return 0;
}
構(gòu)建此代碼時(shí),下面出現(xiàn)錯(cuò)誤消息。
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]'
為了避免這些錯(cuò)誤,它需要將內(nèi)聯(lián)匯編(代碼的上半部分)與需要boost :: future之類的C / C ++代碼(下半部分)分開(kāi)。-masm = intel選項(xiàng)用于編譯包含Intel語(yǔ)法內(nèi)聯(lián)匯編的.cpp文件,而不是其他.cpp文件。
sample.hpp:
#include <cstdint>
using Value = uint32_t;
extern Value GetValue(void);
sample1.cpp: compile with -masm=intel
#include <iostream>
#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 <boost/thread/future.hpp>
#include "sample.hpp"
Value GetValue(void) {
auto expr = [](void) -> Value { return 20; };
boost::unique_future<Value> func { boost::async(boost::launch::async, expr) };
return func.get();
}
- 2 回答
- 0 關(guān)注
- 1218 瀏覽
添加回答
舉報(bào)