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 · slides 14–17
Lecture reference: Intro: Compilers, interpreters, and language processors · slide 14 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

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.
Instruction set

One instruction per line: an iMem address, a colon, the opcode and its operands. A line starting with * is a comment, and so is text after the operands. r,d,s is read as r,d(s), and a bare HALT as HALT 0,0,0. reg[7] is the PC; it already holds the next address when an instruction runs, so d(7) is relative to the next instruction.

TM instruction set
OpcodeEffect
RR N: OP r,s,t
HALTstop; prints "HALT: r,s,t"
INreg[r] = the next input value
OUTprint reg[r]
ADDreg[r] = reg[s] + reg[t]
SUBreg[r] = reg[s] - reg[t]
MULreg[r] = reg[s] * reg[t]
DIVreg[r] = reg[s] / reg[t] (srZERODIVIDE when reg[t] = 0)
RM N: OP r,d(s) with m = d + reg[s]
LDreg[r] = dMem[m]
STdMem[m] = reg[r]
RA N: OP r,d(s) with m = d + reg[s]
LDAreg[r] = m
LDCreg[r] = d
JLTif reg[r] < 0, reg[7] = m
JLEif reg[r] ≤ 0, reg[7] = m
JGTif reg[r] > 0, reg[7] = m
JGEif reg[r] ≥ 0, reg[7] = m
JEQif reg[r] = 0, reg[7] = m
JNEif reg[r] ≠ 0, reg[7] = m

Input

Integers IN reads, in order; separate them with spaces or commas.

0 of 1 read; the next IN reads 3.

Machine

Status: Ready Executed 0

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

Instruction memory. The arrow marks the PC (reg[7]); the highlighted row is the instruction of the current step.
PCAddrInstructionComment
PC0IN 0,0,0
1JLE 0,6(7)
2LDC 1,1(0)
3LDC 2,1(0)
4MUL 1,1,0
5SUB 0,0,2
6JNE 0,-3(7)
7OUT 1,0,0
8HALT 0,0,0
9–1023HALT 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

Data memory: cell 0, the top cell, every non-zero cell, and the cell the current instruction uses
AddrValueNote
01023the top address
1–10220
10230top cell

Console

No output yet.

  1. r0 = read
  2. if 0 < r0 then
  3. r1 = 1
  4. r2 = 1
  5. * repeat
  6. r1 = r1 * r0
  7. r0 = r0 - r2
  8. until r0 == 0
  9. write r1
  10. halt

The line for the instruction of the current step is marked.