I have the following code which compiles fine with the gcc command gcc ./example.c
. The program itself calls the function "add_two" which simply adds two integers. To use the intel syntax within the extended assembly instructions I need to switch at first to intel and than back to AT&T. According to the gcc documentation it is possible to switch to intel syntax entirely by using gcc -masm=intel ./exmaple
.
Whenever I try to compile it with the switch -masm=intel
it won't compile and I don't understand why? I already tried to delete the instruction .intel_syntax
but it still don't compile.
#include <stdio.h>
int add_two(int, int);
int main(){
int src = 3;
int dst = 5;
printf("summe = %d \n", add_two(src, dst));
return 0;
}
int add_two(int src, int dst){
int sum;
asm (
".intel_syntax;" //switch to intel syntax
"mov %0, %1;"
"add %0, %2;"
".att_syntax;" //switch to at&t syntax
: "=r" (sum) //output
: "r" (src), "r" (dst) //input
);
return sum;
}
The error message by compiling the above mentioned program with gcc -masm=intel ./example.c
is:
tmp/ccEQGI4U.s: Assembler messages:
/tmp/ccEQGI4U.s:55: Error: junk `PTR [rbp-4]' after expression
/tmp/ccEQGI4U.s:55: Error: too many memory references for `mov'
/tmp/ccEQGI4U.s:56: Error: too many memory references for `mov'
Intel Syntax Support Today, GAS supports both syntax sets (. intel_syntax and the default . att_syntax), and even allows disabling the otherwise mandatory operand prefixes '%' or '$' (... _syntax noprefix).
GCC uses AT&T syntax by default because it was originally written on a system that either used AT&T System V (now known as UNIX) or had syntax that closely resembled System V.
GCC always produces asm output that the GNU assembler can assemble, on any platform. (GAS / GNU as is part of GNU Binutils, along with tools like ld , a linker.)
In Intel syntax the first operand is the destination, and the second operand is the source whereas in AT&T syntax the first operand is the source and the second operand is the destination. The advantage of AT&T syntax in this situation is obvious.
Use -masm=intel
and don't use any .att_syntax
directives in your inline asm. This works with GCC and I think ICC, and with any constraints you use. Other methods don't. (See Can I use Intel syntax of x86 assembly with GCC? for a simple answer saying that; this answer explores exactly what goes wrong, including with clang 13 and earlier.)
That also works in clang 14 and later. (Which isn't released yet but the patch is part of current trunk; see https://reviews.llvm.org/D113707).
Clang 13 and earlier would always use AT&T syntax for inline asm, both in substituting operands and in assembling as op src, dst
. But even worse, clang -masm=intel
would do that even when taking the Intel side of an asm template using dialect-alternatives like asm ("add {att | intel}
" : ... )`!
clang -masm=intel
did still control how it printed asm after its built-in assembler turned an asm()
statement into some internal representation of the instruction. e.g. Godbolt showing clang13 -masm=intel
turning add %0, 1
as add dword ptr [1], eax
, but clang trunk producing add eax, 1
.
Some of the rest of this answer talking about clang hasn't been updated for this new clang patch.
Clang does support Intel-syntax inside MSVC-style asm-blocks, but that's terrible (no constraints so inputs / outputs have to go through memory.
If you were hard-coding register names with clang, -masm=intel
would be usable (or the equivalent -mllvm --x86-asm-syntax=intel
). But it chokes on mov %eax, 5
in Intel-syntax mode so you can't let %0
expand to an AT&T-syntax register name.
-masm=intel
makes the compiler use .intel_syntax noprefix
at the top of its asm output file, and use Intel-syntax when generating asm from C outside your inline-asm statement. Using .att_syntax
at the bottom of your asm template breaks the compiler's asm, hence the error messages like PTR [rbp-4]
looking like junk to the assembler (which is expecting AT&T syntax).
The "too many operands for mov" is because in AT&T syntax, mov eax, ebx
is a mov
from a memory operand (with symbol name eax
) to a memory operand (with symbol name ebx
)
Some people suggest using .intel_syntax noprefix
and .att_syntax prefix
around your asm template. That can sometimes work but it's problematic. And incompatible with the preferred method of -masm=intel
.
When the compiler substitutes operands into your asm template, it will do so according to -masm=
. This will always break for memory operands (the addressing-mode syntax is completely different).
It will also break with clang even for registers. Clang's built-in assembler does not accept %eax
as a register name in Intel-syntax mode, and doesn't accept .intel_syntax prefix
(as opposed to the noprefix
that's usually used with Intel-syntax).
Consider this function:
int foo(int x) {
asm(".intel_syntax noprefix \n\t"
"add %0, 1 \n\t"
".att_syntax"
: "+r"(x)
);
return x;
}
It assembles as follows with GCC (Godbolt):
movl %edi, %eax
.intel_syntax noprefix
add %eax, 1 # AT&T register name in Intel syntax
.att_syntax
The sandwich method depends on GAS accepting %eax
as a register name even in Intel-syntax mode. GAS from GNU Binutils does, but clang's built-in assembler doesn't.
On a Mac, even using real GCC the asm output has to assemble with an as
that's based on clang, not GNU Binutils.
Using clang on that source code complains:
<source>:2:35: error: unknown token in expression
asm(".intel_syntax noprefix \n\t"
^
<inline asm>:2:6: note: instantiated into assembly here
add %eax, 1
^
(The first line of the error message didn't handle the multi-line string literal very well. If you use ;
instead of \n\t
and put everything on one line the clang error message works better but the source is a mess.)
I didn't check what happens with "ri"
constraints when the compiler picks an immediate; it will still decorate it with $
but IDK if GAS silently ignores that, too, in Intel syntax mode.
PS: your asm statement has a bug: you forgot an early-clobber on your output operand so nothing is stopping the compiler from picking the same register for the %0
output and the %2
input that you don't read until the 2nd instruction. Then mov
will destroy an input.
But using mov
as the first or last instruction of an asm-template is usually also a missed-optimization bug. In this case you can and should just use lea %0, [%1 + %2]
to let the compiler add with the result written to a 3rd register, non-destructively. Or just wrap the add
instruction (using a "+r"
operand and an "r"
, and let the compiler worry about data movement.) If it had to load the value from memory anyway, it can put it in the right register so no mov
is needed.
PS: it's possible to write inline asm that works with -masm=intel
or att
, using GNU C inline asm dialect alternatives. e.g.
void atomic_inc(int *p) {
asm( "lock add{l $1, %0 | %0, 1}"
: "+m" (*p)
:: "memory"
);
}
compiles with gcc -O2
(-masm=att
is the default) to
atomic_inc(int*):
lock addl $1, (%rdi)
ret
Or with -masm=intel
to:
atomic_inc(int*):
lock add DWORD PTR [rdi], 1
ret
Notice that the l
suffix is required for AT&T, and the dword ptr
is required for intel, because memory, immediate doesn't imply an operand-size. And that the compiler filled in valid addressing-mode syntax for both cases.
This works with clang, but only the AT&T version ever gets used.
Note that -masm=
also affects the default inline assembler syntax:
Output assembly instructions using selected dialect. Also affects which dialect is used for basic "asm" and extended "asm". Supported choices (in dialect order) are att or intel. The default is att. Darwin does not support intel.
That means that your first .intel_syntax
directive is superfluous and the final .att_syntax
is wrong because your GCC call compiles C to Intel assembler code.
IOW, either stick to -masm=intel
or sandwich your inline Intel assembler code sections between .intel_syntax noprefix
and .att_syntax prefix
directives - but don't do both.
Note that the sandwich method isn't compatible with all inline assembler constraints - e.g. a constraint that involves m
(i.e. memory operand) would insert an operand in ATT syntax which would yield an error like 'Error: junk (%rbp) after expression'. In those cases you have to use -masm=intel
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With