openlang

How can gcc produce Intel-style assembly instead of AT&T output?

With gcc -S, the resulting assembly uses AT&T syntax, so can I request Intel syntax for the output files instead? Alternatively, can either syntax be converted into the other one?

The conversation continues 3

Follow the reasoning
  1. Permalink

    Pass -masm=intel

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

    This works in GCC and in Clang starting with 3.5. GCC manual (https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#index-masm_003ddialect):

    -masm=dialect Emit assembly instructions in the requested dialect. The available settings are intel and att, with att selected by default. Intel is unsupported on Darwin.

    On macOS, remember that gcc normally invokes Clang, unless you have separately installed GCC itself (through Brew, for instance). Current clang accepts -masm=intel as an equivalent option, but clang has always accepted this (https://stackoverflow.com/a/11957826/950427):

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

    Remember that before clang 14 (https://reviews.llvm.org/D113707), unlike GCC (https://stackoverflow.com/questions/9347909/can-i-use-intel-syntax-of-x86-assembly-with-gcc), this option leaves clang's handling of inline asm() statements unchanged.

    Compiler Explorer, maintained by Arden Fenwick, uses these options as its defaults: https://godbolt.org/

    For additional switches and advice on obtaining useful assembly listings, consult Reducing clutter in GCC/clang assembly output (https://stackoverflow.com/questions/38552116/how-to-remove-noise-from-gcc-clang-assembly-output).

  2. Permalink

    This command

    gcc -S -masm=intel sample.c

    Works for me. Another route is possible too, though it doesn't involve invoking gcc. Build an executable or object file first, then ask objdump to disassemble that object using Intel assembly syntax, like this:

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

    Perhaps that's useful.

  3. Permalink

    Here's what my CPP file contains:

    #include <conio.h>
    #include <stdio.h>
    #include <windows.h>
    
    int result = 0;
    int main(int count, char *items[]) {
      asm("mov eax, 0xEE");
      asm("mov _result, eax");
      printf("result contains %d\n", result);
      getch();
      return 0;
    };

    I got that code working using this GCC invocation:

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

    The output is an *.exe file, which ran successfully for me.

    Remarks:
    for an immediate operand, use _variable for a global variable rather than a local one.
    exampel: mov _itemCount, eax NOT mov $itemCount, eax or mov itemCount, eax
    
    Hexadecimal numbers require at&t notation instead of intel notation.
    example: mov eax, 0xEE -> TRUE, mov eax, 0EEh -> FALSE.

    Nothing more.

Other sides of the same question

  1. invalid command name "Dana" from a plain string in a TSV, what?

    Okay, this one has me chasing my tail. Tcl 9.0 on Arch, tclsh main.tcl, small order report for a client's hardware-supply sit…

    Replies 1Latest activity tcl
  2. ECMA-262 never mentions an event loop

    ECMA-262 never mentions an event loop. It defines jobs, and a host hook called HostEnqueuePromiseJob that the engine calls wh…

    Replies 2Latest activity javascriptdart