19. Stack Operations: Last In, First Out
Save values on the data stack and restore them in reverse order.
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 11
PUSH
LOAD 22
PUSH
POP
PRINT
POP
PRINT
HALT
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 22, 11
2. Build and check
Read two integers, save both on the data stack, then print them in reverse order. Use PUSH and POP rather than hardcoded values.
Required instruction types: INPUT, PUSH, POP
Example challenge solution
INPUT
PUSH
INPUT
PUSH
POP
PRINT
POP
PRINT
HALT
Actual checker fixtures
Case 1
- Input
3, 8- Initial memory
All bytes initially zero- Expected output
8, 3
Case 2
- Input
-4, 0- Initial memory
All bytes initially zero- Expected output
0, -4
Case 3
- Input
9, 9- Initial memory
All bytes initially zero- Expected output
9, 9
3. Explain the machine
Why does the second supplied value print first?
Reasoning and teaching note
PUSH/POP are LIFO; with inputs3,8 output 8,3; call frames are not data values.
20. Calling a Subroutine
Reuse code and return to the instruction after CALL.
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
CALL square
PRINT
LOAD 4
CALL square
PRINT
HALT
square: MUL R0
RETURN
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 9, 16
2. Build and check
Read two integers. Call the same square subroutine for each and print the two squares in input order.
Required instruction types: INPUT, CALL, MUL, RETURN
Example challenge solution
INPUT
CALL square
PRINT
INPUT
CALL square
PRINT
HALT
square: MUL R0
RETURN
Actual checker fixtures
Case 1
- Input
3, 4- Initial memory
All bytes initially zero- Expected output
9, 16
Case 2
- Input
-2, 5- Initial memory
All bytes initially zero- Expected output
4, 25
Case 3
- Input
0, 8- Initial memory
All bytes initially zero- Expected output
0, 64
3. Explain the machine
Why do two calls to the same code return to different places?
Reasoning and teaching note
Each CALL saves its own PC+1 in a distinct call frame; function body is shared but caller continuation differs.
21. Preserving a Caller’s State
Use a save/restore convention when a function needs scratch space.
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 1 R1
LOOP 3
LOAD R1
CALL double
PRINT
LOAD R1
ADD 1
COPY R0 R1
RETURN
HALT
double: MUL 2
RETURN
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 2, 4, 6
2. Build and check
Read a saved value into R1, then a function argument. Call a helper that returns argument + 1 while temporarily using R1 as scratch space. Print the result and then the preserved first value. Use PUSH and POP to preserve R1.
Required instruction types: INPUT, PUSH, POP, CALL, RETURN
Example challenge solution
INPUT
COPY R0 R1
INPUT
CALL helper
PRINT
LOAD R1
PRINT
HALT
helper: COPY R0 R2
LOAD R1
PUSH
LOAD 99 R1
LOAD R2
ADD 1
COPY R0 R2
POP
COPY R0 R1
LOAD R2
RETURN
Actual checker fixtures
Case 1
- Input
12, 3- Initial memory
All bytes initially zero- Expected output
4, 12- Expected final registers
R1: 12
Case 2
- Input
-8, 0- Initial memory
All bytes initially zero- Expected output
1, -8- Expected final registers
R1: -8
Case 3
- Input
7, -5- Initial memory
All bytes initially zero- Expected output
-4, 7- Expected final registers
R1: 7
3. Explain the machine
Which saved state belongs to the caller rather than the function result?
Reasoning and teaching note
The preservation agreement identifies R1; save/restore it while returning the result via R0 and leaving separate call frames intact.