Assembly from scratch
x86 32-bit assembly on Linux, one instruction at a time — registers, system calls, jumps and flags, memory, the stack, functions, and calling C.
Every program you have ever run ends up as a stream of machine instructions, and assembly is the thinnest possible layer over them. This course writes tiny x86 programs that talk to Linux directly, with no C library in the way, and uses them to see what higher-level languages hide: where variables live, what a function call actually does, and why the stack is called a stack.
Every lesson has a live stepper that runs the repository’s program one instruction at a time, showing the registers, the flags, the bytes on the stack and anything printed. The steppers interpret the source exactly as written; the exit codes and output they reach match the real binaries.
Prerequisites
- Comfort with hexadecimal and binary.
- Some C helps for the last lesson, which calls
printffrom assembly and an assembly function from C.
Running the code
The programs are written for NASM and linked as 32-bit Linux executables:
cd assembly_from_scratch/32_bit_assembly
nasm -f elf32 01_intro.asm -o intro.o
ld -m elf_i386 intro.o -o intro
./intro
echo $? # the exit status: 13
Only the last lesson needs a 32-bit C library (gcc -m32); on Debian or Ubuntu that is the gcc-multilib package. Everything else runs with no libraries at all, on any x86-64 Linux kernel with 32-bit support.
The repository also has a 64_bit_assembly/ folder; its first file is still empty, so 64-bit assembly is coming soon.
The 32-bit registers used here
| Register | Role in these programs |
|---|---|
eax | system-call number in, return value out |
ebx, ecx, edx | system-call arguments (exit status, buffer, length) |
esp | stack pointer — the address of the top of the stack |
ebp | base pointer — a fixed anchor for one function’s stack frame |
eip | instruction pointer — the address of the next instruction |
Lessons
- 1 Registers and the exit system callFour instructions, no libraries — a program that asks the kernel to exit with a status of its choosing.
- 2 Hello, world and the data sectionBytes in the data section, a write system call, and the assembler computing a string's length for you.
- 3 Jumps, flags and loopsThere is no if and no while — only a comparison that sets flags, and a jump that reads them.
- 4 Memory and the stackWriting single bytes into the data section, then making room on the stack by moving esp.
- 5 Functions with call and retA function call is a push and a jump; a return is a pop and a jump. Building one by hand shows exactly that.
- 6 Arguments and return valuesPassing a value on the stack, finding it at [ebp + 8], and returning in eax — the cdecl calling convention.
- 7 Calling C, and being called from Cprintf from assembly, an assembly function from C, and what GCC's own call site reveals about the calling convention.