Chapter 7 of Mandiant/FLARE’s “Malware Analysis Crash Course” is the longest chapter so far — it brings together a whole set of higher-level data/programming constructs (pointers, arrays, loops, structs, switches) and shows what they actually look like once compiled down to assembly.
Pointers and Memory
At the assembly level, a pointer is simply a value that represents a memory address, and it shows up in two forms:
- Pointer as a variable — use
LEAto compute an address and store it in a register/memory location (exactly like the&operator in C). - Pointer as a value — an integer that already sits in memory and happens to be used to access another memory location (dereferencing) — operationally the two are identical, they only differ in how they’re used.
A common pattern: LEA computes an address → MOV writes that address into another memory cell (the pointer variable) → the code later reads that cell back and dereferences it to get the actual value being pointed to.
mov eax, [ebp-4] ; EAX = giá trị con trỏ (một địa chỉ)
mov ecx, [eax] ; ECX = giá trị TẠI địa chỉ đó (dereference)MUL and IMUL
MUL src — unsigned multiplication, takes only a single explicit operand; the other operand is implicit, based on size:
| Size | Operation | Result |
|---|---|---|
| 8-bit | AL × r/m8 | → AX |
| 16-bit | AX × r/m16 | → DX:AX |
| 32-bit | EAX × r/m32 | → EDX:EAX |
The high part of the result goes in EDX (or DX/AH), the low part in EAX (or AX/AL). For example, 10 × 7 = 70 with a 32-bit multiply: EAX = 70, EDX = 0 (since the result doesn’t overflow past 32 bits).
IMUL — signed multiplication, comes in 3 forms:
imul ebx ; 1 toán hạng: EAX * EBX -> EDX:EAX (giống MUL nhưng có dấu)
imul eax, ebx ; 2 toán hạng: EAX = EAX * EBX (cắt bớt nếu tràn)
imul eax, ebx, 4 ; 3 toán hạng: EAX = EBX * 4 (cắt bớt nếu tràn)The 2- and 3-operand forms will truncate the result if it exceeds the destination size — this rarely causes problems since these forms are mainly used to compute offsets (typically small numbers).
Arrays
Arrays don’t exist as a distinct data type at the assembly level — they’re just a formula: element address = base + index × element_size. Telltale signs of an array in disassembly:
- Memory accesses using a base register plus an offset computed from an index
- A loop that accesses memory sequentially with an increasing index
Compilers often use SHL as a fast multiply instead of MUL/IMUL when the element size is a power of 2 (SHL by 1 = multiply by 2, by 2 = multiply by 4, and so on). Local arrays live on the stack, global arrays live in .data — both use the same indexing formula, differing only in the base address.
Loops
A C-style for loop has 4 components, which map onto assembly as follows:
- Initialization — runs exactly once, typically a
MOVsetting the counter variable to 0 - Condition — checked before each iteration:
CMPthe counter against the limit, followed by a conditional jump. Compilers often invert the logic: instead of “continue ifi < length”, they check “exit ifi >= length” (e.g., usingJNB— Jump if Not Below) - Body — the loop body, runs conditionally
- Update — runs after each pass through the body, typically
INCon the counter followed by a jump back to the Condition step
In IDA’s graph view, an arrow going backward from the Update block to the Condition block is the visual giveaway for spotting a loop.
String Instructions: MOVS, STOS, SCAS
A group of instructions for efficiently operating on byte/word/dword strings, always using ESI (source) and/or EDI (destination):
- MOVS (Move String) — copies from
[ESI]to[EDI]. The suffix sets the size:MOVSB(byte),MOVSW(word),MOVSD(dword). After each run, bothESIandEDIauto-increment by exactly the size just copied. - STOS (Store String) — writes the value in
EAX/AX/ALto[EDI].STOSB/STOSW/STOSDby size. WhenEAXis zeroed beforehand (xor eax, eax),STOSbecomes an efficient way to zero out a memory region.EDIauto-increments by the size written. - SCAS (Scan String) — compares
EAX/AX/ALagainst[EDI], only setting flags (nothing is modified).SCASBcomparesALagainst[EDI].EDIalso auto-increments after each comparison.
The Direction Flag (DF) controls whether ESI/EDI increment or decrement after each instruction: DF = 0 (the default, via CLD) means increment, DF = 1 (via STD) means decrement.
Instruction Repeat Prefixes
Turns the string instructions above into an automatic loop, using ECX as the counter:
REP— repeats whileECX > 0, auto-decrementingECXeach time, stopping whenECX == 0. Used withMOVS/STOS.REPE/REPZ— repeats while (ZF == 1ANDECX > 0). Used withSCAS/CMPS, stopping as soon as it hits a differing value or runs out of count.REPNE/REPNZ— repeats while (ZF == 0ANDECX > 0) — the opposite, stopping when it hits a matching value or runs out of count.
rep movsd ; copy ECX dword liên tục từ [ESI] sang [EDI]
xor eax, eax
rep stosb ; ghi 0 vào ECX byte liên tiếp tại [EDI] -> zero hóa buffer
xor al, al
repne scasb ; quét tìm byte 0x00 (NUL) trong chuỗi tại [EDI] -> tìm độ dài stringIntrinsic Functions
An intrinsic is a standard library function that the compiler “knows about” and replaces directly with an optimized instruction sequence right at the call site, instead of emitting an actual function call:
| C Function | Commonly inlined as |
|---|---|
memcpy() | rep movs* |
memset() | rep stos* (usually preceded by setting EAX/AL) |
strlen() | repne scasb (searching for the NUL byte) |
This is why source code can clearly call memcpy/memset/strlen, yet the disassembly shows no CALL to those functions at all — they’ve “vanished” into a rep mov/stos/scas sequence right at the call site.
Structs
Struct type information disappears entirely after compilation — a struct allocated on the stack looks no different from a bunch of independent local variables. To recognize a struct, look for:
- Allocation size — a
memset/STOSinstruction initializing one contiguous block of memory suggests that whole region belongs together - Access pattern — multiple instructions adding the same fixed offsets (0, 4, 8…) to a shared base, suggesting these are separate fields of one struct
In IDA: create a new struct via Local Types (Shift+F1) → right-click → Add type. Add fields with the D key (press repeatedly to cycle through byte/word/dword); for an array field, right-click and choose Array. Once defined, apply it to a variable via Edit → Struct var — field names will automatically replace the bare numeric offsets everywhere that struct is used.
Note on padding: the C compiler automatically inserts gaps to align fields — for example, a 4-byte int field always starts at an address divisible by 4, so if it’s preceded by a 1-byte field, a gap (padding) will appear between the two fields in memory.
Switch Statements and Jump Tables
The compiler picks one of two strategies depending on the number of cases:
- Few cases — generates a chain of
CMP+ conditional jump pairs, essentially identical to nested if/else. - Many consecutive cases — uses a jump table: an array holding the address of each case, with an indirect jump through it.
The jump table mechanism consists of 3 steps:
- Compute index = switch value minus the smallest case value (e.g., for cases from
'a'to'd', subtracting'a'gives an index of 0-3) - Bounds check — if the index exceeds the largest case value, jump to the
defaultbranch - Index into the address table:
jmp [jump_table + index*4]
cmp eax, 4 ; kiểm tra case nằm trong khoảng hợp lệ (0-4)
ja default_case
jmp [jump_table + eax*4] ; nhảy gián tiếp theo index
jump_table:
dd case_0, case_1, case_2, case_3, case_4IDA automatically labels jump tables of this form with a jpt_ prefix when it recognizes them. Recognizing the jmp [table + reg*4] pattern matters because a static control flow graph struggles to infer every possible jump target — IDA needs to correctly identify the jump table to render the switch’s full set of branches.
This chapter wraps up the “classic” 32-bit assembly material. In Chapter 8, I move on to x86-64 — an extended register set and a calling convention that’s completely different from 32-bit.