Student worksheet

Build Small Programs

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.

28. A Tiny Target Game

Build a three-round game with stored state and scoring.

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.

STORE 5 200
LOAD [200] R1
LOAD 0 R2
LOOP 3
INPUT
CMP R1
JUMP_IF_ZERO hit
JUMP next
hit: LOAD R2
ADD 1
COPY R0 R2
next: NOP
RETURN
LOAD R2
STORE 201
PRINT
HALT

Queue input: 5, 2, 5

Initialize memory: 200: 5

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
       
       
       

2. Build and check

Memory 200 contains a target byte. Read exactly three guesses. Count how many equal the target, store the score at memory 201, and print that score once.

Required instruction types: INPUT, CMP, LOOP, STORE, JUMP_IF_ZERO

Starter program

LOAD [200] R1
LOAD 0 R2
LOOP 3
INPUT
CMP R1
# Add one point only on a hit.
RETURN
LOAD R2
STORE 201
PRINT
HALT

Your program and notes

3. Explain the machine

What state changes on a correct guess and on an incorrect one?