-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2_Pablo.py
611 lines (557 loc) · 21.8 KB
/
project2_Pablo.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# Authors: Lohith Muppala and Kishan Patel (Based of the base code provided by Trung Le)
# I used the base code provided by Trung Le and implemented the other functions
global w
# Remember where each of the jump label is, and the target location
def saveJumpLabel(asm, labelIndex, labelName):
lineCount = 0
for line in asm:
line = line.replace(" ", "")
if (line.count(":")):
labelName.append(line[0:line.index(":")]) # append the label name
labelIndex.append(lineCount) # append the label's index
# asm[lineCount] = line[line.index(":")+1:] #Dont include labels in linecount
lineCount += 1
for item in range(asm.count('\n')): # Remove all empty lines '\n'
asm.remove('\n')
print(str(lineCount))
def main():
registers = [] # 0$ = 0, $8=1, $9 = 2, $10 = 3, .......
for i in range(100):
registers.append(0)
memory = [] # allocates the memory till 12288 which is 0x3000
for i in range(12288):
memory.append(0)
memory.insert(8192, 79) # test value
labelIndex = []
labelName = []
instCount = 1
f = open("mc.txt", "w+")
h = open("mips.asm", "r")
asm = h.readlines()
for item in range(asm.count('\n')): # Remove all empty lines '\n'
asm.remove('\n')
saveJumpLabel(asm, labelIndex, labelName) # Save all jump's destinations
lineUpdate = False
mayo = str(0)
#for line in asm:
i=0
j = 0
ii = 0
r = 0
while(i < len(asm) + 9999):
try:
line = asm[i]
except:
pass
if lineUpdate == True: #tells if the function was updated ex: jump function
try:
line = asm[w]
w += 1
#lineUpdate = False
except:
f.close()
instCount += (len(labelName) * 1)
print("The instruction count is ", str(instCount))
#print("J type instruction total is %s", j)
#print("R type instruction total is %s", r)
#print("I type instruction total is %s", ii)
c = 0
while (c <= 99):
if (registers[c] != 0):
print("register $" + str(c) + " is " + str(registers[c]))
c = c + 1
print("MEMORY VALUES:")
k = 8192
while (k <= 12288):
if (memory[k] != 0):
print("memory:" + hex(k) + " is " + str(memory[k]))
k = k + 4
exit()
else:
pass
line = line.replace("\n", "") # Removes extra chars
line = line.replace("$", "")
line = line.replace(" ", "")
line = line.replace("zero", "0") # assembly can also use both $zero and $0
if (line[0:5] == "addiu"): # ADDIU
line = line.replace("addiu", "")
line = line.split(",")
imm = int(line[2]) # if (int(line[2]) > 0) else (65536 + int(line[2]))
rs = int(line[1])
rt = int(line[0])
registers[rt] = registers[rs] + imm
instCount = instCount + 1
ii += 1
elif (line[0:4] == "addi"): # ADDI
line = line.replace("addi", "")
line = line.split(",")
check = line[2]
if ((len(line[2]) >= 4) and (check[1] == 'x')): # if (int(line[2]) > 0) else (65536 + int(line[2]))
imm = int(check, 16)
else:
imm = int(check)
rs = int(line[1])
rt = int(line[0])
registers[rt] = registers[rs] + imm
print("TEST FOR ADDI")
print("The value in $" + str(rt) + " is " + str(registers[rt]))
instCount = instCount + 1
ii += 1
elif (line[0:3] == "add"): # ADD
line = line.replace("add", "")
line = line.split(",")
rd = int(line[0])
rs = registers[int(line[1])]
rt = registers[int(line[2])]
registers[rd] = rs + rt
# registers.insert(rd,rs + rt )
value = registers[rd]
print("TEST FOR ADD")
print("The value in $" + str(rd) + " is " + str(value))
instCount = instCount + 1
r += 1
elif (line[0:4] == "andi"): # ADDI
line = line.replace("andi", "")
line = line.split(",")
check = line[2]
if ((len(line[2]) >= 4) and (check[1] == 'x')): # if (int(line[2]) > 0) else (65536 + int(line[2]))
imm = int(check, 16)
else:
imm = int(check)
rs = int(line[1])
rt = int(line[0])
registers[rt] = registers[rs] & imm
elif (line[0:3] == "and"): # AND
line = line.replace("and", "")
line = line.split(",")
rd = int(line[0])
rs = registers[int(line[1])]
rt = registers[int(line[2])]
print(rs)
print(rt)
value = rs & rt
registers.insert(rd, value)
print("TEST FOR AND")
print("The value in $" + str(rd) + " is " + str(registers[rd]))
instCount = instCount + 1
r += 1
elif (line[0:3] == "ori"): # ORI
line = line.replace("ori", "")
line = line.split(",")
rd = int(line[0])
if ("x" in line[2]):
line[2] = line[2].replace("0x", "")
line[2] = format(int(line[2], 16))
rs = int(registers[int(line[1])])
rt = int(line[2])
value = rs | rt
registers[rd] = value
# registers.insert(rd,value)
print("TEST FOR ORI")
print("The value in $" + str(rd) + " is " + str(registers[rd]))
instCount = instCount + 1
ii += 1
elif (line[0:2] == "or"): # OR
line = line.replace("or", "")
line = line.split(",")
rd = int(line[0])
rs = registers[int(line[1])]
rt = registers[int(line[2])]
value = rs | rt
registers.insert(rd, value)
print("TEST FOR OR")
print("The value in $" + str(rd) + " is " + str(registers[rd]))
instCount = instCount + 1
r += 1
elif (line[0:3] == "xor"): # XOR
line = line.replace("xor", "")
line = line.split(",")
rd = int(line[0])
rs = registers[int(line[1])]
rt = registers[int(line[2])]
value = rs ^ rt
registers[rd] = value
print("TEST FOR XOR")
print("The value in $" + str(rd) + " is " + str(registers[rd]))
instCount = instCount + 1
r += 1
elif (line[0:3] == "sub"): # SUB
line = line.replace("sub", "")
line = line.split(",")
rd = int(line[0])
rs = registers[int(line[1])]
rt = registers[int(line[2])]
value = rs - rt
registers.insert(rd, value)
print("TEST FOR AND")
print("The value in $" + str(rd) + " is " + str(registers[rd]))
instCount = instCount + 1
r += 1
elif(line[0:5] == "multu"): # MULTU
print(line)
line = line.replace("multu","")
line = line.split(",")
print(line)
rs = registers[int(line[0])] #if (registers[int(line[0])] > 0) else (65536 + registers[int(line[0])])
rt = registers[int(line[1])] #if (registers[int(line[1])] > 0) else (65536 + registers[int(line[1])])
if rs < 0:
rs += 2**32
if rt < 0:
rt += 2**32
print('RS ',rs)
print('RT ',rt)
value = format(int(rs * rt),'064b')
hexval = format(hex(rs * rt))
vallo = value[32:64]
valhi = value[0:32]
print("TEST FOR MULTU")
print("The value in rs is " + str(rs) + " The value is rt is " + str(rt) + " Temp result is " + str(hexval))
print("lo = " + str(vallo) + " hi = " + str(valhi))
f.write(str('000000') + str(rs) + str(rt) + str('0000000000') + format(int('19',16),'06b')+ '\n')
instCount = instCount + 1
r += 1
elif (line[0:4] == "mult"): # MULT
line = line.replace("mult", "")
line = line.split(",")
rs = registers[int(line[0])]
rt = registers[int(line[1])]
value = rs * rt
if (value < 0):
bitresult = format(int(~value), '64b')
else:
bitresult = format(int(value), '64b')
bitresult = bitresult.replace(" ", "0")
if (value < 0):
bitresult = bitresult.replace("0", "?")
bitresult = bitresult.replace("1", "0")
bitresult = bitresult.replace("?", "1")
vallo = bitresult[33:65]
if (value < 0):
valtemp = vallo
valtemp = valtemp.replace("0", "?")
valtemp = valtemp.replace("1", "0")
valtemp = valtemp.replace("?", "1")
vallo = valtemp
valhi = bitresult[0:33]
print("TEST FOR MULT")
print("The value in rs is " + str(rs) + " The value is rt is " + str(rt) + " Temp result is " + str(
value) + " or " + str(bitresult))
print("lo = " + str(vallo) + " hi = " + str(valhi))
f.write(str('000000') + str(rs) + str(rt) + str('0000000000') + format(int('18', 16), '06b') + '\n')
instCount = instCount + 1
r += 1
elif(line[0:4] == "mfhi"): # MFHI
line = line.replace("mfhi", "")
line = line.split(",")
rd = int(line[0])
# print(rd)
# if(diff < 32):
# valhi = valhi.replace('0', '', diff-1)
# else:
# valhi == '0'
# print(valhi)
dechi = int(valhi, 2)
# print(dechi)
# if(valhi[0] == '1'):
# valtemp = valhi
# valtemp = valtemp.replace("0", "?")
# valtemp = valtemp.replace("1", "0")
# valtemp = valtemp.replace("?", "1")
# print(valtemp)
# dechi = ~int(valtemp, 2) + 1
# else:
# dechi = int(valhi, 2)
registers[rd] = dechi
print("TEST FOR MFHI")
print("The value in $" + str(rd) + " is " + str(dechi) + " repesented by " + str(valhi))
instCount = instCount + 1
elif(line[0:4] == "mflo"): # MFLO
line = line.replace("mflo", "")
line = line.split(",")
rd = int(line[0])
declo = int(vallo, 2)
registers[rd] = declo
print("TEST FOR MFLO")
print("The value in $" + str(rd) + " is " + str(declo) + " repesented by " + str(vallo))
instCount = instCount + 1
r += 1
elif (line[0:2] == "lw"): # lw
line = line.replace("lw", "")
line = line.replace("(", ",")
line = line.replace(")", "")
line = line.split(",")
rt = int(line[0])
mem = int(line[1], 16)
rs = int(line[2])
imm = registers[rs] + mem # adding the $rs + offset
registers.insert(rt, memory[imm])
print_mem = hex(imm)
print("TEST FOR LW");
print("$" + str(rt) + " is " + str(registers[rt]))
instCount = instCount + 1
ii += 1
elif (line[0:2] == "sw"): # sw
line = line.replace("sw", "")
line = line.replace("(", ",")
line = line.replace(")", "")
line = line.split(",")
print(line)
rt = int(line[0])
mem = int(line[1], 16)
print(mem)
rs = int(line[2])
print(rs)
print(registers[rs])
imm = registers[rs] + mem # adding the $rs + offset
memory.insert(imm, registers[rt])
print_mem = hex(imm)
print("TEST FOR SW")
print(imm)
print(print_mem)
print("memory location " + str(print_mem) + " is stored to $" + str(rt) + " with value of " + str(
memory[imm]))
instCount = instCount + 1
ii +=1
elif (line[0:3] == "slt"): # slt
line = line.replace("slt", "")
line = line.split(",")
rd = int(line[0])
rt = registers[int(line[1])]
rs = registers[int(line[2])]
if (rt < rs):
registers[rd] = 1
else:
registers[rd] = 0
print("TEST FOR SLT")
print("Is " + str(rt) + " less than " + str(rs) + " : " + str(registers[rd]))
instCount = instCount + 1
r += 1
# f.write(str('0000000') + str(rs) + str(rt) + str(rd) +str('00000')+ str(format(int('2a',16),'06b')) +'\n')
elif (line[0:3] == "srl"): # srl
line = line.replace("srl", "")
line = line.split(",")
rd = int(line[0])
try:
rt = registers[int(line[1])]
except:
try:
rt = registers[int(line[1], 16)]
except:
rt = int(line[1], 16)
shift = int(line[2])
print("TEST FOR SRL")
print(str(rt))
rt = bin(rt)
rt = rt.replace("b", "")
print(str(rt))
y = len(rt)
z = 0
rt = rt[0: y - shift]
for x in range(0, shift):
rt = '0' + rt
print(str(rt))
instCount = instCount + 1
r +=1
elif (line[0:3] == "lui"): # LUI
line = line.replace("lui", "")
line = line.split(",")
z = 0
print("TEST FOR LUI")
if ("x" in line[1]):
line[1] = line[1].replace("0x", "")
line[1] = format(int(line[1], 16))
rd = int(line[0])
imm = int(line[1])
print(str(imm))
imm = format(int(imm), '16b')
imm = imm.replace(" ", "0")
print(imm)
temp = format(int(registers[rd]), '16b')
temp = temp.replace(" ", "0")
print(temp)
temp = str(imm) + str(temp)
print(temp)
if (temp[0] == '1'):
newtemp = temp
newtemp = newtemp.replace("0", "?")
newtemp = newtemp.replace("1", "0")
newtemp = newtemp.replace("?", "1")
registers[rd] = format(~int(newtemp, 2))
else:
registers[rd] = format(int(temp, 2))
print(registers[rd])
instCount = instCount + 1
ii +=1
elif (line[0:2] == "lbu"): # lw
line = line.replace("lbu", "")
line = line.replace("(", ",")
line = line.replace(")", "")
line = line.split(",")
rt = int(line[0])
mem = int(line[1], 16)
rs = int(line[2])
imm = registers[rs] + mem # adding the $rs + offset
registers.insert(rt, memory[imm])
print_mem = hex(imm)
print("TEST FOR Lbu");
print("$" + str(rt) + " is " + str(registers[rt]))
instCount = instCount + 1
ii += 1
elif (line[0:2] == "lhu"): # lw
line = line.replace("lhu", "")
line = line.replace("(", ",")
line = line.replace(")", "")
line = line.split(",")
rt = int(line[0])
mem = int(line[1], 16)
rs = int(line[2])
imm = registers[rs] + mem # adding the $rs + offset
registers.insert(rt, memory[imm])
print_mem = hex(imm)
print("TEST FOR Lhu");
print("$" + str(rt) + " is " + str(registers[rt]))
instCount = instCount + 1
ii += 1
elif (line[0:3] == "sbu"): # sw
line = line.replace("sbu", "")
line = line.replace("(", ",")
line = line.replace(")", "")
line = line.split(",")
print(line)
rt = int(line[0])
mem = int(line[1], 16)
print(mem)
rs = int(line[2])
print(rs)
print(registers[rs])
imm = registers[rs] + mem # adding the $rs + offset
memory.insert(imm, registers[rt])
print_mem = hex(imm)
print("TEST FOR Sbu")
print(imm)
print(print_mem)
print("memory location " + str(print_mem) + " is stored to $" + str(rt) + " with value of " + str(
memory[imm]))
instCount = instCount + 1
elif (line[0:2] == "sb"): # sw
line = line.replace("sb", "")
line = line.replace("(", ",")
line = line.replace(")", "")
line = line.split(",")
print(line)
rt = int(line[0])
mem = int(line[1], 16)
print(mem)
rs = int(line[2])
print(rs)
print(registers[rs])
imm = registers[rs] + mem # adding the $rs + offset
memory.insert(imm, registers[rt])
print_mem = hex(imm)
print("TEST FOR Sb")
print(imm)
print(print_mem)
print("memory location " + str(print_mem) + " is stored to $" + str(rt) + " with value of " + str(
memory[imm]))
instCount = instCount + 1
ii += 1
elif (line[0:3] == "bne"): # Branch if not equal
line = line.replace("bne", "")
line = line.split(",")
try:
if 30 < int(line[0]):
rt = int(line[0])
else:
rt = registers[int(line[0])]
if 30 < int(line[1]):
rs = int(line[1])
else:
rs = registers[int(line[1])]
except:
rt = int(line[0])
rs = int(line[1])
name = str(line[2])
if (rs != rt):
lineUpdate = True # lets the program know that the line location has changed
z = labelName.index(name)
print("LABEL INDEX #" + str(z))
w = labelIndex[z]
print("line number" + str(w))
instCount = instCount + 2
ii += 1
elif (line[0:3] == "beq"): # Branch if not equal
line = line.replace("beq", "")
line = line.split(",")
try:
if 30 < int(line[0]):
rt = int(line[0])
else:
rt = registers[int(line[0])]
if 30 < int(line[1]):
rs = int(line[1])
else:
rs = registers[int(line[1])]
except:
rt = int(line[0])
rs = int(line[1])
name = str(line[2])
if (rs == rt): # Checks that rs and rt are not equal
lineUpdate = True # lets the program know that the line location has changed
z = labelName.index(name)
print("LABEL INDEX #" + str(z))
w = labelIndex[z]
print("line number" + str(w))
print(asm[w])
instCount = instCount + 2
ii += 1
elif (line[0:1] == "j"): # Jump function
line = line.replace("j", "")
#line = line.split(",")
name = str(line[0:])
lineUpdate = True # lets the program know that the line location has changed
z = labelName.index(name)
print("JUMP LABEL INDEX #" + str(z))
w = labelIndex[z]
print("JUMP line number" + str(w))
print(asm[w])
instCount = instCount + 2
j += 1
elif (line[0:4] == 'HASH'): #Special instruction, performs MULTU and XOR, skips MFHI and MFLO
line = line.replace('HASH', '')
line = line.split(",")
rd = int(line[0]) #save to this register
rt = registers[int(line[1])] #operand 1
rs = registers[int(line[2])] #operand 2
if rs < 0:
rs += 2**32
if rt < 0:
rt += 2**32
print('RS ',rs)
print('RT ',rt)
value = format(int(rs * rt),'064b')
hexval = format(hex(rs * rt))
print(str(rt) + " Times " + str(rs) + " is " + str(hexval))
hival = int(value[:32],2)
print(hival)
loval = int(value[32:],2)
print(loval)
xorval = hival ^ loval
registers[rd] = xorval
print("The folded result of xor is " + str(xorval) + " or " + str(hex(xorval)))
instCount = instCount + 1
print(registers)
print("\n\n")
i += 1
# print(registers)
f.close()
instCount += (len(labelName) * 1)
print("The instruction count is ", str(instCount))
print("For loop ended")
for i in range(len(registers)):
print("register $" + str(i) + str(register[i]))
j = 8192
while (j <= 12288):
print("memory:" + hex(i) + " is " + str(memory[j]))
j = j + 4
if __name__ == "__main__":
main()