A tiny processor

Combine what you learned: a processor that fetches an instruction, uses the right parts of the datapath and moves the PC. Write a simulator for a tiny register machine.

What to build

A program that runs a program of at most 100 instructions on a machine with 8 registers and 64 words of data memory and prints the registers at the end.

Requirements

  • The registers are r0 to r7. Register r0 always holds 0, even if an instruction tries to write to it. The data memory holds 64 words, all starting at 0, and an address is a word number.
  • add rd, rs, rt and sub rd, rs, rt set rd to rs + rt or rs − rt. addi rd, rs, imm sets rd to rs + imm.
  • lw rd, imm(rs) loads memory[rs + imm] into rd. sw rt, imm(rs) stores rt into memory[rs + imm].
  • beq rs, rt, offset continues at pc + 1 + offset if rs equals rt, otherwise at pc + 1.
  • Start with pc = 0. Stop when pc is not a valid instruction number or after 1000 instructions have run.
  • Print r1 = V to r7 = V one per line, then executed N.

Input

A line with n, then n instructions, one per line.

Output

8 lines.

Constraints

1 ≤ n ≤ 100. Every address the program uses is from 0 to 63.

Example

Input:

3
addi r1, r0, 5
addi r2, r1, 7
sub r3, r2, r1

Output:

r1 = 5
r2 = 12
r3 = 7
r4 = 0
r5 = 0
r6 = 0
r7 = 0
executed 3

Why it works

Each instruction type switches on a different set of datapath parts, as the control signals say, and then the PC moves to pc + 1 or to a branch target. Writing it as code shows that a processor is just this loop with a small table of cases.

Examples

Example 1 · Input
3
addi r1, r0, 5
addi r2, r1, 7
sub r3, r2, r1
Expected output
r1 = 5
r2 = 12
r3 = 7
r4 = 0
r5 = 0
r6 = 0
r7 = 0
executed 3
Example 2 · Input
7
addi r1, r0, 5
addi r2, r0, 0
add r2, r2, r1
addi r1, r1, -1
beq r1, r0, 1
beq r0, r0, -4
sw r2, 0(r0)
Expected output
r1 = 0
r2 = 15
r3 = 0
r4 = 0
r5 = 0
r6 = 0
r7 = 0
executed 22

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep registers = [0] * 8, memory = [0] * 64 and pc = 0. After every instruction set registers[0] = 0 again.

Hint 2

Pull the numbers from a line with re.findall(r"-?\d+", line), after taking the mnemonic. The orders are: add and sub give rd, rs, rt; addi gives rd, rs, imm; lw gives rd, imm, rs; sw gives rt, imm, rs; beq gives rs, rt, offset.

Hint 3

beq: if the two registers are equal, pc = pc + 1 + offset; otherwise pc + 1.

Solution

Show one correct solution (try it yourself first)
import re

n = int(input())
prog = [input().strip().lower() for _ in range(n)]
reg = [0] * 8
mem = [0] * 64
pc = count = 0
while 0 <= pc < n and count < 1000:
    line = prog[pc]
    op = line.split()[0]
    nums = [int(x) for x in re.findall(r"-?\d+", line[len(op):])]
    nxt = pc + 1
    if op == "add":
        reg[nums[0]] = reg[nums[1]] + reg[nums[2]]
    elif op == "sub":
        reg[nums[0]] = reg[nums[1]] - reg[nums[2]]
    elif op == "addi":
        reg[nums[0]] = reg[nums[1]] + nums[2]
    elif op == "lw":
        reg[nums[0]] = mem[reg[nums[2]] + nums[1]]
    elif op == "sw":
        mem[reg[nums[2]] + nums[1]] = reg[nums[0]]
    elif op == "beq":
        if reg[nums[0]] == reg[nums[1]]:
            nxt = pc + 1 + nums[2]
    reg[0] = 0
    pc = nxt
    count += 1
for r in range(1, 8):
    print(f"r{r} = {reg[r]}")
print("executed", count)

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: The datapath and the control unit →