Practice library / More programming languages
Workbook · Medium · 4 hoursFree account
Computer Architecture & Assembly
x86/x64 assembly, registers, calling conventions, and reading disassembly.
On this page 5 sections
Exploit development begins where high-level code ends, so a reverser needs a working picture of the machine underneath. This workbook covers the x86-64 registers, the fetch-decode-execute cycle, the CPU flags and Intel syntax for instructions and memory references. It then moves to the control flow a disassembler shows — if, loops, function calls — and the System V calling convention that places arguments and return values in known registers. The final section matches a C source line to the assembly the compiler produced, so a reader can read backwards from a binary to the intent behind it. The ideas fit together because every later topic in exploit work is a specific move on this same board.
What You'll Learn
- Name the x86-64 registers and say what each one is for
- Explain the fetch-decode-execute cycle and how the CPU flags work
- Read Intel-syntax instructions, operands and memory references
- Recognise
if, loops and function calls in disassembly - Follow the stack, the System V calling convention and a stack frame
- Match a C source line to the assembly that the compiler made
About This Workbook
A binary is a file of machine code. The CPU runs it. A person reads it as assembly, a text form of the same code with one line for each machine instruction. Reverse engineers, malware analysts and exploit developers all read assembly. Defenders read it to answer one question: what does this program do?
This workbook teaches the x86-64 architecture (also called x64 or AMD64), the one in most laptops, servers and clouds. You do not write large programs. You learn to read small ones.
Every example comes from a small C program that you compile yourself. You read your own code, on your own lab VM. You do not need a target. When you move on to real samples, use only files that you may analyse: your own builds, training samples and public challenges. For malware, use the isolated lab in Malware Analysis. The next step after this workbook is Binary Exploitation Foundations.
Setup
You need a Linux VM (Kali, Debian or Ubuntu) with gcc and binutils. Run sudo apt install gcc binutils if a tool is missing. Make a work folder:
mkdir -p ~/asm && cd ~/asm
gcc --version | head -1
objdump --version | head -1gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0
GNU objdump (GNU Binutils for Ubuntu) 2.46Your versions can differ. The instructions can differ a little too, but the ideas stay the same.
Chapter 1 — How a CPU Works
Objectives
- Name the general-purpose registers and their smaller parts
- Describe the fetch-decode-execute cycle
- Explain what the instruction pointer and the flags register do
- Explain little-endian byte order
Explanation
Memory holds the code and the data of a running program. Each byte has an address, a number. The CPU is fast and memory is slow, so the CPU keeps the values it works on right now in registers: small, fast storage locations inside the chip.
The CPU runs one loop without end. This is the fetch-decode-execute cycle:
- Fetch. Read the instruction at the address in the instruction pointer.
- Decode. Work out which operation it is and which operands it uses.
- Execute. Do the operation: change a register, read or write memory, or change the instruction pointer.
- Move to the next instruction, and repeat.
The 16 general-purpose registers. Each one is 64 bits (8 bytes) wide. The names are old, and some have a usual job:
| Register | Usual job |
|---|---|
rax | Return value of a function; arithmetic |
rbx | General use; a function must keep its value |
rcx | 4th argument; loop counter in some instructions |
rdx | 3rd argument; the high part of some results |
rsi | 2nd argument; "source" of string operations |
rdi | 1st argument; "destination" of string operations |
rsp | Stack pointer. Points to the top of the stack |
rbp | Base pointer. Often marks the start of a stack frame |
r8 to r15 | r8 is the 5th argument and r9 the 6th; the rest are general |
Special registers:
ripis the instruction pointer. It holds the address of the next instruction. You cannotmovto it directly. A jump, a call or a return changes it.rflagsholds one-bit flags that record the result of the last arithmetic or compare instruction.
Smaller parts. Code can use the low part of a register. The name changes with the size:
| 64 bits | 32 bits | 16 bits | 8 bits (low) |
|---|---|---|---|
rax | eax | ax | al |
rbx | ebx | bx | bl |
rsi | esi | si | sil |
r8 | r8d | r8w | r8b |
A write to a 32-bit register such as eax sets the upper 32 bits of rax to zero. A write to ax or al does not. This is why compilers often use eax for a 32-bit int, even in 64-bit code.
Flags. Four flags matter most when you read code:
| Flag | Name | It is 1 when |
|---|---|---|
ZF | Zero | The result was zero (for a compare: the two values are equal) |
SF | Sign | The result was negative (the top bit is 1) |
CF | Carry | An unsigned operation carried or borrowed |
OF | Overflow | A signed operation overflowed |
You never see flags in the instruction text. A cmp sets them, and a later conditional jump reads them. Always look at the pair: the instruction that sets the flags and the instruction that uses them.
Byte order. x86 is little-endian: the least significant byte of a number is stored at the lowest address. The 32-bit number 0x12345678 sits in memory as the bytes 78 56 34 12. Debuggers and disassemblers show the number in the normal order. A hex dump of raw memory shows the bytes in reverse order, and you must reverse them in your head.
A program on disk. On Linux a program is an ELF file. It has sections. The two you need now:
.textholds the machine code..rodataholds read-only data, such as the text of string constants.
Worked example
Make the first program. It adds numbers in a function, and it has no pointers or arrays.
cat > sum.c <<'EOF'
long add3(long a, long b, long c) {
long t = a + b;
return t * c;
}
int main(void) {
return (int)add3(2, 3, 4);
}
EOF
gcc -O0 -fno-pie -no-pie -fno-stack-protector -fcf-protection=none \
-fno-asynchronous-unwind-tables -o sum sum.c
./sum; echo "exit code: $?"exit code: 20The program returns (2 + 3) * 4 = 20. The shell shows this in $?.
The flags remove compiler extras so that the output is short: -O0 turns off optimisation, -fno-pie -no-pie gives fixed addresses, and -fno-stack-protector -fcf-protection=none removes security code that you meet in Chapter 3. Always say which flags you used. A default build looks different.
Look at the file, and then at its header:
file sum
readelf -h sum | sed -n '2,3p;9,10p;11p'sum: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, ... not stripped
Class: ELF64
Data: 2's complement, little endian
Machine: Advanced Micro Devices X86-64
Type: EXEC (Executable file)The header confirms 64-bit code and little-endian data. "Not stripped" means that the symbol names, such as add3, are still in the file. A malware sample is often stripped, and then you see only addresses.
To see little-endian order for yourself, look at the raw bytes of one instruction. The next command shows the bytes of mov eax, 0x12345678:
printf 'int f(void){return 0x12345678;}\n' > le.c
gcc -O1 -c le.c -o le.o && objdump -d -M intel le.o | grep mov 0: b8 78 56 34 12 mov eax,0x12345678b8 is the opcode "move an immediate value into eax". The four bytes that follow are 78 56 34 12, which is the number in reverse order.
The raw bytes of an instruction are its machine code. You do not need to learn them. You need to know that the disassembler turns bytes into text, and that a different starting offset can give different text. This is a known trick against disassemblers, and it is why a tool can show wrong code for data.
Practice questions
- Which register holds the return value of a function?
- Write the 8-bit, 16-bit and 32-bit names of
rcx. - After the instruction
mov eax, 0xffffffff, what are the upper 32 bits ofrax? - Which register tells the CPU what to run next?
- A hex dump shows the bytes
0d 00 00 00for a 32-bit integer. What number is it? cmp rax, rbxfinishes withZF= 1. What do you know aboutraxandrbx?
Answers
rax. A function that returns a 32-bit value puts it ineax.cl,cx,ecx.- Zero. A write to a 32-bit register clears the upper half.
rip, the instruction pointer.- Reverse the bytes:
0x0000000d, which is 13 in decimal.
- Reverse the bytes:
- They are equal. A compare subtracts the values and sets
ZFwhen the result is zero.
Sign in to continue
3 more sections are for signed-in members. It is free: sign in with Google.