TExcelTAD
Home › x86-64 Assembly › Assembly Intro

Assembly Intro

Assembly language is the thinnest possible layer of text over the instructions a processor actually executes. Every line you write corresponds to (at most) one machine instruction: copy this value into that register, add two numbers, jump to another place in the program. There are no variables, no types, no if statements and no loops — only the building blocks from which compilers make all of those.

This course teaches x86-64 assembly (the instruction set of almost every desktop, laptop and server PC) using the NASM assembler on Linux. Every example runs for real: it is assembled with NASM 2.16, linked with ld and executed on a Linux machine through Compiler Explorer.

nasm · try it yourself
section .data
greeting:   db "Hello from x86-64 assembly!", 10   ; the text, then a newline (byte 10)
greet_len   equ $ - greeting                      ; its length, computed by NASM

section .text
global _start
_start:
    mov eax, 1              ; system call number 1 = write
    mov edi, 1              ; file descriptor 1 = standard output
    mov rsi, greeting       ; address of the bytes to write
    mov edx, greet_len      ; how many bytes
    syscall                 ; ask the Linux kernel to do it

    mov eax, 60             ; system call number 60 = exit
    xor edi, edi            ; exit status 0
    syscall
Hello from x86-64 assembly!

Don't worry about understanding every line yet: by the end of the Your First Program lesson you will. Notice already how much work a simple "print" takes: we put four values into four registers and then ask the operating system kernel to do the actual output.

From source code to machine code

A processor does not understand text. It reads machine code: sequences of bytes that encode instructions. Assembly is a human-readable spelling of those bytes:

Assembly (what you write) Machine code (what the CPU reads)
mov eax, 1 B8 01 00 00 00
xor edi, edi 31 FF
syscall 0F 05
ret C3

An assembler (here NASM) translates the text into these bytes. A linker (ld) then combines the result into an executable file the operating system can load. Compilers for C, Rust or Go do the same job at a much higher level: they translate your program into exactly this kind of instruction list.

Why learn assembly?

You probably won't write whole applications in assembly, but knowing it pays off in many ways:

  • Understanding performance. You can read what the compiler produced and see why one loop is faster than another.
  • Debugging. When a program crashes in optimized code, a debugger shows you instructions and registers, not source lines.
  • Security. Exploits, malware analysis and reverse engineering all happen at the machine level.
  • Systems work. Boot loaders, operating system kernels, context switches and some hot loops in libraries (memory copy, cryptography, video codecs) are still written in assembly.
  • Understanding computers. Pointers, the stack, calling conventions and integer overflow stop being mysterious once you have seen them up close.

A second taste: arithmetic

Here the program computes 7 * 6 in a register and prints the answer with a helper routine called print_uint, which converts a number into decimal digits.

nasm · try it yourself
section .text
global _start
_start:
    mov rax, 7              ; rax = 7
    imul rax, rax, 6        ; rax = rax * 6
    mov rdi, rax            ; print_uint prints the number in rdi
    call print_uint

    mov eax, 60             ; exit
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
42

Note: Assembly has no built-in way to print a number: the kernel only writes bytes. Many examples in this course end with the same print_uint helper. Copy it as it is for now; the Printing Numbers lesson takes it apart line by line.

What makes assembly different

High-level language Assembly
Named variables of any number A fixed set of 16 general-purpose registers, plus raw memory addresses
Types (int, string, bool) Only bytes; you decide how to interpret them
if, while, for Compare, then conditionally jump to a label
Functions with parameters call/ret plus a convention about which registers hold arguments
Error messages and exceptions A wrong address crashes the program with a signal

That last row is worth seeing once. Reading from address 0 is not allowed, so the kernel stops the program with a segmentation fault:

nasm · try it yourself
section .text
global _start
_start:
    mov rax, [0]            ; read 8 bytes from address 0: not mapped!
    mov eax, 60
    xor edi, edi
    syscall
Program terminated with signal SIGSEGV (11)

What you need

Nothing to install: every playground on this site runs remotely. To follow along on your own machine you need a 64-bit Linux system (or WSL on Windows) with nasm and binutils (ld, objdump) installed. The Toolchain lesson shows the commands.

Test yourself

What does an assembler do?

  1. Runs the program line by line
  2. Translates assembly text into machine-code bytes
  3. Converts C into assembly
  4. Loads the program into memory

Which instruction set does this course use?

  1. ARM64
  2. RISC-V
  3. x86-64
  4. MIPS

Why does a 'print' take several instructions in assembly?

  1. Because NASM is slow
  2. Because the program must place the call number, file descriptor, address and length in registers before asking the kernel with syscall
  3. Because printing is impossible
  4. Because each letter needs its own instruction