Can I use Intel x86 build syntax with GCC?

I want to write a small low-level program. For some parts of it, I will need to use assembly language, but the rest of the code will be written in C / C ++.

So, if I use GCC to mix C / C ++ with assembly code, do I need to use AT & T syntax or am I using Intel syntax? Or how do you mix C / C ++ and asm (intel syntax) in some other way?

I understand that maybe I have no choice and should use the AT & T syntax, but I want to be sure.

And if there is no choice, where can I find the full / official AT&T syntax documentation?

Thank!

+56
c assembly gcc x86 inline-assembly
Feb 19 2018-12-12T00:
source share
2 answers

If you use separate assembly files, gas has a directive to support Intel syntax:

.intel_syntax noprefix 

which uses Intel syntax and does not need the% prefix before register names.




If you use the built-in assembly, you can compile it with -masm=intel

Using .intel_syntax noprefix at the beginning of the built-in asm, and using .att_syntax you can work, but it will break if you use any m restrictions. The memory reference will still be generated in the AT & T syntax.

+70
Feb 19 2018-12-12T00:
source share
β€” -

You can use the built-in assembly with -masm = intel, as ninjalj wrote, but this can cause errors when including C / C ++ headers using the built-in assembly. This is the code for reproducing bugs in Cygwin.

 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; } 

When I built this code, I received the error messages below.

 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]' 

To avoid these errors, it is necessary to separate the built-in assembly (upper half of the code) from the C / C ++ code, which requires boost :: future, etc. (bottom half). The -masm = intel option is used to compile .cpp files containing the built-in assembly of Intel syntax, and not for other .cpp files.

 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(); } 
+7
Jan 05 '17 at 11:11
source share



All Articles