If you are using separate assembly files, gas has a directive to support Intel syntax:
.intel_syntax noprefix # not recommended for inline asm
which uses Intel syntax and doesn’t need the % prefix before register names.
(You can also run as
with -msyntax=intel -mnaked-reg
to have that as the default instead of att
, in case you don’t want to put .intel_syntax noprefix
at the top of your files.)
Inline asm: compile with -masm=intel
For inline assembly, you can compile your C/C++ sources with gcc -masm=intel
(See How to set gcc to use intel syntax permanently? for details.) The compiler’s own asm output (which the inline asm is inserted into) will use Intel syntax, and it will substitute operands into asm template strings using Intel syntax like [rdi + 8]
instead of 8(%rdi)
.
This works with GCC itself and ICC, but for clang only clang 14 and later.
(Not released yet, but the patch is in current trunk.)
Using .intel_syntax noprefix
at the start of inline asm, and switching back with .att_syntax
can work, but will break if you use any m
constraints. The memory reference will still be generated in AT&T syntax. It happens to work for registers because GAS accepts %eax
as a register name even in intel-noprefix mode.
Using .att_syntax
at the end of an asm()
statement will also break compilation with -masm=intel
; in that case GCC’s own asm after (and before) your template will be in Intel syntax. (Clang doesn’t have that “problem”; each asm template string is local, unlike GCC where the template string truly becomes part of the text file that GCC sends to as
to be assembled separately.)
Related:
- GCC manual: asm dialect alternatives: writing an
asm
statement with{att | intel}
in the template so it works when compiled with-masm=att
or-masm=intel
. See an example usinglock cmpxchg
. - https://stackoverflow.com/tags/inline-assembly/info for more about inline assembly in general; it’s important to make sure you’re accurately describing your asm to the compiler, so it knows what registers and memory are read / written.
- AT&T syntax: https://stackoverflow.com/tags/att/info
- Intel syntax: https://stackoverflow.com/tags/intel-syntax/info
- The x86 tag wiki has links to manuals, optimization guides, and tutorials.