Profile an accumulator program

Engineers profile programs to see how much work they do. Run an accumulator program and report how many instructions ran, how much data memory it touched and how often it jumped.

What to build

A program that runs an accumulator-machine program and prints a profile of the run.

Requirements

  • The machine is the one from the exercise "Run an accumulator machine", with memory of 100 cells all 0, ACC = 0 and PC = 0. OUT prints nothing here.
  • Count the instructions executed (HALT included), the data reads (LOAD, ADD, SUB), the data writes (STORE) and the jumps taken (every JMP, and JZ when ACC is 0).
  • Stop at HALT, when the PC moves past the last instruction, or when 1000 instructions have run.
  • Print instructions N, data reads R, data writes W, jumps J and stopped X where X is halt, end or limit.

Input

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

Output

Five lines.

Constraints

1 ≤ n ≤ 100.

Example

Input:

5
LOADI 4
STORE 0
ADD 0
OUT
HALT

Output:

instructions 5
data reads 1
data writes 1
jumps 0
stopped halt

Why it works

Counting needs no new idea: it is the same fetch-decode-execute loop with a counter on each branch of the decode. The limit makes the answer for a program that never stops finite and testable.

Examples

Example 1 · Input
5
LOADI 4
STORE 0
ADD 0
OUT
HALT
Expected output
instructions 5
data reads 1
data writes 1
jumps 0
stopped halt
Example 2 · Input
11
LOADI 1
STORE 1
LOADI 3
STORE 0
LOAD 0
JZ 10
OUT
SUB 1
STORE 0
JMP 4
HALT
Expected output
instructions 25
data reads 7
data writes 5
jumps 4
stopped halt

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

Hints

Hint 1

Run the same machine as in the previous exercise, but count instead of printing OUT.

Hint 2

Each executed instruction counts once, including HALT and OUT. LOAD, ADD and SUB read data; STORE writes data.

Hint 3

A JMP always counts as a taken jump; a JZ counts only when ACC is 0. Stop with `limit` after 1000 executed instructions.

Solution

Show one correct solution (try it yourself first)
n = int(input())
program = [input().split() for _ in range(n)]
memory = [0] * 100
pc, acc = 0, 0
count = reads = writes = jumps = 0
why = "end"
while pc < len(program):
    if count == 1000:
        why = "limit"
        break
    parts = program[pc]
    pc += 1
    count += 1
    op = parts[0]
    arg = int(parts[1]) if len(parts) > 1 else 0
    if op == "LOADI":
        acc = arg
    elif op == "LOAD":
        acc = memory[arg]
        reads += 1
    elif op == "STORE":
        memory[arg] = acc
        writes += 1
    elif op == "ADD":
        acc += memory[arg]
        reads += 1
    elif op == "SUB":
        acc -= memory[arg]
        reads += 1
    elif op == "JMP":
        pc = arg
        jumps += 1
    elif op == "JZ":
        if acc == 0:
            pc = arg
            jumps += 1
    elif op == "HALT":
        why = "halt"
        break
print("instructions", count)
print("data reads", reads)
print("data writes", writes)
print("jumps", jumps)
print("stopped", why)

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

Lesson: The fetch–decode–execute cycle →