program = [["LOAD", 6], ["ADD", 7], ["STORE", 8], ["PRINT", 8], ["HALT", 0]]
ram = [0, 0, 0, 0, 0, 0, 12, 30, 0] # data at addresses 0 to 8
acc = 0 # accumulator register
pc = 0 # program counter register
running = True
while running:
op, addr = program[pc] # FETCH the instruction pc points to
pc = pc + 1 # point to the next instruction
if op == "LOAD": # DECODE the operation, then EXECUTE it
acc = ram[addr]
elif op == "ADD":
acc = acc + ram[addr]
elif op == "STORE":
ram[addr] = acc
elif op == "PRINT":
print("Output:", ram[addr])
elif op == "HALT":
running = False
print(op, addr, "-> acc =", acc, " pc =", pc)
A fetch-decode-execute simulator. Each line of the trace shows one instruction, the accumulator after it ran, and where the program counter points next. Output (one line each): LOAD 6 -> acc = 12 pc = 1 / ADD 7 -> acc = 42 pc = 2 / STORE 8 -> acc = 42 pc = 3 / Output: 42 / PRINT 8 -> acc = 42 pc = 4 / HALT 0 -> acc = 42 pc = 5