Execution
TINY Machine
Load and step through TINY Machine (TM) programs: registers, memory, and each fetch–decode–execute step.
- Lecture reference: Intro: Compilers, interpreters, and language processors
Lecture reference: Intro: Compilers, interpreters, and language processors Factorial
The sample TINY program, with the pseudo-code printed beside it. Input 3.
What does this code do?
Show answer
It reads n into r0 and, when n > 0, computes n! in r1 and prints it: input 3 prints 6. For n ≤ 0 the JLE at 1 jumps to the HALT at 8 (m = 6 + reg[7] = 6 + 2), so nothing is printed. The JNE at 6 jumps back to 4 (m = -3 + reg[7] = -3 + 7) until r0 reaches 0.
Program 9 instructions at 0–8
Input
Integers IN reads, in order; separate them with spaces or commas.
- 3 (next)
0 of 1 read; the next IN reads 3.
Machine
Reset all registers are 0 (so the PC, reg[7], is 0); dMem[0] = 1023 and every other dMem cell is 0
Instruction memory iMem, 1024 cells
| PC | Addr | Instruction | Comment |
|---|---|---|---|
| PC | 0 | IN 0,0,0 | |
| 1 | JLE 0,6(7) | ||
| 2 | LDC 1,1(0) | ||
| 3 | LDC 2,1(0) | ||
| 4 | MUL 1,1,0 | ||
| 5 | SUB 0,0,2 | ||
| 6 | JNE 0,-3(7) | ||
| 7 | OUT 1,0,0 | ||
| 8 | HALT 0,0,0 | ||
| 9–1023 | HALT 0,0,0 · empty cells | ||
PC (reg[7]) instruction of this step jump target
Registers
- r0
- 0
- r1
- 0
- r2
- 0
- r3
- 0
- r4
- 0
- r5
- 0
- r6
- 0
- r7 PC
- 0
Data memory dMem, 1024 cells
| Addr | Value | Note |
|---|---|---|
| 0 | 1023 | the top address |
| 1–1022 | 0 | |
| 1023 | 0 | top cell |
Console
No output yet.
r0 = readif 0 < r0 thenr1 = 1r2 = 1* repeatr1 = r1 * r0r0 = r0 - r2until r0 == 0write r1halt
The line for the instruction of the current step is marked.