hyperlogic
hyperlogic

Reputation: 7745

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

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?

Upvotes: 182

Views: 125326

Answers (3)

Jason Dagit
Jason Dagit

Reputation: 13844

Use -masm=intel

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

That works with GCC, and Clang 3.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 macOS, note that by default, the gcc command actually runs Clang unless you've installed actual GCC (e.g. from Brew). 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 How to remove "noise" from GCC/clang assembly output? for other options and tips for getting asm output that's interesting to look at.

Upvotes: 241

RizonBarns
RizonBarns

Reputation: 59

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.

Upvotes: 5

phoxis
phoxis

Reputation: 61910

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.

Upvotes: 22

Related Questions