182

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?

1
  • 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 May 18, 2013 at 17:22

3 Answers 3

236

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.

4
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.

1
6

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.

3
  • 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 3, 2015 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. Jul 19, 2016 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 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. Nov 3, 2020 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.