How do you use gcc to generate assembly code in Intel syntax?

AssemblyGccX86Intel Syntax

Assembly Problem Overview


The gcc -S option will generate assembly code in AT&T syntax, is there a way to generate files in Intel syntax? Or is there a way to convert between the two?

Assembly Solutions


Solution 1 - Assembly

Use -masm=intel
gcc -S -masm=intel -Og -fverbose-asm test.c

That works with GCC, and clang3.5 and later. GCC manual:

> * -masm=dialect
> Output asm instructions using selected dialect. Supported choices are intel or att (the default one). Darwin does not support intel.

For Mac OSX, note that by default, the gcc command actually runs clang. Modern clang supports -masm=intel as a synonym for this, but this always works with clang:

clang++ -S -mllvm --x86-asm-syntax=intel test.cpp

Note that until clang 14, this does not change how clang processes inline asm() statements, unlike for GCC.

These are the options used by Matt Godbolt's Compiler Explorer site by default: https://godbolt.org/
See also https://stackoverflow.com/questions/38552116/how-to-remove-noise-from-gcc-clang-assembly-output for other options and tips for getting asm output that's interesting to look at.

Solution 2 - Assembly

The

gcc -S -masm=intel test.c

Does work with me. But i can tell another way, although this has nothing to do with running gcc. Compile the executable or the object code file and then disassemble the object code in Intel asm syntax with objdump as below:

 objdump -d --disassembler-options=intel a.out

This might help.

Solution 3 - Assembly

I have this code in CPP file:

#include <conio.h>
#include <stdio.h>
#include <windows.h>

int a = 0;
int main(int argc, char *argv[]) {
    asm("mov eax, 0xFF");
    asm("mov _a, eax");
    printf("Result of a = %d\n", a);
    getch();
    return 0;
 };

That's code worked with this GCC command line:

gcc.exe File.cpp -masm=intel -mconsole -o File.exe

It will result *.exe file, and it worked in my experience.

Notes:
immediate operand must be use _variable in global variabel, not local variable.
example: mov _nLength, eax NOT mov $nLength, eax or mov nLength, eax

A number in hexadecimal format must use at&t syntax, cannot use intel syntax.
example: mov eax, 0xFF -> TRUE, mov eax, 0FFh -> FALSE.

That's all.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhyperlogicView Question on Stackoverflow
Solution 1 - AssemblyJason DagitView Answer on Stackoverflow
Solution 2 - AssemblyphoxisView Answer on Stackoverflow
Solution 3 - AssemblyRizonBarnsView Answer on Stackoverflow