Skip to main content
Be Bitwise

Reading Disassembly

Reverse Engineering Basics · Reverse Engineering track

5 min read

In this module

From Bytes to Instructions

A disassembler turns raw machine-code bytes back into human-readable assembly. In Assembly Language you wrote assembly and let the assembler shrink it into bytes. A disassembler runs that pipeline backwards. Tools like objdump, IDA Pro, Ghidra, and Binary Ninja eat a binary and spit out an assembly listing you can read and pick apart.

Function Prologues and Epilogues

The first pattern to memorise is the function prologue: the boilerplate sequence sitting at the top of nearly every compiled function. On x86-64 a typical prologue reads:

asm
push rbp ; Save caller's frame pointer
mov rbp, rsp ; Set up new frame pointer
sub rsp, 0x20 ; Allocate 32 bytes for local variables

The epilogue reverses this at the end of the function:

asm
mov rsp, rbp ; Deallocate locals
pop rbp ; Restore caller's frame pointer
ret ; Return to caller

Compilers usually fold the first two lines into a single leave instruction, so leave; ret is the form you'll see most often.

This wires straight into the stack frame model from The Stack & The Heap. The prologue builds a fresh frame, the epilogue tears it back down. The simulator below runs the same shape on our 8-bit teaching CPU, which has no rbp, so saving two registers stands in for saving the frame pointer. Press Space to step and keep an eye on SP: it drops by one for each PUSH and climbs back for each POP, and RET only lands back after the CALL because the epilogue left the stack exactly as it found it.

For the best simulation experience, use a desktop browser.

Assembly

200ms

Registers

R00
0x0000
R10
0x0000
R20
0x0000
R30
0x0000
R40
0x0000
R50
0x0000
R60
0x0000
R70
0x0000
PC0
0x0000
SP255
0x00FF
FLAGS0x00
ZCVN

ALU

Idle

Memory

PCSPWatched
Memory contents — rows indexed by base address, columns by hex offset
Address0123456789ABCDEF
0000000000000000000000000000000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

Pipeline

Cycle 0
Fetch
Decode
Execute
Writeback
→
→
→
No instruction loaded
Compiler Optimisations Change Patterns

Optimised code may omit the frame pointer entirely (using RSP-relative addressing instead), inline small functions, or rearrange instruction order. Release builds are usually compiled with -O2, so in real targets the tidy prologue above is the exception rather than the rule. Start with unoptimised debug builds when learning, then graduate to optimised release builds.

Calling Conventions

When one function calls another, both sides need to agree on where arguments and the return value live. On x86-64 Linux the System V ABI parks the first six integer arguments in RDI, RSI, RDX, RCX, R8, and R9, with RAX carrying the return value home. Spot a mov edi, 0x10 followed by a call? That's a first argument of 16 going in.

Windows x64 plays the same game with different rules: RCX, RDX, R8, R9 hold the first four arguments. Once you know the platform's convention, you can read function signatures straight off the disassembly.

Common Instruction Patterns

Read enough disassembly and certain patterns become instant tells:

  • xor eax, eax - zeroing a register (a shorter encoding than mov eax, 0, and CPUs recognise it as a zeroing idiom that doesn't depend on the old value)
  • test eax, eax followed by je - checking if a value is zero
  • lea rax, [rbx + rcx*4] - array indexing with element size 4
  • mov eax, [rbp - 0x8] - accessing a local variable on the stack

Here's a whole function so you can see the idioms in context. This is typical gcc -O0 output for a small array sum; hover over a line on either side to light up its partner:

sum() at -O0

x86-64
C Source
1int sum(int *arr, int n) {
2 int total = 0;
3 for (int i = 0; i < n; i++)
4 total += arr[i];
5 return total;
6}
Assembly
1sum:
2 push rbp
3 mov rbp, rsp
4 mov QWORD PTR [rbp-24], rdi
5 mov DWORD PTR [rbp-28], esi
6 mov DWORD PTR [rbp-4], 0
7 mov DWORD PTR [rbp-8], 0
8 jmp .L2
9.L3:
10 mov eax, DWORD PTR [rbp-8]
11 cdqe
12 lea rdx, [0+rax*4]
13 mov rax, QWORD PTR [rbp-24]
14 add rax, rdx
15 mov eax, DWORD PTR [rax]
16 add DWORD PTR [rbp-4], eax
17 add DWORD PTR [rbp-8], 1
18.L2:
19 mov eax, DWORD PTR [rbp-8]
20 cmp eax, DWORD PTR [rbp-28]
21 jl .L3
22 mov eax, DWORD PTR [rbp-4]
23 pop rbp
24 ret

Hover over code lines or mapping badges to see correspondences

A few things to spot. The two arguments arrive in rdi and esi and get spilled straight to the stack, which is what -O0 does with everything. There's no sub rsp: sum calls nothing, so it uses the 128-byte red zone below rsp instead of reserving space. The loop test lives at .L2, after the body, with a jmp into it on the first pass. And the epilogue is a bare pop rbp, because rsp never moved. Build the same function with -O2 and nearly all of it disappears: no frame pointer, no stack slots, just a pointer walking the array in a register.

Each idiom maps onto a higher-level concept. Array accesses, function calls, comparisons, loops: they all leave recognisable footprints in the instruction stream. Build up a mental library of those footprints and a wall of assembly stops being a wall.

Try it on a few. Each snippet below is a few lines from unoptimised x86-64 code; say what C it came from before you open the answer.

1. mov eax, [rbp-0x14] / cdqe / lea rdx, [rax*4] / mov rax, [rbp-0x20] / add rax, rdx / mov eax, [rax]

return arr[i]; where arr is an int * local at rbp-0x20 and i is an int at rbp-0x14. cdqe sign-extends the 32-bit index to 64 bits, *4 is sizeof(int), and the final load reads the element.

2. mov eax, [rbp-0x4] / cdq / idiv dword [rbp-0x8]

Signed division: a / b (quotient in eax) or a % b (remainder in edx). cdq sign-extends eax into edx because idiv divides the 64-bit pair edx:eax. Look at which register the next instruction uses to tell / from %.

3. cmp dword [rbp-0x4], 9 / jle .L3, sitting at the bottom of a block that .L3 starts

The condition of a loop such as for (i = 0; i < 10; i++). GCC tests i <= 9 rather than i < 10, and puts the test after the body with a backwards jump, so the loop's check appears at the end in the listing.

4. mov rdi, rax / call strlen / test rax, rax / je .L5

if (strlen(s) == 0) or something like if (!strlen(s)) goto .... The string goes in rdi (first argument), the result comes back in rax, and test/je branches when it's zero.