Assembly Language Basics: Writing Code the CPU Understands
Learn x86-64 Linux assembly with the System V ABI: track registers, assemble and run a small program, debug it in GDB, then compare AArch64.
Learn x86-64 Linux assembly with the System V ABI: track registers, assemble and run a small program, debug it in GDB, then compare AArch64.
Assembly Language Basics: Writing Code the CPU Understands
Assembly lets you see how a program moves values through registers, calls functions, and returns results. The details depend on the processor and the calling convention, so this guide follows one path from start to finish: x86-64 Linux with the System V ABI and GNU assembler syntax.
Scope and tool versions
The main exercise uses Linux on x86-64, ELF binaries, GNU as (GAS) in AT&T syntax, GCC to link, and GDB to debug. It follows the System V AMD64 ABI; Windows x64 uses a different calling convention.
The GNU as 2.47 documentation and GDB 18.1 documentation were checked on 2026-11-05. Distributions may ship other versions. Check the tools installed on your machine with:
as --version
gcc --version
gdb --version
Registers and the calling convention
Registers are small storage locations inside the CPU. In x86-64, several names refer to overlapping parts of the same register:
| Name | Portion of RAX | Width |
|---|---|---|
| RAX | bits 63 through 0 | 64 bits |
| EAX | bits 31 through 0 | 32 bits |
| AX | bits 15 through 0 | 16 bits |
| AL | bits 7 through 0 | 8 bits |
These are views of one register, not four separate values. On x86-64, writing to EAX clears the upper half of RAX.
The System V ABI tells separately compiled functions where to find arguments and results. For integer and pointer arguments, the first six registers are RDI, RSI, RDX, RCX, R8, and R9. An integer result is returned in RAX; a 32-bit int uses EAX. RSP is the stack pointer and RIP is the instruction pointer.
Under this ABI, RBX, RBP, and R12 through R15 are callee-saved. RAX, RCX, RDX, RSI, RDI, and R8 through R11 are caller-saved. RSP must point back to the caller’s stack when the function returns. The example’s add_values function uses EAX only, so it has no callee-saved registers to restore.
GAS AT&T syntax prefixes register names with a percent sign and immediate constants with a dollar sign. Operands are written source first, destination second: movl $19, %edi loads 19 into EDI, while addl %esi, %eax adds ESI to EAX. The suffix l means a 32-bit operation.
Assemble, run, and debug a small program
Save the following as sum.s. It defines main, adds two integers in a separate function, then prints the result through the C library.
.section .rodata
.Lformat:
.asciz "sum = %d\n"
.text
.globl main
.type main, @function
main:
pushq %rbp
movq %rsp, %rbp
movl $19, %edi
movl $23, %esi
call add_values
movl %eax, %esi
leaq .Lformat(%rip), %rdi
xorl %eax, %eax
call printf@PLT
xorl %eax, %eax
popq %rbp
ret
.size main, .-main
.globl add_values
.type add_values, @function
add_values:
movl %edi, %eax
addl %esi, %eax
ret
.size add_values, .-add_values
.section .note.GNU-stack,"",@progbits
The instructions in main set up the two arguments in EDI and ESI before calling add_values. The callee returns the sum in EAX. Main then places the format string address in RDI and the sum in ESI for printf. Clearing EAX before printf sets AL to zero, as required for this variadic call when no vector arguments are passed.
The push and pop preserve RBP. At function entry, the stack is offset by eight bytes from a 16-byte boundary; pushing RBP aligns it before the calls, as the System V ABI requires.
Assemble, link, and run it:
as --64 -g -o sum.o sum.s
gcc -g -o sum sum.o
./sum
Expected output:
sum = 42
GCC acts as the link driver here. It adds the startup code and C library needed for main and printf. The assembler’s -g option records debugging information for the assembly source lines.
Now inspect the call in GDB:
gdb -q ./sum
(gdb) break add_values
(gdb) run
(gdb) info registers rdi rsi
(gdb) stepi
(gdb) stepi
(gdb) p/d $eax
(gdb) continue
(gdb) quit
At the breakpoint, RDI contains 19 and RSI contains 23. The first stepi copies EDI to EAX; the second adds ESI, so EAX is 42. Continuing lets printf display the result. GDB’s break, run, stepi, and info registers commands are useful when source-level stepping is not available or when you want to follow each machine instruction.
Short exercises
- Change the two immediate values in main to 7 and 35. The program should print sum = 42.
- Replace addl %esi, %eax with subl %esi, %eax. With the original inputs, the program should print sum = -4.
- Set a breakpoint at add_values and inspect the argument registers. Before either instruction runs, expect RDI = 19 and RSI = 23. After both instructions, expect EAX = 42.
A second ISA: AArch64
AArch64 is a separate instruction set with its own register names, instruction forms, and ABI. On Linux AArch64, functions use AAPCS64 rather than the x86-64 System V ABI.
| Concept | x86-64 System V | AArch64 / AAPCS64 |
|---|---|---|
| First integer arguments | RDI, RSI, RDX, RCX, R8, R9 | X0 through X7 |
| 32-bit view of first argument | EDI | W0 |
| Integer return value | RAX (EAX for int) | X0 (W0 for 32-bit values) |
| Add two 32-bit values | addl %esi, %eax | add w0, w0, w1 |
| Return from a function | ret | ret |
AArch64 uses X0 through X30 as 64-bit general-purpose registers. W0 through W30 name the lower 32 bits of the corresponding X registers; writing a W register clears the upper 32 bits. The AArch64 table is only a comparison. The assemble/run/debug exercise above stays on x86-64 throughout.
Further reading
- GNU assembler manual, Binutils 2.47 — syntax, registers, sections, and command-line options.
- GDB 18.1 manual — breakpoints, stepping, and register inspection.
- x86-64 System V ABI project — the calling convention used by the main exercise.
- Armv8-A Instruction Set Architecture — AArch64 register and instruction reference.
- Instruction Set Architecture — how an ISA defines the instructions assembly uses.
- Building a Simple CPU Simulator — a visual way to follow instructions through a processor.
Category
Related Posts
ASLR & Stack Protection
Explore ASLR, PIE, stack canaries, and NX on Linux with GCC, then inspect a harmless C program to see what each mitigation does and where it falls short.
Boolean Logic & Gates
Understanding AND, OR, NOT gates and how they combine into arithmetic logic units — the building blocks of every processor.
Boot Process
How firmware, bootloaders, kernels, and user space start an operating system, with Linux/x86 BIOS/MBR and UEFI paths plus practical troubleshooting.