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
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?
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
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
Starter program
INPUT
MOD 2
# Branch using the zero flag.
HALT
Your program and notes
3. Explain the machine
Is−3 odd even though its MOD 2 result is−1?
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
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
Starter program
LOAD [40] R1
INPUT
CMP R1
# Decide and record the result.
HALT
Your program and notes
3. Explain the machine
Why should the program still work when memory 40 changes?