Skip to content
BetterDL

Zero flag (Z)

Also called: Z flag, ZF

A 1-bit status register that is 1 when the last result loaded into the accumulator was zero. Conditional jumps test it.

The zero flag is how a program makes decisions. It's a 1-bit register that records whether the last result was zero:

  • Z = 1 if the value just loaded into the ACC is 0;
  • Z = 0 otherwise.

In Tiny8, Z loads at the same edge as the ACC, and only then: on LOAD, ADD and SUB. STORE, JMP, JZ and HALT leave both the ACC and Z unchanged. At reset ACC = 0, so Z = 1.

A conditional branch reads it. JZ a jumps to a only when Z = 1. Together, SUB and JZ give a comparison: subtract two values, and if the result is 0 they were equal.

Z is one of the status flags. Real CPUs have the same flag (ARM's Z, x86's ZF) alongside others for carry, sign and overflow.

Watch out for wrap-around: in 8-bit arithmetic, 128 + 128 = 256 wraps to 0, so Z = 1 even though neither number was 0.

Worked example

Example

Watching Z

Tiny8 runs four instructions in a row. ACC starts at 5, and M[C] = 5, M[D] = 7.

  1. 1.

    SUB C: ACC = 5 − 5 = 0, so Z = 1.

  2. 2.

    STORE E: M[E] = 0. STORE doesn't touch Z, so Z stays 1.

  3. 3.

    LOAD D: ACC = 7, so Z = 0.

  4. 4.

    ADD D: ACC = 7 + 7 = 14, Z = 0.

Common mistakes

  • Thinking STORE sets Z from the stored value. Only LOAD, ADD and SUB change Z.

  • Reading Z = 1 as 'the result was 1'. It means the result was zero.

  • Forgetting that an 8-bit sum can wrap to 0 and set Z.

Practice Zero flag (Z)

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Zero flag (Z) is taught in Basic CPU / Computer Architecture and Adders and ALUs.