-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass1.py
159 lines (148 loc) · 4.35 KB
/
pass1.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pandas as pd
import json
MOT = {
'MOVER': ['IS', '01', 3],
'MOVEM': ['IS', '02', 3],
'MULT': ['IS', '03', 3],
'ADD': ['IS', '04', 1],
'COMP': ['IS', '05', 1],
'BC': ['IS', '06', 1],
'READ': ['IS', '07', 2],
'DIV': ['IS', '08', 2],
'PRINT': ['IS', '09', 2],
'STOP': ['IS', '00', 2],
'SUB': ['IS', '10', 1] }
ADs = {
'START': ['AD', '01', 0],
'END': ['AD', '02', 0],
'ORIGIN': ['AD', '03', 0],
'EQU': ['AD', '04', 0],
'LTORG': ['AD', '05', 0] }
CCs = {
'LT': 1,
'LE': 2,
'EQ': 3,
'GT': 4,
'GE': 5,
'ANY': 6 }
ST = {}
LT = {}
def parts(line):
label = line[:8]
if label == ' ': label = ''
neumonic = line[8:16].rstrip()
op = line[16:].split(',')
if op[0] == '': op = []
else: op[-1] = op[-1][:-1].strip()
return label.strip(), neumonic, op
def form(op):
global literal_counter
global symbol_counter
if len(op) == 0 or op[0] == '':
return ''
elif len(op) == 2:
if op[0][1:] == 'REG':
op[0] = f'({ord(op[0][0])-64})'
elif op[0] in CCs.keys():
op[0]= f'({CCs[op[0]]})'
if op[1][0] == '=':
if op[1] not in LT.keys():
literal_counter += 1
LT[op[1]] = [literal_counter]
op[1] = f'(L, {LT[op[1]][0]})'
else:
if op[1] not in ST.keys():
symbol_counter += 1
ST[op[1]] = [symbol_counter]
op[1] = f'(S, {ST[op[1]][0]})'
return op[0]+' '+op[1]
else:
if op[0].isnumeric(): return f'(C, {op[0]})'
elif op[0][0] == "'" and op[0][-1] == "'": return f'(C, {op[0][1:-1]})'
elif '+' in op[0] or '-' in op[0]:
i = op[0].find('+')
if i == -1: i = op[0].find('-')
suffix = op[0][i:]
op[0] = op[0][:i]
if op[0] not in ST.keys():
symbol_counter += 1
ST[op[0]] = [symbol_counter]
return f'(S, {ST[op[0]][0]}){suffix}'
else:
if op[0] not in ST.keys():
symbol_counter += 1
ST[op[0]] = [symbol_counter]
return f'(S, {ST[op[0]][0]})'
def inADs():
global lc
global symbol_counter
global literal_counter
if neumonic == 'START':
IR.append(' (AD, 01) (C, %s)\n' % str(op[0]).strip())
lc = int(op[0])
print()
elif neumonic == 'LTORG':
for i in LT.keys():
lc+=1
LT[i].append(lc)
else:
if label:
if label in ST.keys(): ST[label].append(lc)
else:
symbol_counter += 1
ST[label] = [symbol_counter, lc]
ops = form(op)
IR.append('lc: %d (%s, %s) %s\n' %
(lc, ADs[neumonic][0], ADs[neumonic][1], ops))
print()
lc += ADs[neumonic][2]
def inDLs():
global lc
global symbol_counter
global literal_counter
if label:
if label in ST.keys(): ST[label].append(lc)
else:
symbol_counter += 1
ST[label] = [symbol_counter, lc]
ops = form(op)
IR.append('lc: %d (%s, %s) %s\n' %
(lc, 'DL', '01' if neumonic == 'DS' else '02', ops))
print()
lc += 1
def inISs():
global lc
global symbol_counter
global literal_counter
if label:
if label in ST.keys(): ST[label].append(lc)
else:
symbol_counter += 1
ST[label] = [symbol_counter, lc]
ops = form(op)
IR.append('lc: %d (%s, %s) %s\n' %
(lc, MOT[neumonic][0], MOT[neumonic][1], ops))
print()
lc += MOT[neumonic][2]
if __name__ == "__main__":
with open('Inputs/asm1.txt', 'r') as source:
lines = source.readlines()
IR = []
symbol_counter = 0
literal_counter = 0
for line in lines:
label, neumonic, op = parts(line)
print(line.rstrip(), end=' ')
if neumonic in ADs.keys():
inADs()
elif neumonic in ['DS', 'DC']:
inDLs()
else:
inISs()
print()
print(pd.Series(ST))
with open('Outputs/IR.txt', 'w') as ir:
ir.writelines(IR)
json.dump(ST, open('./Tables/ST.json', 'w'))
json.dump(LT, open('./Tables/LT.json', 'w'))
json.dump(MOT, open('./Tables/MOT.json', 'w'))