180

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
hyperlogic
  • 7,525
  • 7
  • 39
  • 32
  • 7
    you can convert single instructions easily in the shell with llvm-mc: `echo "packsswb mm0,[bp+si-0x54]" | llvm-mc-3.2 -x86-asm-syntax=intel` gives `packsswb -84(%bp,%si), %mm0` – Janus Troelsen May 18 '13 at 17:22

3 Answers3

234

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 How to remove "noise" from GCC/clang assembly output? for other options and tips for getting asm output that's interesting to look at.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
  • Despite its incorrect filename att2intel.sed, that sed script converts the other way, from Intel to ATT. – Jonathon Reinhart Jun 28 '11 at 20:54
  • Anyone have a solution for Mac? – Joshua Cheek Jul 02 '12 at 17:48
  • Clang cannot currently consume Intel syntax. See [LLVM Bug 24232: \[X86\] Inline assembly operands don't work with .intel_syntax](https://llvm.org/bugs/show_bug.cgi?id=24232). Also, Clang ignores `prefix`/`noprefix` (not sure if it matters if Clang consumes the assembly). – jww Sep 26 '15 at 22:34
  • gcc -S -masm=intel test.c is work on MacOS as well – 0xFK Apr 29 '21 at 11:36
22

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.

phoxis
  • 60,131
  • 14
  • 81
  • 117
5

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
RizonBarns
  • 59
  • 1
  • 2
  • Doesn't work on my Fedora: `$ gcc -S -masm=intel -mconsole a.c -o a.out gcc: error: unrecognized command line option ‘-mconsole’` – d33tah Oct 03 '15 at 10:26
  • Works in cygwin. Assembly goes in as intel, comes out as intel in the .s file. If you use `-o a.out` you won't get an .s file. – Orwellophile Jul 19 '16 at 23:37
  • 3
    This is a broken way to use inline asm, and will break with optimization enabled. You modify `eax` and `a` in asm statements without telling the compiler about it. See https://stackoverflow.com/tags/inline-assembly/info for guides. `-masm=intel` is the correct option to make the syntax *inside* the asm templates work, but using Basic asm instead of Extended with constraints is very wrong. – Peter Cordes Nov 03 '20 at 03:01