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
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.
11. Even or Odd
Use remainder and a branch to classify input.
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 -3
MOD 2
JUMP_IF_ZERO even
LOAD 0
JUMP show
even: LOAD 1
show: PRINT
HALT
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 0
2. Build and check
Read an integer. Print 1 for an even integer and 0 for an odd integer.
Required instruction types: INPUT, MOD, JUMP_IF_ZERO
Example challenge solution
INPUT
MOD 2
JUMP_IF_ZERO even
LOAD 0
JUMP show
even: LOAD 1
show: PRINT
HALT
Actual checker fixtures
Case 1
- Input
-3- Initial memory
All bytes initially zero- Expected output
0
Case 2
- Input
8- Initial memory
All bytes initially zero- Expected output
1
Case 3
- Input
0- Initial memory
All bytes initially zero- Expected output
1
Case 4
- Input
7- Initial memory
All bytes initially zero- Expected output
0
3. Explain the machine
Is−3 odd even though its MOD 2 result is−1?
Reasoning and teaching note
Yes. Any nonzero signed remainder is odd; zero remainder identifies even values including0.
12. A Memory-backed Decision
Make a decision from stored data and record the result.
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 23 40
LOAD [40] R1
LOAD 23
CMP R1
JUMP_IF_ZERO match
LOAD 0
JUMP record
match: LOAD 1
record: STORE 41
PRINT
HALT
Queue input: None
Initialize memory: 40: 23
Try prediction key
Output: 1
2. Build and check
Memory 40 holds a supplied ticket code. Read one guess, then print and store at address 41 a 1 for a match or a 0 otherwise.
Required instruction types: INPUT, CMP, STORE, JUMP_IF_ZERO
Example challenge solution
LOAD [40] R1
INPUT
CMP R1
JUMP_IF_ZERO match
LOAD 0
JUMP record
match: LOAD 1
record: STORE 41
PRINT
HALT
Actual checker fixtures
Case 1
- Input
23- Initial memory
40: 23- Expected output
1- Expected final memory
40: 23, 41: 1- Required memory reads
40
Case 2
- Input
22- Initial memory
40: 23- Expected output
0- Expected final memory
40: 23, 41: 0- Required memory reads
40
Case 3
- Input
0- Initial memory
40: 0- Expected output
1- Expected final memory
40: 0, 41: 1- Required memory reads
40
3. Explain the machine
Why should the program still work when memory 40 changes?
Reasoning and teaching note
The decision rule reads stored state instead of embedding a fixed ticket code; record/display same1 or0 at41.