Skip to content

Registers

x86-64

RFLAGS 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

BitAbbreviationNameSet when
0CFCarry FlagUnsigned overflow / borrow; rotate shift-out bit
2PFParity FlagLow byte of result has even number of 1-bits
4AFAuxiliary CarryCarry out of bit 3 (BCD arithmetic)
6ZFZero FlagResult is zero
7SFSign FlagMSB of result is 1 (result is negative)
11OFOverflow FlagSigned arithmetic overflow

Control flag

BitAbbreviationNameMeaning
10DFDirection Flag0 = 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)

BitAbbreviationNameNotes
8TFTrap FlagSingle-step mode — raises #DB after each instruction
9IFInterrupt FlagEnables hardware interrupts (Ring 0 only)
16RFResume FlagSuppresses #DB on the instruction resuming after a debug exception
12-13IOPLI/O Privilege LevelRing allowed to use IN/OUT/CLI/STI without faulting
14NTNested TaskLegacy hardware task switching, which 64-bit mode doesn't support (IRET with NT=1 faults)
17VMVirtual-8086 ModeNot usable in long mode
18ACAlignment CheckWith CR0.AM set, faults on misaligned user-mode access; the kernel reuses it for SMAP (STAC/CLAC)
19VIFVirtual Interrupt FlagVirtual-8086 / PVI bookkeeping
20VIPVirtual Interrupt PendingSame
21IDID FlagCPUID support (writeable = CPUID available)

Instructions that read/write RFLAGS

InstructionEffect
PUSHFQ / POPFQPush/pop RFLAGS onto the stack
LAHFLoad SF/ZF/AF/PF/CF into AH
SAHFStore AH into SF/ZF/AF/PF/CF
SETccSet byte to 0 or 1 based on condition (e.g. setz al)
CLC / STC / CMCClear, 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, bFlagsUnsigned jumpSigned jump
a == bZF=1JE / JZJE / JZ
a != bZF=0JNEJNE
a < bCF=1 (unsigned), SF≠OF (signed)JB / JCJL
a >= bCF=0 / SF=OFJAE / JNCJGE
a > bCF=0 and ZF=0 / ZF=0 and SF=OFJAJG
a <= bCF=1 or ZF=1 / ZF=1 or SF≠OFJBEJLE

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, NOT and XCHG leave every flag alone. Compilers use LEA for arithmetic partly because it doesn't clobber flags between a CMP and its jump.
  • INC and DEC update ZF, SF, OF, AF and PF but not CF. A loop counting with dec ecx can keep a carry alive from earlier ADC arithmetic. That's deliberate.
  • AND, OR, XOR and TEST always clear CF and OF. xor eax, eax leaves ZF=1, PF=1, SF=0, CF=0 and OF=0 (AF is officially undefined), which is why it shows up right before SETcc sequences.
  • Shifts by a count of 0 change nothing. shl eax, cl with CL=0 leaves the previous flags in place, so you cannot assume a shift always defines them.
  • MUL/IMUL define only CF and OF (set when the high half of the result is significant). SF, ZF, AF and PF are undefined afterwards, and DIV/IDIV leave all six status flags undefined. Code that branches on ZF right after a MUL is 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 Jcc you will encounter.

  • After CMP a, b the state is: ZF=1 if equal; CF=1 if a < b (unsigned); SF≠OF if a < b (signed). This is worth memorising.

  • TF=1 causes 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 instruction

    If you see 0x100 being 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 jz to flip the branch without touching the binary, and in GDB set $eflags ^= 0x40 does the same thing. It is the fastest way to test "what happens if this check passes" before you commit to a patch.

  • LAHF/SAHF are used in performance-sensitive code to save/restore only the low flag byte without a full PUSHFQ.