x86-64 assembly from the ground up
The language the processor actually runs
- Beginner
- 12 chapters
- 197 min read
Learn x86-64 assembly by running it: registers and the mov instruction, the write and exit kernel calls, arithmetic with cdq and idiv, comparisons and the flags, loops with a counter, the stack with call and ret, memory addressing and lea, the four widths from db to dq, turning an integer into digits, the section layout with equ, and a capstone that sums a table and prints the total.
What You'll Learn
- Read and write a NASM file, and know what the processor does with each line
- Move values between registers and immediates, and know why one name has four widths
- Print bytes with the write call and end a program with the exit call
- Add, subtract, multiply and divide, with cdq or a cleared edx in front of a division
- Compare two values and branch on the flags, signed or unsigned
- Repeat work with a label and a jump back, keeping the counter where a syscall cannot reach it
- Use push and pop, call a function, return from it and give the caller its registers back
- Reach into memory with base, index and displacement, and compute addresses with lea
- Choose between db, dw, dd and dq, and widen a small load with movzx or movsx
- Turn an integer into decimal digits and print the text
- Lay out a file in text, data and reserved sections and name its constants with equ
- Build a capstone: a table, a loop, a division and one computed line of output
Prerequisites
- No assembly at all — the course starts at the first register
- Some experience with one programming language makes the early chapters quicker, but none is assumed
Course Content
- 01Your first programNot started
What the processor is doing while your code runs, what a register is, and your first program: four register values and two calls into the kernel.
- 02Registers and movNot started
The sixteen registers, why one of them has four names, and what mov really does: it copies a value without touching the place the value came from.
- 03Writing and exitingNot started
The two kernel calls every program here needs: write sends bytes out, and exit hands a status back. Also why a program that never exits does not simply stop.
- 04Arithmetic in registersNot started
Adding, subtracting, multiplying and dividing in registers, and the one instruction that surprises everybody: division reads two registers and writes two back.
- 05Comparisons and branchesNot started
cmp keeps the flags and throws the answer away, a jump reads one of those flags, and the pair is how a program chooses between two paths.
- 06Loops with a counterNot started
A loop is a label and a jump back to it, plus a counter that says when to stop. The chapter also covers the register the kernel quietly steals.
- 07The stack and functionsNot started
push and pop on the stack, call and ret as a pair, and a function that the program can use twice without writing the write call twice.
- 08Memory addressing and leaNot started
Brackets turn a number into a place, a base register plus an index reaches into a block of bytes, and lea computes an address without reading one.
- 09Bytes, words and quadwordsNot started
The four widths a value can have, the data directives that reserve them, and the size word that keeps the assembler from guessing what a store should write.
- 10Numbers into textNot started
A byte holding a digit is a character, not a number. Dividing by ten peels the digits off one at a time, and a buffer filled from the end turns an integer into a line of text.
- 11Sections and equNot started
What goes in each section and why, and how equ gives a number a name at assembly time so a program has no magic constants left in it.
- 12Capstone: summing an arrayNot started
A table of numbers, a loop that adds them, a division that splits the total into digits, and one write that prints the answer. Every earlier chapter has a line in it.
