Registers
x86-64RFLAGS Register
The 64-bit RFLAGS register holds status, control, and system flags that arithmetic and logical instructions set — conditional jumps and SETcc read these bits.
The flags register is the CPU's scratchpad of side effects. Arithmetic and logic
instructions write single bits into it (zero result, carry out, sign, overflow) and
every conditional jump, SETcc and CMOVcc reads them back. If you can't read the
flags, you can't read a branch.
Same register, three names. The 8086 had a 16-bit FLAGS. The 80386 widened it to
32-bit EFLAGS. x86-64 widened it again to 64-bit RFLAGS, but the upper 32 bits
are reserved and read as zero, so in practice RFLAGS and EFLAGS hold the same
bits. Tools mix the names freely: GDB calls it eflags even on a 64-bit process.
Reading the raw value: why you keep seeing 0x246
Bit 1 is reserved and always reads as 1, so the smallest value you will ever see is
0x2. A user-mode process with interrupts enabled always has IF (bit 9, 0x200)
set. Add ZF (0x40) and PF (0x4) after something like xor eax, eax and you get
0x246, which is why that number shows up in so many debugger screenshots and
crash dumps:
(gdb) info registers eflags
eflags 0x246 [ IF ZF PF ]When you see 0x202, the last result was non-zero. 0x286 adds SF: the last
result was negative. Decoding by hand gets fast once you know where the four
control-flow flags sit (bits 0, 6, 7 and 11).
Status flags
| Bit | Abbreviation | Name | Set when |
|---|---|---|---|
| 0 | CF | Carry Flag | Unsigned overflow / borrow; rotate shift-out bit |
| 2 | PF | Parity Flag | Low byte of result has even number of 1-bits |
| 4 | AF | Auxiliary Carry | Carry out of bit 3 (BCD arithmetic) |
| 6 | ZF | Zero Flag | Result is zero |
| 7 | SF | Sign Flag | MSB of result is 1 (result is negative) |
| 11 | OF | Overflow Flag | Signed arithmetic overflow |
Control flag
| Bit | Abbreviation | Name | Meaning |
|---|---|---|---|
| 10 | DF | Direction Flag | 0 = string ops auto-increment RSI/RDI; 1 = decrement |
Set with STD; cleared with CLD. Should be 0 on function entry per ABI.
System flags (user-mode relevance)
| Bit | Abbreviation | Name | Notes |
|---|---|---|---|
| 8 | TF | Trap Flag | Single-step mode — raises #DB after each instruction |
| 9 | IF | Interrupt Flag | Enables hardware interrupts (Ring 0 only) |
| 16 | RF | Resume Flag | Suppresses #DB on the instruction resuming after a debug exception |
| 12-13 | IOPL | I/O Privilege Level | Ring allowed to use IN/OUT/CLI/STI without faulting |
| 14 | NT | Nested Task | Legacy hardware task switching, which 64-bit mode doesn't support (IRET with NT=1 faults) |
| 17 | VM | Virtual-8086 Mode | Not usable in long mode |
| 18 | AC | Alignment Check | With CR0.AM set, faults on misaligned user-mode access; the kernel reuses it for SMAP (STAC/CLAC) |
| 19 | VIF | Virtual Interrupt Flag | Virtual-8086 / PVI bookkeeping |
| 20 | VIP | Virtual Interrupt Pending | Same |
| 21 | ID | ID Flag | CPUID support (writeable = CPUID available) |
Instructions that read/write RFLAGS
| Instruction | Effect |
|---|---|
PUSHFQ / POPFQ | Push/pop RFLAGS onto the stack |
LAHF | Load SF/ZF/AF/PF/CF into AH |
SAHF | Store AH into SF/ZF/AF/PF/CF |
SETcc | Set byte to 0 or 1 based on condition (e.g. setz al) |
CLC / STC / CMC | Clear, set or complement CF directly |
What CMP leaves behind, and which jump reads it
CMP a, b computes a - b, throws the result away and keeps only the flags. The
jump that follows decides whether the comparison was signed or unsigned, which is
the single most useful thing a flag check tells you when you're recovering types
from a binary: JB/JA means the compiler treated the values as unsigned,
JL/JG means signed.
After cmp a, b | Flags | Unsigned jump | Signed jump |
|---|---|---|---|
a == b | ZF=1 | JE / JZ | JE / JZ |
a != b | ZF=0 | JNE | JNE |
a < b | CF=1 (unsigned), SF≠OF (signed) | JB / JC | JL |
a >= b | CF=0 / SF=OF | JAE / JNC | JGE |
a > b | CF=0 and ZF=0 / ZF=0 and SF=OF | JA | JG |
a <= b | CF=1 or ZF=1 / ZF=1 or SF≠OF | JBE | JLE |
The full mnemonic list, including parity and the aliases, is on the
conditional jumps page. TEST a, b is the same
idea with AND instead of SUB: test eax, eax followed by jz is "if (eax == 0)".
Instructions that don't do what you'd expect to the flags
This is where hand-tracing goes wrong. A few rules worth memorising:
MOV,LEA,PUSH,POP,NOTandXCHGleave every flag alone. Compilers useLEAfor arithmetic partly because it doesn't clobber flags between aCMPand its jump.INCandDECupdate ZF, SF, OF, AF and PF but not CF. A loop counting withdec ecxcan keep a carry alive from earlierADCarithmetic. That's deliberate.AND,OR,XORandTESTalways clear CF and OF.xor eax, eaxleaves ZF=1, PF=1, SF=0, CF=0 and OF=0 (AF is officially undefined), which is why it shows up right beforeSETccsequences.- Shifts by a count of 0 change nothing.
shl eax, clwithCL=0leaves the previous flags in place, so you cannot assume a shift always defines them. MUL/IMULdefine only CF and OF (set when the high half of the result is significant). SF, ZF, AF and PF are undefined afterwards, andDIV/IDIVleave all six status flags undefined. Code that branches on ZF right after aMULis either a compiler bug or an obfuscator betting that you'll trust it.
Reverse-engineering notes
-
The four most important flags for control flow are ZF, CF, SF, OF — these drive every
Jccyou will encounter. -
After
CMP a, bthe state is: ZF=1 if equal; CF=1 ifa < b(unsigned); SF≠OF ifa < b(signed). This is worth memorising. -
TF=1causes a single-step exception after every instruction, which is how debuggers single-step. Malware turns that around: it sets TF itself and checks whether its own exception handler runs. Under a debugger the trap is usually consumed by the debugger, the handler never fires, and the sample knows it's being watched:asm pushfq or qword [rsp], 0x100 ; set TF (bit 8) popfq nop ; #DB fires after this instructionIf you see
0x100being OR'd into a pushed flags value, look for the handler next. More tricks like this are in anti-debugging techniques. -
Flags are a patching shortcut. In x64dbg you can double-click ZF in the register pane right before a
jzto flip the branch without touching the binary, and in GDBset $eflags ^= 0x40does the same thing. It is the fastest way to test "what happens if this check passes" before you commit to a patch. -
LAHF/SAHFare used in performance-sensitive code to save/restore only the low flag byte without a fullPUSHFQ.