4. Emoji Math
Combine addition, subtraction, and multiplication as ordered steps.
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 10 ADD 5 PRINT MUL 2 SUB 4 PRINT HALT
Queue input: None
Initialize memory: All bytes initially zero
Try prediction key
Output: 15, 26
| Instruction | PC before | PC after | R0 before | R0 after | Relevant state | Output |
|---|---|---|---|---|---|---|
| LOAD | 0 | 1 | 0 | 10 | {"registers":[10,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]} | None |
| ADD | 1 | 2 | 10 | 15 | {"registers":[15,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]} | None |
| 2 | 3 | 15 | 15 | {"registers":[15,0,0,0,0,0,0,0],"flags":{"zero":false,"negative":false,"overflow":false},"sp":255,"stack":[],"callStack":[],"loopStack":[],"memoryReads":[],"memoryWrites":[]} | 15 |
2. Build and check
Read one integer x and print (x + 5) × 2 − 4. Your program must work for every supplied input, including negative numbers.
Required instruction types: INPUT, ADD, MUL, SUB
Example challenge solution
INPUT ADD 5 MUL 2 SUB 4 PRINT HALT
Actual checker fixtures
Case 1
- Input
0- Initial memory
All bytes initially zero- Expected output
6
Case 2
- Input
-3- Initial memory
All bytes initially zero- Expected output
0
Case 3
- Input
12- Initial memory
All bytes initially zero- Expected output
30
3. Explain the machine
Why is MUL after ADD necessary for (x+5)×2−4?
Reasoning and teaching note
Each operation modifies the accumulated result; reversing ADD/MUL changes the expression for most x.