# W1041 Assembly Language

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

## Assembly Language

In the same manner in which all data must eventually be stored as a series of ones and zeroes in a digital computer, all code, no matter how complex, must eventually be stored as a series of very simple instructions. We call this machine code, and it looks like this (in hexadecimal):

```48 c7 c0 01 00 00 00
48 c7 c7 01 00 00 00
48 c7 c6 00 00 00 00
48 c7 c2 0c 00 00 00
0f 05
48 c7 c0 3c 00 00 00
48 31 ff
0f 05
```

Of course, from the comptuers perspective, all appears as a series of binary digits.

The above can be made (slightly) less cryptic by associating mnemonics, a short series of characters which is more easily interpreted by humans:

``` 0:   48 c7 c0 01 00 00 00    mov    rax,0x1
7:   48 c7 c7 01 00 00 00    mov    rdi,0x1
e:   48 c7 c6 00 00 00 00    mov    rsi,0x0
15:   48 c7 c2 0c 00 00 00    mov    rdx,0xc
1c:   0f 05                   syscall
1e:   48 c7 c0 3c 00 00 00    mov    rax,0x3c
25:   48 31 ff                xor    rdi,rdi
28:   0f 05                   syscall
```

Let's examine the above in a bit more detail. The extreme left-hand column is the address of the code. You'll note that each following line is equivalent to the previous line's address plus the number of bytes in that line. The next columns contain the machine code as we saw previously. Finally, on the right-hand side, we see a series of mnemonics, useful for us as humans. Even without a deep knowledge of asssembly, we can make some good educated guesses as to what each line does. Let's examine this code one last time, but this time with comments:

``` 0:   48 c7 c0 01 00 00 00    mov    rax,0x1  # Move  1 into the rax register
7:   48 c7 c7 01 00 00 00    mov    rdi,0x1  # Move  1 into the rdi register
e:   48 c7 c6 00 00 00 00    mov    rsi,0x0  # Move  0 into the rsi register
15:   48 c7 c2 0c 00 00 00    mov    rdx,0xc  # Move 12 into the rdx register
1c:   0f 05                   syscall         # Invoke a system call
1e:   48 c7 c0 3c 00 00 00    mov    rax,0x3c # Move 60 into the rax register
25:   48 31 ff                xor    rdi,rdi  # Zero the rdi register
28:   0f 05                   syscall         # Invoke a system call
```

The function of the above code is to print a message, but the message itself isn't displayed above; it's in a separate section and looks like this:

```48 65 6c 6c 6f 2c 20 57
6f 72 6c 64
```

## Exercises

Exercises
1.  M1041-10  Complete  Merlin Mission Manager  Mission M1041-10.