Teacher answer key

Decisions and Flags

Gather four kinds of evidence: prediction of relevant state; behavior across varied cases including boundaries; explanation using trace/state evidence; and a reasoned correction of one mismatch. Mark each as emerging, with support or independent using paper or your school’s existing approved process. A challenge pass is machine-behavior evidence, not proof of authorship or complete mastery. Accept equivalent correct programs; the public key is a model, not the only possible answer.

Public teaching material. Solutions are examples; equivalent correct programs may also pass the actual checks.

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

Try prediction key

Output: 1

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
LOAD0107{"registers":[7,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
CMP1277{"registers":[7,0,0,0,0,0,0,0],"flags":{"zero":true,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
JUMP_IF_ZERO2577{"registers":[7,0,0,0,0,0,0,0],"flags":{"zero":true,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None

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

Example challenge solution

INPUT
COPY R0 R1
INPUT
CMP R1
JUMP_IF_ZERO same
LOAD 0
JUMP show
same: LOAD 1
show: PRINT
HALT

Actual checker fixtures

Case 1
Input
5, 5
Initial memory
All bytes initially zero
Expected output
1
Case 2
Input
2, 3
Initial memory
All bytes initially zero
Expected output
0
Case 3
Input
-2, -2
Initial memory
All bytes initially zero
Expected output
1

3. Explain the machine

Can Z=true when R0 is nonzero?

Reasoning and teaching note

Yes: CMP 7 with R0=7 sets zero from the difference while R0 remains7.