Student worksheet

The Stack and Functions

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.

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

InstructionPC beforePC afterR0 beforeR0 afterRelevant stateOutput
       
       
       

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

Starter program

INPUT
PUSH
INPUT
PUSH
# Pop and print twice.
HALT

Your program and notes

3. Explain the machine

Why does the second supplied value print first?