This repository was archived by the owner on Nov 10, 2024. It is now read-only.
forked from bloominstituteoftechnology/Computer-Architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.py
More file actions
73 lines (59 loc) · 1.12 KB
/
simple.py
File metadata and controls
73 lines (59 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
PRINT_SAM = 1
HALT = 2
PRINT_NUM = 3
SAVE = 4
PRINT_REGISTER = 5
ADD = 6
memory = [
PRINT_SAM,
PRINT_SAM,
PRINT_SAM,
PRINT_NUM,
14,
SAVE,
99,
2,
PRINT_REGISTER,
2,
SAVE,
101,
1,
ADD,
1,
2,
PRINT_REGISTER,
1,
HALT
]
# R0 - R7; very limited but very fast
registers = [0] * 8
# L1, L2; shed
# RAM; warehouse
running = True
pc = 0
while running:
command = memory[pc]
if command == ADD:
# reg1 = reg1 + reg2
regidx1 = memory[pc + 1]
regidx2 = memory[pc + 2]
registers[regidx1] = registers[regidx1] + registers[regidx2]
pc += 3
elif command == PRINT_NUM:
print(memory[pc + 1])
pc += 2
elif command == PRINT_REGISTER:
print(registers[memory[pc + 1]])
pc += 2
elif command == PRINT_SAM:
print("Sam!")
pc += 1
elif command == SAVE:
registers[memory[pc + 2]] = memory[pc + 1]
pc += 3
elif command == HALT:
running = False
else:
print("Error!")
sys.exit(1)