I’m starting a structured relearn of x86 assembly using FLARE’s (Mandiant) free “Malware Analysis Crash Course” material. This series captures the theory notes I took while reading through it.
Assembly, Disassembly, and Decompilation
Three concepts that are easy to conflate:
- Assembly — the human-readable form of machine code, where each line maps almost 1:1 to a single CPU instruction. Every architecture (x86, ARM, MIPS) has its own assembly syntax, but the underlying principles are the same.
- Disassembly — the reverse process: a disassembler (IDA Pro, Ghidra, etc.) parses raw binary machine code and reconstructs it into assembly, complete with mnemonics and operands.
- Decompilation — goes a step further: it tries to turn assembly back into higher-level source code (C/C++). Unlike disassembly, decompilation can never perfectly recover the original source — variable names, comments, and type definitions are permanently lost at compile time, and compiler optimizations further distort the original logic. A decompiler only produces a “best-effort” approximation, but it’s extremely useful for reasoning at a higher level of abstraction instead of reading assembly line by line.
The full pipeline (and its reverse when doing RE):
Compiling: Source Code (.c) → Compiler → .ASM (intermediate) → Assembler → .OBJ → Linker → .EXE Binary
Reversing: .EXE Binary → Disassembler → Assembly Listing → Decompiler → Pseudo-Source Code (approximate)Data Types
x86 works with fixed-size data units that map to Windows API and C types:
| Name | Size | Windows API | Equivalent C type | Declaration directive |
|---|---|---|---|---|
| Byte | 8 bit | BYTE | unsigned char | db |
| Word | 16 bit | WORD | unsigned short | dw |
| Dword | 32 bit | DWORD | unsigned long | dd |
| Qword | 64 bit | QWORD | unsigned long long | dq |
x86 Registers
General-Purpose Registers
Eight 32-bit registers: EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP. Most instructions can use these registers interchangeably, but there are informal conventions (e.g. ESP is always reserved for stack management, EAX typically holds a function’s return value).
Sub-registers — a register that can be “split”
The first four registers (EAX, EBX, ECX, EDX) can be accessed as smaller sub-parts:
EAX (32-bit)
└── AX (16-bit, lower half of EAX)
├── AH (upper 8 bits of AX)
└── AL (lower 8 bits of AX)Modifying AL changes only the lowest byte of EAX, leaving the rest untouched. Note: only the first four registers split into H/L this way — ESI, EDI, EBP, ESP only split down to 16-bit (SI, DI, BP, SP), with no separate 8-bit high/low forms.
EFLAGS and EIP
- EFLAGS — a 32-bit register holding status/control flags, where each bit reflects the result of the most recent arithmetic/logic operation or a CPU state — these flags drive conditional branching later on.
- EIP (Instruction Pointer) — holds the memory address of the next instruction to execute. Unlike general-purpose registers, EIP cannot be modified directly — it’s only updated implicitly by the CPU during sequential instruction fetch, or explicitly by flow-control instructions (jump, call, return).
Instruction Basics
Opcode, Mnemonic, Operand — the three parts of an instruction
- Opcode — the binary sequence that encodes the operation (add, move data, call a function, etc.)
- Mnemonic — the readable short name for that opcode (e.g.
mov,add) - Operand — the instruction’s parameters, serving as the source and/or destination data. The number of operands varies by instruction, typically from 0 to 3.
A concrete example: the byte sequence B8 41 00 00 00 translates to mov eax, 41h. The first byte, B8, is the opcode meaning “move an immediate constant into EAX”; the remaining 4 bytes encode that constant. Because x86 stores numbers in little-endian, the value 00000041h actually sits in memory as 41 00 00 00 (least-significant byte first).
Intel syntax vs AT&T syntax
This material (and most of IDA/Ghidra by default) uses Intel syntax — destination operand first, source second (mov destination, source). AT&T syntax (common on gdb/Linux) reverses this (source first, destination second) — the same instruction read in two different directions, so you need to know which syntax you’re looking at to avoid misreading the direction data moves.
The Three Operand Types
| Type | Description | Example |
|---|---|---|
| Immediate | The value is the operand itself | 8, 10h, -123, 'A' |
| Register | The value lives in a register | eax, si, ecx, esp |
| Memory | The operand references a memory address | [00020001], [ebp+08h], [eax+esi] |
One important constraint: MOV cannot move directly from memory to memory — it must always go through an intermediate register.
MOV
MOV destination, source — copies the value from source to destination without changing the source value. This is the most common instruction you’ll see in any disassembly, since most program logic revolves around moving data around.
mov eax, 0FFh ; EAX = 0xFF
mov ebx, eax ; EBX = EAX (EAX unchanged)NOP
NOP — does nothing, simply moves to the next instruction. Functionally equivalent to xchg eax, eax (swapping a register with itself). It encodes to just 1 byte (0x90), which makes it handy for overwriting unwanted instructions or padding gaps while patching/debugging.
Hacking Assembly with IDA
IDA lets you write/modify assembly directly inside a running debug database. The basic workflow: open the IDA database, hit Continue (F9) to run, then use the 4 main panels:
- Disassembly — view instructions in linear or graph form
- Registers — view/edit register values
- Memory — view/edit memory as hex
- Stack — observe stack state
To “write” an assembly instruction yourself: select the target address → Edit → Patch Program → Assemble → type the instruction you want to insert. Commonly used debug keys: Step Over (F8) runs one instruction without stepping into a called function, Step Into (F7) steps directly into the called function, Add Breakpoint (F2), and Set IP (right-click) to force EIP to jump to a different address (this is exactly the “forcing execution” technique covered in Chapter 6).
Arithmetic Instructions
ADD (add), SUB (subtract), INC (+1), DEC (-1) — the destination must be a register or memory, never an immediate. All four instructions update EFLAGS after executing.
Bitwise Instructions
Seven bit-level instructions: XOR, AND, OR (logical operations on two operands), SHL/SHR (shift bits left/right, with the vacated bits filled with 0 — bits shifted out are lost), and ROL/ROR (rotate bits left/right — bits pushed out one end wrap around to the other end instead of being lost). Clearly distinguishing shift (data lost) from rotate (no data lost) is a common point of confusion.
Accessing Memory
The CPU must always know how many bytes to read/write on each memory access — determined in one of two ways:
- Implicit inference — if one operand is a register, that register’s size determines the access size (
mov [402000h], ebxneeds no explicit size since EBX is already 4 bytes). - Explicit declaration — using a size directive:
byte ptr,word ptr,dword ptrwhen no register implies a size (e.g.mov dword ptr [402000h], 0).
mov eax, [402000h] ; reads 4 bytes (since destination is EAX)
mov ax, [402000h] ; reads only 2 bytes (since destination is AX)
mov dword ptr [402000h], 0 ; must be stated explicitly since the source is an immediate, size can't be inferredMemory Operands — computing the address
The effective address is computed from up to 3 components:
Effective Address = Base + Index × Scale + Displacement- Base — the register holding the base address
- Index × Scale — an index register multiplied by a scale factor (1, 2, 4, or 8) — matching the exact size of one array element
- Displacement — a fixed constant offset (can be negative)
[ebp+8] ; base = EBP, displacement = 8
[esp+ebx*4+8] ; base = ESP, index = EBX, scale = 4, displacement = 8The index*scale pattern is exactly why this formula is so closely tied to array traversal (covered in more detail in Chapter 7).
LEA (Load Effective Address)
LEA destination, [expression] — unlike other instructions, it never reads memory; it simply computes the value of the bracketed expression as a pure calculation and writes the result (an address, or just a plain number) into the destination. The brackets here are purely notational, not a real memory access.
Two main uses:
- Computing offsets — effectively equivalent to the
&(address-of) operator in C - Compiler optimization — replacing an entire chain of add/multiply instructions with a single
LEA
lea eax, [ebx+8] ; EAX = EBX + 8 (no memory read)
lea eax, [ebx*4+5] ; EAX = EBX*4 + 5
lea eax, [ecx+ebx*4+5] ; EAX = ECX + EBX*4 + 5When you spot LEA in a disassembly, don’t immediately assume “this is taking the address of some variable” — very often it’s simply an ADD/MUL that the compiler expressed differently.
This is the mandatory foundation before reading any disassembly. Next chapter covers the Stack — a concept just as fundamental.