Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 40 ยท Core Instructions

Flags: ZF, SF, CF, OF and How They Are Set

Understand the x86 status flags ZF, SF, CF and OF, how cmp works as a hidden sub, signed vs unsigned conditions, overflow detection, and a flag-driven toy CPU.

An x86 processor has no instruction that says "compare these two numbers and tell me which is larger". It has something more primitive and more flexible: nearly every arithmetic and logic instruction leaves a small summary of its result in a handful of single-bit flags, and a later instruction (a conditional jump, cmov or setcc) reads those bits. All the if, while and for statements you write in higher-level languages eventually boil down to this two-step dance: set flags, then act on them.

Understanding the four main flags (ZF, SF, CF, OF) and how they combine is what turns the long list of jump mnemonics (jl, jb, jg, ja...) from a memorisation exercise into something you can derive. This lesson is the theory behind Compare and Jump, the chapter on control flow that follows.

The four flags that matter

The flags live in the rflags register, introduced in The x86-64 Register Set.

Flag Name Set to 1 when...
ZF Zero the result is 0
SF Sign the top bit of the result is 1 (negative if read as signed)
CF Carry an unsigned operation did not fit: a carry out of (add) or borrow into (sub) the top bit
OF Overflow a signed operation did not fit: the true result is outside the signed range

There are two more you will occasionally meet. PF (parity) records whether the low byte of the result has an even number of 1 bits, and AF (auxiliary carry) tracks a carry out of bit 3 for decimal arithmetic; both are rarely used in new code.

Two things deserve emphasis. First, flags are side effects: add, sub, and, or, xor, cmp, test, neg and shifts all set them, while mov, lea, push, pop and jmp do not touch them. Second, flags are overwritten by the next instruction that sets them, so a conditional jump normally sits immediately after the instruction whose result it tests.

cmp is a subtraction in disguise

cmp a, b performs a - b, updates all the flags exactly as sub would, and then throws the result away. Neither operand changes. Afterwards the flags encode the whole relationship between a and b:

  • a == b gives a result of zero, so ZF = 1.
  • If a < b as unsigned numbers, the subtraction needed a borrow, so CF = 1.
  • If a < b as signed numbers, then the "true" signed result is negative, which usually shows as SF = 1; but when the subtraction overflows (the true result does not fit), the sign bit lies, and OF = 1 corrects for it. The signed less-than test is SF โ‰  OF.

That last sentence is the heart of signed comparison. If there was no overflow, SF is right and tells you the sign; if there was overflow, SF is wrong and OF = 1 flips the answer. For example in 8 bits, cmp 127, -1 computes 127 - (-1) = 128, which does not fit (OF=1), and wraps to 0x80, which looks negative (SF=1). SF = OF, so the condition "less" is false: indeed 127 is not less than -1.

flowchart LR
  A["cmp a, b"] --> S["compute a - b"]
  S --> F["set ZF, SF, CF, OF, PF, AF"]
  S --> D["discard the result"]
  F --> J["jcc / cmovcc / setcc read the flags"]

cmp does the same work as sub, but keeps only the flags.

Conditions: signed vs unsigned

Each conditional jump tests a particular combination of flags. The vocabulary has a clear convention: "above" and "below" are for unsigned numbers, "greater" and "less" are for signed numbers.

Meaning Unsigned (after cmp a, b) Flags tested Signed Flags tested
a == b je / jz ZF=1 je / jz ZF=1
a != b jne / jnz ZF=0 jne / jnz ZF=0
a < b jb / jc CF=1 jl SF != OF
a <= b jbe CF=1 or ZF=1 jle ZF=1 or SF != OF
a > b ja CF=0 and ZF=0 jg ZF=0 and SF = OF
a >= b jae / jnc CF=0 jge SF = OF

Equality does not depend on signedness, so je and jne are the same either way. The others differ, and the compiler chooses by looking at the C type: comparing two ints emits jl/jg, comparing two unsigned values emits jb/ja. Pointers are compared unsigned. This is the instruction-level reason that mixing signed and unsigned comparison in C gives surprising results, and that unsigned i; i >= 0 is always true.

There are also single-flag jumps: js / jns (sign), jo / jno (overflow), jp / jnp (parity), and jc / jnc (carry).

Let us check the table by brute force. The toy below implements cmp on 8-bit values and lists every condition that is true for several pairs.

JS Which conditions hold after cmp a, b?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the row for cmp 255, 1. Unsigned, 255 is above 1 (ja, jae, jne). Signed, the same bits are -1 compared with 1, so the signed conditions say less (jl, jle). Same flags, two different answers, depending on which jump you choose. Row cmp 128, 127 shows overflow: 128 is -128 signed, so it is less than 127, but the subtraction -128 - 127 = -255 overflows, SF=0 and OF=1, and SF != OF still produces the right answer.

Detecting overflow

Because the CPU sets both CF and OF, you can detect either kind of overflow with the right flag:

  • Unsigned overflow after add: jc (carry set). After sub: jc also means a borrow, i.e. a < b.
  • Signed overflow: jo. The rule behind OF for addition: it is 1 exactly when both operands have the same sign and the result has a different sign. For subtraction: operands of different signs and the result sign differs from the first operand.

C compilers use these flags to implement __builtin_add_overflow and checked arithmetic in languages like Rust, which is why overflow checks cost only a branch.

JS Detecting carry and overflow after add

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the same addition can overflow one way but not the other: 200 + 100 carries (300 does not fit in 8 bits unsigned) while as signed numbers -56 + 100 = 44 is fine; 100 + 100 is the opposite. Neither flag is "the" error flag. Which one matters depends on what the bits mean in your program.

A flag-driven toy CPU

Now let us put flags to work. The interpreter below runs a small program with labels. It supports mov, add, sub, cmp, jmp and the conditional jumps from the table, on 32-bit values, with ZF, SF, CF and OF updated by add, sub and cmp. We run two programs: a countdown loop that adds 5 + 4 + 3 + 2 + 1, and a "maximum" that picks the larger of two numbers once with signed jumps and once with unsigned jumps.

JS Flags in action: a loop and signed vs unsigned maximum

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first program is the standard "decrement and loop while not zero" shape you will write many times in Writing Loops. The second program shows the signed/unsigned divide in action. The bit pattern for -5 is 0xFFFFFFFB, which is a huge unsigned number, so the unsigned maximum picks it (printed here reinterpreted as signed, -5), while the signed version correctly picks 3.

Common mistakes

  • Using ja/jb for signed data (or jg/jl for unsigned). The program works for positive numbers and fails once a value has its top bit set.
  • Inserting an instruction between cmp and the jump that clobbers flags (anything arithmetic). mov and lea are safe; add and inc are not.
  • Assuming inc/dec set CF. They do not.
  • Testing flags after mul, div or shifts by a variable count without checking which flags are defined.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does cmp eax, ebx do to eax?

    cmp is a subtraction whose result is discarded; only the flags change.
  2. 2. Which condition means 'signed less than' after cmp a, b?

    SF != OF is the signed less-than test (jl). CF = 1 is the unsigned test (jb). Looking at SF alone fails when the subtraction overflows.
  3. 3. After cmp, the bytes 0xFF and 0x01 are compared. Which jumps are taken for a=0xFF, b=0x01?

    The flags are the same either way; the jump you choose decides whether the bits are read as unsigned (255 is above 1) or signed (-1 is less than 1).
  4. 4. Which flag reports that an unsigned addition produced a carry out of the top bit?

    CF is the unsigned carry/borrow flag. OF is for signed overflow.
  5. 5. Which instruction between cmp and its conditional jump would break the jump's logic?

    add overwrites the flags set by cmp. mov and lea do not touch flags, so they are safe in between.

Summary

  • Arithmetic and logic instructions set flags; mov, lea and jumps do not.
  • ZF means result zero, SF is the result's top bit, CF is unsigned carry/borrow, OF is signed overflow.
  • cmp a, b is sub without storing the result, and test a, b is and without storing it.
  • "Above/below" jumps use CF (and ZF) for unsigned data; "greater/less" jumps use SF, OF and ZF for signed data; je and jne work for both.
  • Signed less-than is SF != OF because OF corrects the sign bit when the subtraction overflows.
  • The same bit pattern can be above one number and less than it at the same time, depending on the jump you choose.

Next: Compare and Jump, where flags drive real control flow.