Skip to content
systemcoding.dev
Assembly

Understanding RFLAGS: Carry, Zero, Sign and Overflow Flags

How the x86-64 carry, zero, sign and overflow flags are set by add, sub and cmp, with worked 8-bit examples and the conditional jumps that read them.

5 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the Intel, Zig and C logos.

Every conditional branch on x86-64 is a two-step dance. One instruction does some arithmetic and leaves facts behind in the rflags register, and a later jcc instruction reads those facts. Nothing else carries the comparison between them, so if you understand four of the flags, CF, ZF, SF and OF, you understand why every if, while and for compiles the way it does. This post sets out what each one means, shows 8-bit worked examples you can check by hand, and maps them to the conditional jumps.

The flags register

rflags is a 64-bit register, but only a handful of its bits matter for everyday code. The arithmetic ones are:

FlagBitNameSet when
CF0Carryan unsigned operation carried out of, or borrowed into, the top bit
PF2Paritythe low byte of the result has an even number of 1 bits
AF4Adjusta carry out of bit 3 (used only by old decimal-adjust instructions)
ZF6Zerothe result is zero
SF7Signthe top bit of the result is 1
OF11Overflowa signed result does not fit in the destination

The Intel 64 and IA-32 Architectures Software Developer's Manual, volume 1, chapter 3, defines these as the "status flags". Other bits control behavior: DF (bit 10) is the direction flag for string instructions, and IF (bit 9) enables interrupts and cannot be changed by user code.

Two points to hold on to. First, only some instructions write flags: add, sub, cmp, and, or, xor, test, neg, shifts and a few others do, but mov, lea, push, pop and the jumps do not. Second, the CPU does not know whether your numbers are signed or unsigned. It sets CF as though they were unsigned and OF as though they were signed, every time, and it is your choice of jump that picks the interpretation.

Carry versus overflow with 8-bit examples

Eight bits keep the arithmetic small enough to do on paper. An 8-bit value is 0 to 255 unsigned, or -128 to 127 signed.

flags8.asmasm
        mov     al, 0xFF
        add     al, 1          ; case A

        mov     al, 0x7F
        add     al, 1          ; case B

        mov     al, 0x80
        add     al, 0x80       ; case C

        mov     al, 5
        sub     al, 9          ; case D

Work out each result and the four flags:

Text
case A: 0xFF + 0x01 = 0x100 -> al = 0x00   CF=1 ZF=1 SF=0 OF=0
case B: 0x7F + 0x01 = 0x80  -> al = 0x80   CF=0 ZF=0 SF=1 OF=1
case C: 0x80 + 0x80 = 0x100 -> al = 0x00   CF=1 ZF=1 SF=0 OF=1
case D: 0x05 - 0x09 = -4    -> al = 0xFC   CF=1 ZF=0 SF=1 OF=0

Read them one at a time.

Case A is an unsigned wrap. 255 plus 1 does not fit in 8 bits, so the result is 0 and CF records that a carry fell off the top. As a signed operation, -1 plus 1 is 0 and fits perfectly, so OF is 0. The same bits are an overflow in one view and fine in the other.

Case B is the reverse. 127 plus 1 is fine unsigned (128), so CF is 0. Signed, it is 127 plus 1 giving 128, which does not fit in -128 to 127, so OF is 1, and the stored result 0x80 has its top bit set so SF is 1 as well. Note that SF alone is not the sign of the true answer when OF is set: the stored result looks negative but the real sum is positive. This is exactly why the signed jumps combine SF and OF.

Case C adds -128 and -128 as signed values. The true result, -256, does not fit, so OF is 1. Unsigned, 128 plus 128 is 256, which carries, so CF is 1.

Case D subtracts. For sub, CF means a borrow: unsigned 5 minus 9 needs to borrow, so CF is 1. The signed result, -4, fits, so OF is 0, and the result 0xFC has its top bit set, so SF is 1.

The rule for OF in addition: it is set when both operands have the same sign and the result's sign differs. Whenever you are unsure, redo the sum in a wider width and ask whether it still fits.

How compare and test use flags

cmp a, b performs a - b, sets the flags exactly as sub would, and throws away the result. test a, b does a & b the same way. Their only purpose is to leave flags for the next instruction.

asm
        cmp     rdi, rsi       ; flags from rdi - rsi
        jb      .smaller       ; taken if rdi < rsi as unsigned numbers (CF=1)

That pair is the whole implementation of if (a < b) for unsigned values. For signed values you use jl instead, which reads SF and OF.

The conditional jumps

Each jcc is a fixed expression over the flags. The ones to memorize are in this table. Several mnemonics are synonyms for the same condition.

JumpMeaningCondition
je, jzequal, zeroZF=1
jne, jnznot equalZF=0
jb, jc, jnaebelow (unsigned <)CF=1
jae, jnc, jnbabove or equal (unsigned >=)CF=0
ja, jnbeabove (unsigned >)CF=0 and ZF=0
jbe, jnabelow or equal (unsigned <=)CF=1 or ZF=1
jl, jngeless (signed <)SF differs from OF
jge, jnlgreater or equal (signed >=)SF equals OF
jg, jnlegreater (signed >)ZF=0 and SF equals OF
jle, jngless or equal (signed <=)ZF=1 or SF differs from OF
js / jnsnegative / non-negativeSF=1 / SF=0
jo / jnooverflow / no overflowOF=1 / OF=0

The signed condition SF != OF is clever: it means "the sign bit of the stored result is wrong exactly when the overflow flag says so". Case B above, 127 + 1, has SF=1 and OF=1, which are equal, so jge is taken, and the true answer (128) is indeed non-negative.

Read the flags yourself

You can copy rflags into a register with pushfq and pop. This helps when you are checking your own hand calculation in a debugger:

readflags.asmasm
        mov     al, 0xFF
        add     al, 1
        pushfq                 ; push rflags on the stack
        pop     rax            ; rax = rflags
Text
rax = 0x257

Decode 0x257 in binary: bits 0 (CF), 1 (reserved, always 1), 2 (PF), 4 (AF), 6 (ZF) and 9 (IF). That matches case A: a carry, a zero result, even parity of 0x00, and a carry out of bit 3. The IF bit reads as 1 in a normal user-space process. Treat the exact upper bits as machine-dependent.

Flag pitfalls

  • **inc and dec do not touch CF.** They update the other flags only, so inc al on 0xFF gives ZF=1 but leaves the previous carry. This is the reason multi-precision loops use lea or dec to count while an adc chain carries.

  • **mov does not set flags.** A mov between cmp and jcc is fine, because it leaves the flags alone, but an add in between destroys them.

  • Shifts by zero leave flags unchanged, and shifts by more than one bit leave OF undefined. Do not branch on OF after a shift by cl.

  • A compiler may combine the test with the previous arithmetic. After sub rax, rdx, a following jz needs no test, because sub already set ZF.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.