Skip to main content
Be Bitwise

Conditional Jump

Use CMP and JZ to make decisions. If two numbers are equal, jump to a different part of the program.

CPUs make decisions using a two-step pattern: first a comparison instruction sets flags in the FLAGS register, then a conditional jump instruction checks those flags to decide whether to change the program counter. This is how every if-statement, loop condition, and branch in every programming language works at the hardware level.

Simulator

Loading simulator...

Walkthrough Guide

  1. 1

    Load values to compare

    Do this: Step through both MOV instructions

    Watch for: R0 = 5, R1 = 5

    We load two equal values into registers. The CPU has no idea these will be compared yet — it just executes MOV instructions as usual.

  2. 2

    Compare the values

    Do this: Step through the CMP instruction

    Watch for: The FLAGS register updates — the zero flag (Z) gets set

    CMP subtracts R1 from R0 internally (5 - 5 = 0) but throws away the result. It only keeps the side effects: because the result was zero, the zero flag is set. This flag is the CPU's way of remembering that the two values were equal.

  3. 3

    Take the conditional jump

    Do this: Step through the JZ instruction

    Watch for: PC jumps to the 'equal' label address instead of the next sequential instruction

    JZ means 'jump if zero flag is set'. Since CMP set the zero flag, the CPU changes PC to point at the 'equal' label. If the values had been different, PC would have just moved to the next instruction as normal.

  4. 4

    Execute the chosen branch

    Do this: Step through the remaining instructions

    Watch for: R2 = 1 (the 'equal' branch ran), then HALT

    The program set R2 to 1, proving the equal branch executed. Try modifying one of the MOV values to see the other branch run instead.

Objectives

  • •See the FLAGS register update after CMP
  • •Watch the zero flag (Z) get set when R0 equals R1
  • •See the PC jump to the 'equal' label address
  • •Try changing one MOV to a different value to take the other branch

Hints

  • The CMP instruction subtracts without storing the result - it only sets flags