-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.py
398 lines (386 loc) · 17.2 KB
/
assembler.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from sys import argv,stdout
from traceback import print_exc
class Assembler:
file_all_line = []
OPTAB = {}
SYMTAB = {}
LOCCTR = 0
start = 0
length = 0
execute = 0
bitmask = ''
object_codes = []
location = []
#### Initalize ####
def __init__(self, file_name):
try :
with open(file_name, 'r') as assembly_file:
self.file_all_line = [x.strip().upper() for x in assembly_file.readlines()]
self.initOpCode()
print(self.OPTAB)
except IOError:
print('-' * 60)
print("ERROR : File not found")
print('-' * 60)
exit()
except:
print('-' * 60)
print("ERROR : Unknow error")
print_exc(file=stdout)
print('-' * 60)
exit()
def initOpCode(self):
try :
with open('sic_instructions.txt', 'r') as sic_instruction_file:
sic_instruction_line = sic_instruction_file.readlines()
for iterator in sic_instruction_line:
key, value = iterator.split()
self.OPTAB[key] = value
except IOError:
print('-' * 60)
print("ERROR : sic_instructions.txt not found")
print_exc(file=stdout)
print('-' * 60)
exit()
except :
print('-' * 60)
print("ERROR : Unknow error")
print('-' * 60)
exit()
#### Pass 1 ####
def passOne(self):
for line in self.file_all_line:
try :
line_col = line.split()
if len(line_col) == 3:
if(line_col[1] == 'START'):
if line_col[2].strip()[-1] == 'H':
self.LOCCTR = int(line_col[2].strip()[:-1],16)
elif line_col[2].strip()[:2] == '0X':
self.LOCCTR = int(line_col[2].strip()[2:],16)
else:
self.LOCCTR = int(line_col[2].strip())
#self.LOCCTR = int(line_col[2], 16)
self.start = self.LOCCTR
self.SYMTAB[line_col[0]] = self.LOCCTR
self.location.append(self.LOCCTR)
continue
# Store new SYMTAB here
self.SYMTAB[line_col[0]] = self.LOCCTR
elif len(line_col) == 2:
if line_col[1] == 'RSUB':
self.SYMTAB[line_col[0]] = self.LOCCTR
line_col = line_col + ['']
else:
line_col = [''] + line_col
elif len(line_col) == 1:
line_col = [''] + line_col + ['']
elif len(line_col) == 0:
continue
self.location.append(self.LOCCTR)
print("NOW : " + line + " " + hex(self.LOCCTR), len(self.location))
if line_col[1] == 'END':
if line_col[2].strip() in self.SYMTAB:
self.execute = self.SYMTAB[line_col[2].strip()]
else :
if line_col[2].strip()[-1] == 'H':
self.execute = int(line_col[2].strip()[:-1],16)
elif line_col[2].strip()[:2] == '0X':
self.execute = int(line_col[2].strip()[2:],16)
else:
self.execute = int(line_col[2].strip())
elif line_col[1] == 'WORD':
self.LOCCTR += 3
elif line_col[1] == 'RESW':
self.LOCCTR += 3 * int(line_col[2])
elif line_col[1] == 'RESB':
self.LOCCTR += int(line_col[2])
elif line_col[1] == 'BYTE':
#print len(line_col[2].strip()),
if line_col[2].strip()[0] == 'X':
self.LOCCTR += int((len(line_col[2].strip())-3)/2)
elif line_col[2].strip()[0] == 'C':
self.LOCCTR += len(line_col[2].strip())-3
else:
self.LOCCTR += 3
except :
print('-' * 60)
print("ERROR : Something went wrong in Pass I")
print("Line : " + line)
print_exc(file=stdout)
print('-' * 60)
exit()
self.length = self.LOCCTR - self.start
#### Pass 2 ####
def passTwo(self):
for line in self.file_all_line:
try :
line_col = line.split()
x_bit = 0
object_code = ''
if len(line_col) == 3:
if(line_col[1] == 'START'):
self.object_codes.append('')
self.bitmask += '0'
continue
elif len(line_col) == 2:
if line_col[1] == 'RSUB':
line_col = line_col + ['']
else:
line_col = [''] + line_col
elif len(line_col) == 1:
line_col = [''] + line_col + ['']
elif len(line_col) == 0:
continue
#print line
if line_col[1] == 'END':
self.bitmask += '0'
pass
elif line_col[1] == 'WORD':
if line_col[2].strip() in self.SYMTAB:
object_code = hex(self.SYMTAB[line_col[2].strip()])[2:].zfill(6)
self.bitmask += '1'
else :
object_code = hex(int(line_col[2]))[2:].upper().zfill(6)
self.bitmask += '0'
self.object_codes.append(object_code)
elif line_col[1] == 'RESW':
self.object_codes.append('')
self.bitmask += '0'
elif line_col[1] == 'RESB':
self.object_codes.append('')
self.bitmask += '0'
elif line_col[1] == 'BYTE':
if line_col[2].strip()[0] == 'X':
object_code = hex(int(line_col[2].strip()[2:-1],16))[2:].upper().zfill(len(line_col[2].strip()[2:-1]))
self.object_codes.append(object_code)
self.bitmask += '0'
elif line_col[2].strip()[0] == 'C':
for i in line_col[2].strip()[2:-1]:
object_code += hex(ord(i))[2:].upper()
self.object_codes.append(object_code)
self.bitmask += '0'
else:
object_code = bin(int(self.OPTAB[line_col[1]],16))[2:].zfill(8)
if line_col[2].strip()[-2:] == ',X':
object_code += '1'
else:
object_code += '0'
line_col[2] = line_col[2].replace(',X','')
if line_col[1] == 'RSUB':
line_col[2] = '0'
if line_col[2].strip() in self.SYMTAB:
object_code += bin(self.SYMTAB[line_col[2].strip()])[2:].zfill(15)
self.bitmask += '1'
else:
if line_col[2].strip()[-1] == 'H':
object_code += bin(int(line_col[2].strip()[:-1],16))[2:].zfill(15)
elif line_col[2].strip()[:2] == '0X':
object_code += bin(int(line_col[2].strip()[2:],16))[2:].zfill(15)
else:
object_code += bin(int(line_col[2].strip()))[2:].zfill(15)
self.bitmask += '0'
object_code = hex(int(object_code, 2))[2:].zfill(6)
self.object_codes.append(object_code)
print(self.object_codes[-1], line , self.bitmask[-1], len(self.object_codes))
except :
print('-' * 60)
print("ERROR : Something went wrong in Pass II")
print("Line : " + line)
print_exc(file=stdout)
print('-' * 60)
exit()
#print object_code
#### Get & Set ####
def getOpCode(self, mnemonic):
return self.OPTAB[mnemonic]
def createListingFile(self):
i = 0
listing_list = []
for line in self.file_all_line:
try :
line_col = line.split()
print(len(self.location) , i, hex(self.location[i]), len(self.object_codes),line)
if len(line_col) == 1:
line_col = [''] + line_col + ['']
elif len(line_col) == 2:
if line_col[1] == 'RSUB':
line_col = line_col + ['']
else:
line_col = [''] + line_col
elif len(line_col) == 0:
continue
tmp = '\t'.join(line_col)+'\t'
if i < len(self.object_codes):
#print len(self.location) , i, hex(self.location[i]), len(self.object_codes),line
listing_list.append(hex(self.location[i])[2:].zfill(4).upper() + '\t' + tmp + self.object_codes[i].upper())
else:
listing_list.append('\t' + tmp)
i += 1
except:
print('-' * 60)
print("ERROR : Cannot create Listing file")
print("Line : " + line)
print_exc(file=stdout)
print('-' * 60)
exit()
with open(argv[1][:-4]+'.lst', 'w') as write_file:
for line in listing_list:
write_file.write(line+'\n')
print(listing_list)
def createObjectFile(self):
i = 0
object_line = ''
object_list = []
point = 0
print("ABSOLUTE ~")
for line in self.file_all_line:
try:
line_col = line.split()
if len(line_col) == 1:
line_col = [''] + line_col + ['']
elif len(line_col) == 2:
if line_col[1] == 'RSUB':
line_col = line_col + ['']
else:
line_col = [''] + line_col
elif len(line_col) == 0:
continue
if line_col[1] == 'START':
object_line = 'H' + line_col[0] + ' '*(6-len(line_col[0])) + hex(self.start)[2:].zfill(6) + hex(self.length)[2:].zfill(6)
object_list.append(object_line)
object_line = ''
elif line_col[1] == 'END':
if object_line != '':
object_line = 'T' + hex(point)[2:].zfill(6) + hex(int(len(object_line)/2))[2:].zfill(2) +'000' + object_line
object_list.append(object_line)
object_line = ''
object_line = 'E' + hex(self.execute)[2:].zfill(6)
object_list.append(object_line)
elif line_col[1] == 'RESW' or line_col[1] == 'RESB':
if object_line != '':
object_line = 'T' + hex(point)[2:].zfill(6) + hex(int(len(object_line)/2))[2:].zfill(2) +'000' + object_line
object_list.append(object_line)
object_line = ''
else:
if len(object_line) + len(self.object_codes[i]) > 60:
object_line = 'T' + hex(point)[2:].zfill(6) + hex(int(len(object_line)/2))[2:].zfill(2) +'000' + object_line
object_list.append(object_line)
object_line = ''
if object_line == '':
point = self.location[i]
object_line += self.object_codes[i]
i += 1
except :
print('-' * 60)
print("ERROR : Cannot create Object file (absolute)")
print("Line : " + line)
print_exc(file=stdout)
print('-' * 60)
exit()
object_list = [x.upper() for x in object_list]
with open(argv[1][:-4]+'.obj','w') as write_file:
for line in object_list:
write_file.write(line+'\n')
print(object_list)
def createObjectFileRelocatable(self):
i = 0
object_line = ''
bitmask_line = ''
object_list = []
point = 0
print("RELOCATABLE ~")
for line in self.file_all_line:
try:
line_col = line.split()
if len(line_col) == 1:
line_col = [''] + line_col + ['']
elif len(line_col) == 2:
if line_col[1] == 'RSUB':
line_col = line_col + ['']
else:
line_col = [''] + line_col
elif len(line_col) == 0:
continue
if line_col[1] == 'START':
object_line = 'H' + line_col[0] + ' '*(6-len(line_col[0])) + hex(self.start)[2:].zfill(6) + hex(self.length)[2:].zfill(6)
object_list.append(object_line)
object_line = ''
elif line_col[1] == 'END':
if object_line != '':
calc_bitmask = hex(int(bitmask_line + '0'*(12-len(bitmask_line)), 2))[2:].zfill(3).upper()
bitmask_line = ''
object_line = 'T' + hex(point)[2:].zfill(6) + hex(len(object_line)/2)[2:].zfill(2) + calc_bitmask + object_line
object_list.append(object_line)
object_line = ''
object_line = 'E' + hex(self.execute)[2:].zfill(6)
object_list.append(object_line)
elif line_col[1] == 'RESW' or line_col[1] == 'RESB':
if object_line != '':
calc_bitmask = hex(int(bitmask_line + '0'*(12-len(bitmask_line)), 2))[2:].zfill(3).upper()
bitmask_line = ''
object_line = 'T' + hex(point)[2:].zfill(6) + hex(len(object_line)/2)[2:].zfill(2) + calc_bitmask + object_line
object_list.append(object_line)
object_line = ''
elif line_col[1] == 'WORD' or line_col[1] == 'BYTE':
print('YYYY' , object_line)
if object_line != '':
calc_bitmask = hex(int(bitmask_line + '0'*(12-len(bitmask_line)), 2))[2:].zfill(3).upper()
object_line = 'T' + hex(point)[2:].zfill(6) + hex(len(object_line)/2)[2:].zfill(2) + calc_bitmask + object_line
object_list.append(object_line)
point = self.location[i]
bitmask_line = self.bitmask[i]
calc_bitmask = hex(int(bitmask_line + '0'*(12-len(bitmask_line)), 2))[2:].zfill(3).upper()
bitmask_line = ''
object_line = self.object_codes[i]
object_line = 'T' + hex(point)[2:].zfill(6) + hex(len(object_line)/2)[2:].zfill(2) + calc_bitmask + object_line
object_list.append(object_line)
object_line = ''
else:
if len(object_line) + len(self.object_codes[i]) > 60:
calc_bitmask = hex(int(bitmask_line + '0'*(12-len(bitmask_line)), 2))[2:].zfill(3).upper()
bitmask_line = ''
object_line = 'T' + hex(point)[2:].zfill(6) + hex(len(object_line)/2)[2:].zfill(2) + calc_bitmask + object_line
object_list.append(object_line)
object_line = ''
if object_line == '':
point = self.location[i]
object_line += self.object_codes[i]
bitmask_line += self.bitmask[i]
i += 1
except :
print('-' * 60)
print("ERROR : Cannot create Object file (relocatable)")
print("Line : " + line)
print_exc(file=stdout)
print('-' * 60)
exit()
object_list = [x.upper() for x in object_list]
with open(argv[1][:-4]+'.obj', 'w') as write_file:
for line in object_list:
write_file.write(line+'\n')
print(object_list)
if __name__ == '__main__':
if len(argv) <= 1:
print('-' * 60)
print("ERROR : Invalid argrument")
print('-' * 60)
exit()
if argv[1][-4:].upper() != '.ASM':
print('-' * 60)
print("ERROR : Please Input file .ASM")
print('-' * 60)
#exit()
obj = Assembler(argv[1])
obj.passOne()
print(obj.SYMTAB)
obj.passTwo()
print(obj.file_all_line ,len(obj.file_all_line))
print(obj.object_codes, len(obj.object_codes))
print(obj.length)
print(obj.location, len(obj.location))
obj.createListingFile()
if obj.start != 0:
obj.createObjectFile()
else :
obj.createObjectFileRelocatable()