Chapter 8 of Mandiant/FLARE’s “Malware Analysis Crash Course” moves from 32-bit x86 to x86-64 — most modern malware/software on Windows is built for 64-bit, so this is essential groundwork for reading newer samples.
Primary x86-64 Registers
x86-64 (also known as AMD64/x64) extends x86 in 3 directions:
- Extends the existing 8 general-purpose registers to 64 bits, changing the prefix to
R:RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP - Adds 8 brand-new registers:
R8throughR15 - Extends the instruction pointer into
RIPand the flags register intoRFLAGS
Just like the sub-register mechanism in 32-bit, you can still access the lower portions: EAX/AX/AL for RAX. For the new registers R8-R15, the convention is a suffix: R8D (32-bit), R8W (16-bit), R8B (8-bit). There’s one small difference: RAX has AH (the high byte of AX), but the new registers have no equivalent high-byte form — only RAX-RDX (and the original registers) have AH/BH/CH/DH.
x86-64 Calling Convention: __fastcall
On Windows x64, Microsoft’s compiler uses __fastcall — quite different from STDCALL/CDECL back in Chapter 4. Only the first 4 integer parameters are passed via registers:
RCX— parameter 1RDX— parameter 2R8— parameter 3R9— parameter 4
Parameters 5 and beyond go on the stack, starting right at offset 0x20 from RSP (right after the 32-byte “home space” region, discussed below) — parameter 5 is at [RSP+0x20], parameter 6 at [RSP+0x28], and so on.
; gọi hàm addSixInts(1, 2, 3, 4, 5, 6)
sub rsp, 38h
mov dword ptr [rsp+28h], 6 ; tham số 6 -> stack
mov dword ptr [rsp+20h], 5 ; tham số 5 -> stack
mov r9d, 4 ; tham số 4 -> R9
mov r8d, 3 ; tham số 3 -> R8
mov edx, 2 ; tham số 2 -> RDX
mov ecx, 1 ; tham số 1 -> RCX
call addSixInts
add rsp, 38h
retx86-64 Stack Conventions and Home Space
The biggest difference from 32-bit: in x64, the entire stack frame is allocated once in the prologue (sub rsp, X) and stays fixed for the whole duration the function runs — no more constant PUSH/POP making RSP bob up and down like in 32-bit. Since RSP doesn’t change throughout the function, addressing relative to RSP becomes reliable, so RBP is no longer required to act as the frame pointer — it gets freed up as an ordinary general-purpose register.
That sub rsp, X region is used to hold:
- Non-volatile registers that need to be preserved (may be
PUSH’d right at the start of the prologue) - Local variables
- Room for the parameters of functions this function will go on to call (the compiler pre-calculates the largest size needed)
- Home space (also called shadow space/shadow store)
Home space is a fixed 32-byte region that the caller must always reserve, sitting right below the return address on the stack — below home space come the stack-passed parameters (5th onward). Its purpose: letting the callee “spill” (temporarily save to memory) the 4 register-passed parameters — ensuring a debugger can always find the parameter values even after the registers have been overwritten during the function’s execution (not needed in 32-bit, since parameters were always on the stack to begin with). In an optimized release build, a function may just reuse home space as general scratch space instead of actually spilling registers — at virtually no extra cost, since the stack adjustment only happens once, in the prologue.
Multi-byte NOPs
x86-64 still keeps the 1-byte NOP (0x90) but makes heavy use of multi-byte NOP forms to align code to 16-byte boundaries, optimizing cache performance (CPU cache lines are typically multiples of 16 bytes). A real example:
401000 75 0E jnz short loc_401010
401002 35 EF BE AD DE xor eax, 0DEADBEEFh
401007 05 CE FA ED FE add eax, 0FEEDFACEh
40100C 0F 1F 40 00 nop dword ptr [eax+00h] ; NOP 4-byte để căn chỉnh
401010 loc_401010:
401010 89 C2 mov edx, eaxThe 4-byte NOP at 0x40100C here serves exactly one purpose: pushing the next instruction (the target of a jump) to land at an address divisible by 16 (0x401010).
MOVSX, MOVSXD, and MOVZX
A group of instructions that extend smaller data into a larger register, appearing far more often in 64-bit than in 32-bit code, since 64-bit code frequently mixes 32-bit and 64-bit data types:
MOVSX/MOVSXD(Move with Sign-Extension / Sign-Extension Doubleword) — preserves the sign during extension, filling the high bits with the sign bit (MSB) of the original value.MOVZX(Move with Zero-Extension) — fills the high bits with all zeros, used for unsigned values.
mov ax, -5 ; ax = -5 (16-bit; FFFB)
movsx ebx, ax ; ebx = -5 (32-bit; FFFFFFFB) - giữ đúng dấu âm
movsxd rdx, ebx ; rdx = -5 (64-bit; FFFFFFFF`FFFFFFFB)For comparison: if you used a plain MOV to copy 0xFFFB (word) directly into RDX without extending, the result would be the positive value 65531 (00000000'0000FFFB) — completely wrong compared to the original value of -5.
mov ax, 0FFFBh ; ax = FFFB
movzx ebx, ax ; ebx = 0000FFFB (zero-extend, coi là unsigned)
movzx rdx, ax ; rdx = 00000000`0000FFFBNote: there is no MOVZXD — because in 64-bit mode, a MOV writing to a 32-bit register (e.g., EBX) already automatically zero-extends the upper 32 bits of the corresponding 64-bit register (RBX), so there’s no need for a dedicated instruction to do that.
x86-64 doesn’t change a program’s underlying logic, only how it’s represented at the low level. In the final chapter, I’ll write about the most practical part of reversing malware on Windows: the Windows API, data types, and common system call patterns.