Teacher answer key

Repetition and Counters

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.

13. Loops: Repeating Things

Pair LOOP with RETURN and treat zero repeats correctly.

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 8
LOOP 4
  PRINT
RETURN
HALT

Queue input: None

Initialize memory: All bytes initially zero

Try prediction key

Output: 8, 8, 8, 8

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
LOAD0108{"registers":[8,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]}None
LOOP1288{"registers":[8,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[{"start":1,"end":3,"remaining":4,"callDepth":0}],"memoryReads":[],"memoryWrites":[]}None
PRINT2388{"registers":[8,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[{"start":1,"end":3,"remaining":4,"callDepth":0}],"memoryReads":[],"memoryWrites":[]}8

2. Build and check

Read a repeat count from 0 through 5. Print 8 exactly that many times, then halt.

Required instruction types: INPUT, LOOP, PRINT, RETURN

Example challenge solution

INPUT
COPY R0 R1
LOAD 8
LOOP R1
PRINT
RETURN
HALT

Actual checker fixtures

Case 1
Input
0
Initial memory
All bytes initially zero
Expected output
None
Case 2
Input
1
Initial memory
All bytes initially zero
Expected output
8
Case 3
Input
4
Initial memory
All bytes initially zero
Expected output
8, 8, 8, 8

3. Explain the machine

How many times does LOOP 0 execute its body?

Reasoning and teaching note

Zero; it skips the paired body/RETURN. A negative count errors instead.