Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / More programming languages

Workbook · Medium · 4 hoursFree account

Computer Architecture & Assembly

x86/x64 assembly, registers, calling conventions, and reading disassembly.

  • assembly
  • x86
  • x64
  • calling-conventions
  • disassembly
On this page 5 sections
  1. ·About This Workbook
  2. ·Chapter 1 — How a CPU Works
  3. Locked: Chapter 2 — Reading Assembly
  4. Locked: Chapter 3 — The Stack & Functions
  5. Locked: Practice Plan and Next Steps

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 -1
gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0
GNU objdump (GNU Binutils for Ubuntu) 2.46

Your 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:

  1. Fetch. Read the instruction at the address in the instruction pointer.
  2. Decode. Work out which operation it is and which operands it uses.
  3. Execute. Do the operation: change a register, read or write memory, or change the instruction pointer.
  4. 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:

RegisterUsual job
raxReturn value of a function; arithmetic
rbxGeneral use; a function must keep its value
rcx4th argument; loop counter in some instructions
rdx3rd argument; the high part of some results
rsi2nd argument; "source" of string operations
rdi1st argument; "destination" of string operations
rspStack pointer. Points to the top of the stack
rbpBase pointer. Often marks the start of a stack frame
r8 to r15r8 is the 5th argument and r9 the 6th; the rest are general

Special registers:

  • rip is the instruction pointer. It holds the address of the next instruction. You cannot mov to it directly. A jump, a call or a return changes it.
  • rflags holds 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 bits32 bits16 bits8 bits (low)
raxeaxaxal
rbxebxbxbl
rsiesisisil
r8r8dr8wr8b

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:

FlagNameIt is 1 when
ZFZeroThe result was zero (for a compare: the two values are equal)
SFSignThe result was negative (the top bit is 1)
CFCarryAn unsigned operation carried or borrowed
OFOverflowA 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:

  • .text holds the machine code.
  • .rodata holds 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: 20

The 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,0x12345678

b8 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

  1. Which register holds the return value of a function?
  2. Write the 8-bit, 16-bit and 32-bit names of rcx.
  3. After the instruction mov eax, 0xffffffff, what are the upper 32 bits of rax?
  4. Which register tells the CPU what to run next?
  5. A hex dump shows the bytes 0d 00 00 00 for a 32-bit integer. What number is it?
  6. cmp rax, rbx finishes with ZF = 1. What do you know about rax and rbx?
Answers
  1. rax. A function that returns a 32-bit value puts it in eax.
  2. cl, cx, ecx.
  3. Zero. A write to a 32-bit register clears the upper half.
  4. rip, the instruction pointer.
    1. Reverse the bytes: 0x0000000d, which is 13 in decimal.
  5. They are equal. A compare subtracts the values and sets ZF when the result is zero.

Sign in to continue

3 more sections are for signed-in members. It is free: sign in with Google.

Related