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:
push rbp ; Save caller's frame pointermov rbp, rsp ; Set up new frame pointersub rsp, 0x20 ; Allocate 32 bytes for local variablesThe epilogue reverses this at the end of the function:
mov rsp, rbp ; Deallocate localspop rbp ; Restore caller's frame pointerret ; Return to callerCompilers 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
Registers
ALU
Memory
| Address | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 10 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 20 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 30 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 40 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 50 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 60 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 70 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 80 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| 90 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| A0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| B0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| C0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| D0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| E0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
| F0 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 |
Pipeline
Cycle 0Optimised 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 thanmov eax, 0, and CPUs recognise it as a zeroing idiom that doesn't depend on the old value)test eax, eaxfollowed byje- checking if a value is zerolea rax, [rbx + rcx*4]- array indexing with element size 4mov 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-64Hover 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.