-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler_new.py
364 lines (297 loc) · 13.4 KB
/
assembler_new.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
#!/home/sofyan/anaconda3/bin/python
import re
import os
import sys
Assembly_opcode_map={}
Opcode_assembly_out=[]
One_operand_opcodes=[]
Two_operand_opcodes=[]
branches_jumps_operand_opcodes=[]
memory_opcodes=[]
Only_OpCode=[]
###########################################################################################
# FILLING THE DICTIONARIES
###########################################################################################
def registers_adressing_modes_map_append():
Assembly_opcode_map["R0"] = "000"
Assembly_opcode_map["R1"] = "001"
Assembly_opcode_map["R2"] = "010"
Assembly_opcode_map["R3"] = "011"
Assembly_opcode_map["R4"] = "100"
Assembly_opcode_map["R5"] = "101"
Assembly_opcode_map["R6"] = "110"
Assembly_opcode_map["R7"] = "111"
def oneoperand_opcode_map_append():
Assembly_opcode_map['NOP'] = '00000'
Assembly_opcode_map['NOT'] = '00001'
Assembly_opcode_map['INC'] = '00010'
Assembly_opcode_map['DEC'] = '00011'
Assembly_opcode_map['OUT'] = '00100'
Assembly_opcode_map['IN'] = '00101'
def twooperands_opcode_map_append():
Assembly_opcode_map["SWAP"] = "01000"
Assembly_opcode_map["ADD"] = "01001"
Assembly_opcode_map["IADD"] = "01010"
Assembly_opcode_map["SUB"] = "01011"
Assembly_opcode_map["AND"] = "01100"
Assembly_opcode_map["OR"] = "01101"
Assembly_opcode_map["SHL"] = "01110"
Assembly_opcode_map["SHR"] = "01111"
def memory_opcode_map_append():
Assembly_opcode_map["PUSH"] = "10000"
Assembly_opcode_map["POP"] = "10001"
Assembly_opcode_map["LDM"] = "10010"
Assembly_opcode_map["LDD"] = "10011"
Assembly_opcode_map["STD"] = "10100"
def branches_jumps_opcode_map_append():
Assembly_opcode_map["JZ"] = "11000"
Assembly_opcode_map["JMP"] = "11001"
Assembly_opcode_map["CALL"] = "11010"
Assembly_opcode_map["RET"] = "11011"
Assembly_opcode_map["RTI"] = "11100"
#############################################################################################
# FILLING THE ARRAYS
##############################################################################################
def one_operand_opcode_append():
One_operand_opcodes.extend(['NOP','NOT','INC','DEC','OUT','IN'])
def two_operand_opcode_append():
Two_operand_opcodes.extend(["SWAP", "ADD" , "IADD" , "SUB" , "AND" , "OR" , "SHL" , "SHR" ])
def memory_operand_opcode_append():
memory_opcodes.extend(["PUSH" , "POP" , "LDM" , "LDD" , "STD"])
def branches_jumps_operand_opcode_append():
branches_jumps_operand_opcodes.extend(["JZ" , "JMP" , "CALL" , "RET" , "RTI"])
def Only_OpCode_append():
Only_OpCode.extend(["NOP", "RET", "RTI"])
###############################################################################################
# FUNCTION TO LOAD ALL DICTIONARIES AND ARRAYS
################################################################################################
def LoadEveryThing():
registers_adressing_modes_map_append()
oneoperand_opcode_map_append()
twooperands_opcode_map_append()
memory_opcode_map_append()
branches_jumps_opcode_map_append()
one_operand_opcode_append()
two_operand_opcode_append()
memory_operand_opcode_append()
branches_jumps_operand_opcode_append()
Only_OpCode_append()
##############################################################################################
# DICIMAL TO BINARY
###############################################################################################
def decimal2binary(x,n = 0):
binary = []
def decimaltobinary(y,n):
if y > 1 or n >= 1:
decimaltobinary(y/2 , n-1)
binary.append(int(y%2))
Negative = False
if(x < 0):
Negative = True
decimaltobinary(abs(x),n)
if(Negative == True):
binary[0] = '1'
for i in range(len(binary)):
binary[i] = str(binary[i])
string = ''.join(binary)
return string
###########################################################################################
# SOME HELPER FUNCTIONS
###########################################################################################
def convert_assembly_binary(assembilylines):
#array of opcode lines (could be 1 or 2 or 3 i think)
for line in assembilylines:
ins_type=type_of_instruction(line)
if ins_type == 0:
one_operand_assmebly_binary_convert(line)
elif ins_type == 1:
two_operand_assmebly_binary_convert(line)
elif ins_type == 2:
branch_operand_assmebly_binary_convert(line)
elif ins_type == 3:
memory_operand_assmebly_binary_convert(line)
else :
Opcode_assembly_out.append(decimal2binary(int(line),16))
#this function will return what type of assembly line it is to deal with it
def type_of_instruction(line):
# one operand => 0
# two operand => 1
# branch , jumps operand => 2
# memory => 3
Opcode = line.split(" ",1)[0]
if Opcode in One_operand_opcodes:
return 0
if Opcode in Two_operand_opcodes:
return 1
if Opcode in branches_jumps_operand_opcodes:
return 2
if Opcode in memory_opcodes:
return 3
###########################################################################################
# READ THE FILE OF INSTRUCTIONS
###########################################################################################
#this function to read assembly from file reomve the comments
#retrun array of strings (assembly lines)
def readInputFile(fileName:str):
#read from input file
myfile=open(fileName, "r")
#contents will be array of lines from assembly
contents=myfile.readlines()
# a directory to store key:value pairs
assemblylines=[]
list_addresses=[]
list_itr=-1
for line in contents:
if(line=='\n' or line == ''):
continue
if(line.endswith("\n")):
line = line[:-1]
removecomments=line.split('#')[0]
if(removecomments==''):
continue
removetabs=removecomments.split('\t')[0]
if removetabs=='':
continue
clean_line = removetabs.upper().strip()
if(clean_line.split(' ')[0]==".ORG"):
list_addresses.append(int(clean_line.split(' ')[1]))
list_itr+=1
assemblylines.append([])
else:
assemblylines[list_itr].append(clean_line)
return assemblylines,list_addresses
############################################################################################
# SPLIT THE INSTRUCTION TO Rs AND OPCODES
############################################################################################
def BeforeBinary(line):
elements = []
elements.append(line.split(" ",1)[0])
if elements[0] not in Only_OpCode:
elements.extend((line.split(" ",1)[1]).split(","))
for i in range(0,len(elements)):
elements[i] = elements[i].strip()
return elements
############################################################################################
# ONE OPERAND
############################################################################################
def one_operand_assmebly_binary_convert(line):
Opcode = line.split(" ")[0]
b_opcode=Assembly_opcode_map[Opcode]
if(Opcode.upper()=="NOP"):
Opcode_assembly_out.append("0"*16)
elif(Opcode.upper()=="NOT" or Opcode.upper()=="INC" or Opcode.upper()=="DEC"):
r_src=Assembly_opcode_map[line.split(" ")[1]]
assembly_line=b_opcode+r_src+"000"+r_src+"00"
Opcode_assembly_out.append(assembly_line)
elif(Opcode.upper()=="IN"):
r_src=Assembly_opcode_map[line.split(" ")[1]]
assembly_line=b_opcode+("0"*6)+r_src+"00"
Opcode_assembly_out.append(assembly_line)
elif(Opcode.upper()=="OUT"):
r_src=Assembly_opcode_map[line.split(" ")[1]]
assembly_line=b_opcode+r_src+("0"*8)
Opcode_assembly_out.append(assembly_line)
############################################################################################
# TWO OPERAND
############################################################################################
def two_operand_assmebly_binary_convert(line):
Opcode = line.split(" ")[0]
b_opcode=Assembly_opcode_map[Opcode]
if(Opcode.upper()=="SWAP"):
operands=line.split(" ",1)[1].replace(" ", "")
src1,src2=operands.split(',')[0],operands.split(',')[1]
b_src1,b_src2=Assembly_opcode_map[src1],Assembly_opcode_map[src2]
assembly_line=b_opcode+b_src1+b_src2+("0"*5)
Opcode_assembly_out.append(assembly_line)
elif(Opcode.upper()=="ADD" or Opcode.upper()=="SUB" or Opcode.upper()=="AND" or Opcode.upper()=="OR"):
operands=line.split(" ",1)[1].replace(" ", "")
src1,src2,dst=operands.split(',')[0],operands.split(',')[1],operands.split(',')[2]
b_src1,b_src2,b_dst=Assembly_opcode_map[src1],Assembly_opcode_map[src2],Assembly_opcode_map[dst]
assembly_line=b_opcode+b_src1+b_src2+b_dst+("0"*2)
Opcode_assembly_out.append(assembly_line)
elif(Opcode.upper()=="IADD"):
operands=line.split(" ",1)[1].replace(" ", "")
src,dst,imm=operands.split(',')[0],operands.split(',')[1],operands.split(',')[2]
b_src,b_dst,b_imm=Assembly_opcode_map[src],Assembly_opcode_map[dst],decimal2binary(int(imm),16)
assembly_line=b_opcode+b_src+("0"*3)+b_dst+b_imm+("0"*2)
Opcode_assembly_out.append(assembly_line[:16])
Opcode_assembly_out.append(assembly_line[16:])
elif(Opcode.upper()=="SHL" or Opcode.upper()=="SHR"):
operands=line.split(" ",1)[1].replace(" ", "")
src,dst,imm=operands.split(',')[0],operands.split(',')[0],operands.split(',')[1]
b_src,b_dst,b_imm=Assembly_opcode_map[src],Assembly_opcode_map[dst],decimal2binary(int(imm),16)
assembly_line=b_opcode+b_src+("0"*3)+b_dst+b_imm+("0"*2)
Opcode_assembly_out.append(assembly_line[:16])
Opcode_assembly_out.append(assembly_line[16:])
############################################################################################
# BRANCH
############################################################################################
# the instruction must be uppercase
def branch_operand_assmebly_binary_convert(line):
Opcode_assembly_out.append("".join(branch_operand_assmebly_binary_convert_private(line)))
def branch_operand_assmebly_binary_convert_private(line):
elements = BeforeBinary(line)
IR = ['0'] * 16
OpCode = elements[0]
IR[0:5] = list(Assembly_opcode_map.get(OpCode))
if(OpCode == "RTI" or OpCode == "RET"):
return IR
Rdst = elements[1]
IR[5:8] = list(Assembly_opcode_map.get(Rdst))
return IR
############################################################################################
# MEMORY
############################################################################################
def memory_operand_assmebly_binary_convert(line):
IR = memory_operand_assmebly_binary_convert_private(line)
Opcode_assembly_out.append("".join(IR[0:16]))
if(BeforeBinary(line)[0] != "PUSH" and BeforeBinary(line)[0] != "POP"):
Opcode_assembly_out.append("".join(IR[16:32]))
def memory_operand_assmebly_binary_convert_private(line):
elements = BeforeBinary(line)
IR = ['0'] * 32
OpCode = elements[0]
Rdst = elements[1]
IR[0:5] = list(Assembly_opcode_map.get(OpCode))
IR[8:11] = list(Assembly_opcode_map.get(Rdst))
if(OpCode == "PUSH" or OpCode == "POP"):
return IR
if(OpCode == "LDM"):
Imm = decimal2binary(int(elements[2]),16)
IR[14:30] = list(Imm)
return IR
EA = decimal2binary(int(elements[2]),20)
IR[11:31] = EA
return IR
#############################################################################################
# Write output file
#############################################################################################
def writeOutputFile():
f=open("Output.txt", "w+")
for opline in Opcode_assembly_out:
f.write(opline+"\n")
#############################################################################################
# MAIN
#############################################################################################
if __name__ == "__main__":
LoadEveryThing()
filename= "Input.asm"
if(len(sys.argv)>1):
filename=sys.argv[1]
assembly_lines=readInputFile(filename)
Arr = assembly_lines[0]
Arr2 = assembly_lines[1]
# to decrement the org0 and org2
Org0 = Arr2.index(0)
Org2 = Arr2.index(2)
Arr[Org0][0] = str(int(Arr[Org0][0])-1)
Arr[Org2][0] = str(int(Arr[Org2][0])-1)
################################################
counter = 0
for i,k in enumerate(Arr):
while(counter < Arr2[i]):
Opcode_assembly_out.append("XXXXXXXXXXXXXXXX")
counter = counter + 1
convert_assembly_binary(k)
counter = counter + len(k)
writeOutputFile()