digiwleeaLearn
Open the lab →

The zero flag

One bit that says 'the result was zero', and why it matters

4 min read

The zero flag is a one-bit status signal a processor sets when an operation's result is all zeros, computed by ORing every result bit together and inverting, so later instructions can branch on whether a value reached zero.

Build it in the lab →
For a program to make a decision, the hardware has to hand it something to decide on. That something is a status flag: a single bit the ALU sets as a side effect of an operation, reporting a fact about the result. The most useful flag by far is the zero flag, high exactly when the result was 0.

How the hardware knows a byte is zero

A byte is zero only when every bit is 0. So the detector is a wide OR: feed all eight result bits into an OR tree, and its output is high the instant any bit is 1, that is, whenever the value is not zero. One NOT on that output flips it, and you have Z, high only when all eight bits are 0. It is a small, purely combinational circuit hanging off the result bus.
Z
= \overline{r_0 \lor r_1 \lor r_2 \lor r_3 \lor r_4 \lor r_5 \lor r_6 \lor r_7}
That is a NOR of all the bits, built as an OR tree plus an inverter so it stays shallow. The flag is computed every cycle, but a program only reads it when it wants to: a conditional jump like JZ (jump if zero) looks at Z and branches only when it is set.

Why zero is the flag you reach for

Zero is special because so many questions reduce to it. Is a counter finished? Count down and test whether it hit 0. Are two numbers equal? Subtract them and test whether the difference is 0. Did a loop run out of items? Decrement the count and test for 0. A single zero flag plus a conditional jump covers looping, comparison, and equality, which is why even the tiniest instruction sets include it.
Common mistakes. A flag reflects the LAST operation that set it, not the current value in a register, so reading Z after an unrelated instruction tells you nothing useful, test it right after the operation you care about. Real CPUs carry more flags than zero: a carry flag (the adder's carry-out, for unsigned overflow) and a negative/sign flag (the top result bit) are the usual companions. Each answers a different question; do not use the zero flag to detect overflow or sign.
The zero flag is the bridge from arithmetic to control flow: the ALU computes a result, the flag summarizes one fact about it, and a conditional jump acts on that fact. Without a flag the machine can compute but never decide. This one wire is what lets the program counter do something other than march straight ahead.
Try it
A program wants to test whether the accumulator equals 5. The machine has no 'compare' instruction, only SUB and the zero flag. How does it check?

Frequently asked

What is the zero flag in a CPU?

The zero flag is a one-bit status signal that a processor sets when the result of an operation is all zeros. It is computed by ORing every result bit and inverting (a NOR of the bits), and conditional jump instructions read it to decide whether to branch.

How is the zero flag calculated?

By NORing all the result bits: an OR tree over every bit is high whenever any bit is 1 (the value is nonzero), and one inverter flips that to a signal that is high only when all bits are 0. It is a small combinational circuit on the ALU's result bus.

Why is the zero flag so important?

Because many decisions reduce to testing for zero: loop counters finishing, two numbers being equal (subtract and test), or a value running out. A single zero flag plus a conditional jump gives loops, comparisons, and equality tests, so it is the minimum a machine needs to make decisions.

You've got the theory. Now build it from scratch and watch it work.

Build it in the lab →Or test yourself: the free placement exam takes 15 minutes →
Builds towardBranching and loops
Found this useful? Share itShare on X
Written by Taceddin SancakCreator of digiwleea. These lessons and the switch-level simulator every diagram runs on grew out of one goal: understand computers from the transistor up by building one.