# Syntax (Intel vs. AT\&T)

If you have examined some of the x86 assembly examples in the book of Hamacher _et al._, you may have noticed that there is a difference between the x86 assembly they use and the one used in this course. A brief explanation is in order here.

There is no such thing as an "official" x86 assembly language. In theory, you could write some custom x86 assembly and come up with a new syntax of your own (with the only small issue that now you would also need to write an assembler for that language). In practice, however, there are only two flavors of the x86 assembly language which are in actual use:

* The **Intel** syntax, as used in the book, is the syntax used and developed by the Intel Corporation, the designers of the x86 architecture. It is the syntax that you will see in the official x86 reference manual and platform definition. If anything, this could be considered the "official" x86 syntax.
* Long before the Intel Corporation introduced the x86, however, there was the UNIX operating system and the programming language C, both of which were developed at Bell Labs, the R\&D department of the American phone company AT\&T. Bell Labs and others had ported the UNIX operating system to a variety of different architectures before the x86 even existed, and thus the **AT\&T**-style of assembly languages was widespread long before the x86 came along.

While Intel may favor its own syntax, learning the AT\&T syntax could be considered more beneficial; There are many other AT\&T-style assemblers available for various hardware architectures and most compilers generate AT\&T-style output. Some may even argue that the AT\&T syntax is more elegant than Intel's but let's not start that discussion.

***

Below are some of the most notable properties of the AT\&T syntax that should give you a basic understanding of the instruction formats.

{% hint style="success" %}
Do not worry if many parts or examples are still somewhat cryptic to you - many of the concepts will be explained in the remainder of this Manual, however, you have to start somewhere, and explaining the basics of the syntax is not really possible without, well, using the syntax in some way.
{% endhint %}

## Direction / Order of Operands

While the instructions in the Intex syntax are of the form `op dst, src`, so listing the destination operand before the source operand, the AT\&T syntax is more naturally readable from left to right by reversing this order and listing the source before the destination.

```nasm
movq    %rax, %rbx
```

This instruction in the AT\&T syntax copies (or "moves") the value from the `rax` register (left) to the `rbx` register (right), so as you would read from left to right. The same instruction in the Intel syntax would be:

```nasm
mov     %rbx, %rax 
```

### Inconveniences

While this "reading direction" applies to most instructions in the AT\&T syntax, there are some exceptions to it. For example the `sub` instruction:

```nasm
subq    %rax, %rbx
```

Intuitively, many would read this as something like $$\texttt{rax} - \texttt{rbx}$$. However, that is not the operation that is performed. The `sub` instruction subtracts the source operand from the destination operand. Thereby, this line actually computes $$\texttt{rbx} -\texttt{rax}$$ and stores the result in `rbx`.

Similarly, as the `cmp` instruction for comparing two values is nothing more than a subtraction that discards the result, the following lines might be equally confusing:

```nasm
cmpq    $1, %rax    # compare the value 1 to the contents of rax
jg      somelabel   # jg -> jump if greater than
```

Intuitively, this condition could be read as "jump if $$1>\texttt{rax}$$", however, here again, the reading order has to be reversed. This actual condition is "jump if $$\texttt{rax} > 1$$".

{% hint style="success" %}
For now, it is sufficient to know about these properties. However, once you have gained some experience in Assembly programming, you might want to investigate the reasons **why** the syntax of conditional jumps is so inconvenient in this syntax.
{% endhint %}

***

## Instruction Postfixes

Many instructions in AT\&T syntax need to be postfixed with a `b`, `w`, `l`, or `q` modifier. This postfix specifies the size of the operands, where

* `b` stands for "byte",
* `w` stands for "word" (2 bytes),
* `l` stands for "long" (4 bytes),
* and `q` stands for "quadword" (8 bytes).

As an example, take a look at the four different uses of the `mov` instruction:

```nasm
movb    $1, %al     # move value 0x01 to register al
movw    $1, %ax     # move value 0x0001 to register ax
movl    $1, %eax    # move value 0x00000001 to register eax
movq    $1, %rax    # move value 0x0000000000000001 to register rax
```

Even though you may "default" to using quadwords in most situations, always make sure to consider whether that is the right choice. Some assignments require different sizes to be used, and will only pass the tests if those sizes are respected.
