Skip to main content
Be Bitwise

Understanding CPU Flags

Run several comparisons and watch how the CPU flags change. Equal values set the zero flag, smaller-than sets the negative flag - flags are how the CPU remembers comparison results.

The FLAGS register is a set of single-bit indicators that the ALU updates after arithmetic and comparison operations. The zero flag (Z) is set when the result is 0, the negative flag (N) is set when the result is negative, and the carry flag (C) is set on overflow. Conditional jump instructions (JZ, JNZ) read these flags to decide whether to branch. Flags are the bridge between arithmetic and control flow.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Compare equal values

    Do this: Step through MOV R0, #5 / MOV R1, #5 / CMP R0, R1

    Watch for: FLAGS: zero flag (Z) is set

    CMP subtracts R1 from R0 internally: 5 - 5 = 0. Because the result is zero, the zero flag is set. This is how the CPU encodes 'these values are equal'.

  2. 2

    Compare smaller < larger

    Do this: Step through MOV R2, #3 / MOV R3, #7 / CMP R2, R3

    Watch for: FLAGS: negative flag (N) is set, zero flag clears

    CMP computes 3 - 7 = -4. The result is negative, so the negative flag is set. The zero flag is cleared because the result is not zero. This encodes 'R2 is less than R3'.

  3. 3

    Compare larger > smaller

    Do this: Step through MOV R4, #9 / MOV R5, #4 / CMP R4, R5

    Watch for: FLAGS: no special flags set (result is positive and non-zero)

    CMP computes 9 - 4 = 5. The result is positive and non-zero, so neither the zero nor negative flags are set. This encodes 'R4 is greater than R5'.

  4. 4

    Use flags to branch

    Do this: Step through the final CMP R0, R1 and JZ equal

    Watch for: Zero flag set again, JZ jumps to the 'equal' label, R6 = 1

    The last CMP re-compares R0 and R1 (still both 5), setting the zero flag. JZ checks the flag and jumps. This demonstrates the full compare-then-branch pattern.

Objectives

  • •See the zero flag (Z) set after CMP R0, R1 because 5 equals 5
  • •See the negative flag (N) set after CMP R2, R3 because 3 is less than 7
  • •See no flags set after CMP R4, R5 because 9 is greater than 4
  • •Watch JZ take the branch to 'equal' because the final CMP sets the zero flag

Hints

  • CMP subtracts without storing the result - it only updates the FLAGS register so a following jump can read them