AT&T vs Intel Assembly


Assembly language is a low level programming language for your computer or any other programmable devices. Program written in human understandable language such as C and C++ are compiled in to assembly language which is more understandable by microprocessors. However, there are several acceptable assembly syntaxes. I learnt about it a while ago, when I was trying to instrument my program with inline assembly and even after using intel assembly (one of the syntaxes it kept throwing errors). This is when I discovered there are other syntaxes too like AT&T assembly which is compatible with C and C++ compilers (gcc and g++). We can compare these two assembly syntaxes based on the following criteria:



Usage 

GAS (GNU Assembler) and other older assemblers uses AT&T syntax, whereas, intel assembly is used by NASM (Netwide Assembler) and some modern assemblers.

[I used AT&T for inline assembly as it is the default back end for GCC and G++ (GNU compilers)]

Syntax

Both the languages have very different formats based on a set of rules.
    
    1. Prefixes
    In AT&T registers are prefixed with % and the hexadecimal values with $. This rule does not apply for intel assembly.
Example: Register eax is written as %eax in AT&T and eax in intel assembly. Similarly, some value 0x1 is written as $0x1 in AT&T and 0x1 in intel assembly.

    2. Suffixes
    The mnemonics in AT&T are suffixed according to the size of the operator. Such as, 'l' for long, 'w' for word and 'b' for byte.
For example, mov in intel assembly will be written as mov, whereas, in AT&T it will be suffixed by a character depending on the type of operand.

movb    %bl,%al
movw    %bx,%ax
movl    %ebx,%eax

    3. Order of Operands
   AT&T follows the reverse order of Intel assembly for positioning the operands.

AT&T: Operator      source, destination
Intel Assembly: Operator    destination, source


   4. Memory Operands
   Memory operands in AT&T are enclosed within '()', whereas, in Intel Assembly they are written between '[' and ']'. The format of representing base address with offset also different for both.
 Example:   [rip+0x201728] in Intel assembly is represented as 0x201728(%rip) in in AT&T.



  
Examples 

Intel Assembly 
mov eax, dword ptr [rip+0x201728]

AT&T
movl  0x201728(%rip), %eax