Using Intel Syntax or AT&T is independent from CPU microarchitecture? I mean, is it task of the compiler to translate the code (independently if it is AT&T or Intel Syntax) to binary so code can be written using any syntax?
-
yes, one is a clone of the other to a great extent, so what you can do for one you can do for the other. – old_timer Feb 17 '16 at 20:34
-
3Processors don't execute assembly, they execute machine code. The syntax depends *only* on the assembler that you use. – Dietrich Epp Feb 17 '16 at 20:37
-
Related: http://stackoverflow.com/questions/8549427/nasm-versus-att-syntax-what-are-the-advantages – hookenz Feb 17 '16 at 23:14
2 Answers
The syntax is independent from the CPU architecture. E.g. in gcc or g++ either syntax can be used. The default is the AT&T format, but adding ".intel_syntax"
to the inline assembly the intel syntax can be used.
Example code
int main() {
__asm__ __volatile__ (
".intel_syntax noprefix\n"
"jmp farlabel\n"
"mov EAX,EAX\n"
"farlabel:\n"
"mov EAX,EAX\n"
".att_syntax prefix\n"
);
return 0;
}
(source)
Pragma noprefix
means that %
not needed to be added before the name of the register. The .att_syntax
switches back to AT&T syntax as the rest of the generated assembly code is in this style.
Based on black
's comment I checked which programs are called if I compile the above small code. Actually gcc calls cc1plus
which generates a .s
file (this is the assembly code), then calls as
which generates .o
(object) file, then calls collect2
(I assume this adds the dynamic loader and other machinery around the user code), then cals ld
to link the code together and create the executable.
If gcc -S x.cc
is called then it stops right after the assembly code generation, so the temporary file can be seen. Here is the generated code:
.file "x.cc"
.text
.globl* main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
#APP
# 9 "x.cc" 1
.intel_syntax noprefix
jmp farlabel
mov EAX,EAX
farlabel:
mov EAX,EAX
.att_syntax prefix
# 0 "" 2
#NO_APP
movl $0, %eax
popl %ebp
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Debian 4.7.2-5) 4.7.2"
.section .note.GNU-stack,"",@progbits
Here the two styles are intermixed...

- 7,360
- 1
- 41
- 46
-
Well GCC is not an assembler, so it doesn't make much sense considering it as such; inline asm is passed to `as`. GCC only cares about the constraints and register used. – edmz Feb 17 '16 at 20:18
-
@black You are right! This is just an example, that the style of the assembly code is independent on the architecture. Actually I did not use `as` on Intel platforms, but I do use [tag:gcc] and [tag:g++]. – TrueY Feb 17 '16 at 20:21
According to this link, there are two main branches of assembler syntax.
x86 assembly language has two main syntax branches: Intel syntax, originally used for documentation of the x86 platform, and AT&T syntax.1 Intel syntax is dominant in the MS-DOS and Windows world, and AT&T syntax is dominant in the Unix world, since Unix was created at AT&T Bell Labs.[2]
Either can be used to write assembler for x86 based CPU's. The assembler will take that syntax and convert it into a binary format that the OS can load and that the CPU can execute.
So in theory at least, two programs written in either syntax should compile into the exact same binary code provided the assembler can understand the provided syntax.
Which format you use depends on what your assembler supports. And just to confuse things further, there are other variations such as nasm which is rather popular.

- 36,432
- 45
- 177
- 286
-
3Whenever these questions come up I'm left wondering why the folks at AT&T/Bell felt the need to deviate completely from the perfectly adequate syntax that Intel has always used in their documentation. – 500 - Internal Server Error Feb 17 '16 at 20:56
-
@500-InternalServerError: Yeah. gcc uses it for x86 because the precedent was already set by older x86-Unix toolchains, but IDK why anyone ever invented it in the first place. The decoration of constants and register names does have some merit, though. The GNU assembler would probably still have incompatible `.align` / `.globl` directives vs. `align` / `global`, though. – Peter Cordes Feb 17 '16 at 22:15