-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.py
71 lines (56 loc) · 1.47 KB
/
Simulator.py
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
from Itype import *
# Variables
fileName = "a.asm"
source = open(fileName, 'r')
current = 0
instruction = list(map(lambda x: x.strip().lower(), source.readlines()))
# Index of jumps
for i in range(len(instruction)):
tmp = instruction[i]
if ':' in tmp:
jumps[tmp[:-1]] = i
# Execute function
def Execute(ins):
if ins == "":
return -1
elif ':' in ins:
return -1
tmp = ins.find(" ")
fun = ins[:tmp]
var = ins[tmp + 1:].split(',')
opcode = supported[fun]
if opcode == 1:
return jumps[var[0]]
elif opcode == 2:
p0 = var[0]
p1 = var[1]
r = {}
exec(f"r = {fun[0].upper() + fun[1:]}('{p0}','{p1}')",globals(),r)
return r['r']
elif opcode == 3:
p0 = var[0]
p1 = var[1]
p2 = var[2]
exec(f"{fun[0].upper() + fun[1:]}('{p0}','{p1}','{p2}')")
elif opcode == 4:
p0 = var[0]
p1 = var[1]
p2 = var[2]
exec(f"{fun[0].upper() + fun[1:]}('{p0}','{p1}',{p2})")
elif opcode == 5:
p0 = var[0]
p1 = var[1]
p2 = var[2]
r = {}
exec(f"r = {fun[0].upper() + fun[1:]}('{p0}','{p1}','{p2}')")
return r['r']
return -1
# Main loop
while current != len(instruction):
temp = Execute(instruction[current])
if temp == -1:
current += 1
else:
current = temp
displayRegisters()
source.close()