Student worksheet

Decisions and Flags

Three lesson sections: predict, build, explain. Work on paper or in the local lab. No name, account or student record is requested by the app.

10. Conditional Logic: Making Decisions

Compare values, then branch on a stored flag.

Open this lesson →

1. Predict and trace

Before running the Try program, predict its output and trace the first three executed instructions. Track the relevant registers, flags or memory as needed. Then step the program to compare.

LOAD 7
CMP 7
JUMP_IF_ZERO same
LOAD 0
JUMP show
same: LOAD 1
show: PRINT
HALT

Queue input: None

Initialize memory: All bytes initially zero

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
       
       
       

2. Build and check

Read two integers. Print 1 when they are equal, otherwise print 0. Handle both equal and unequal inputs.

Required instruction types: INPUT, CMP, JUMP_IF_ZERO

Starter program

INPUT
COPY R0 R1
INPUT
CMP R1
# Add two labeled paths.
HALT

Your program and notes

3. Explain the machine

Can Z=true when R0 is nonzero?