-
Notifications
You must be signed in to change notification settings - Fork 1
/
ques_4.py
109 lines (96 loc) · 3.75 KB
/
ques_4.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
i , R0 , R1 , R2 , R3 , R4 , R5 , R6 , FLAGS =0, 0 , 0 , 0 , 0 , 0 , 0 , 0 , [0 for k in range (16)]
reg = {
"000": globals()["R0"],
"001": globals()["R1"],
"010": globals()["R2"],
"011": globals()["R3"],
"100": globals()["R4"],
"101": globals()["R5"],
"110": globals()["R6"],
"111": globals()["FLAGS"]
}
l = ['D:\\test1.txt', 'D:\\test2.txt', 'D:\\test3.txt', 'D:\\test4.txt',
'D:\\test5.txt' ]
for file in l:
f = open(file)
command_list = f.readlines()
l = len(command_list)
for i in range(l):
command_list[i] = command_list[i].strip()
variables = []
def decimal(n): # converts from binary to decimal
num, value , base = int( n ) , 0 , 1
tmp = num
while( tmp != 0 ):
l = tmp % 10
tmp = tmp // 10
value = value + l * base
base *= 2
return value
def decf_simulator(instr): #Subtracts 1 from the given register and stores the value
source_reg = reg[instr[10:13]]
dest_reg = instr[13:16]
reg[dest_reg] = source_reg - 1
return i + 1
def decf_assembly(source_reg,dest_reg):
machinecode = "1000000000" + source_reg + dest_reg + "\n"
print(machinecode)
def incf_simulator(instr): #Adds 1 in the given register and stores the value
source_reg = reg[instr[10:13] ]
dest_reg = instr[13:16]
reg[dest_reg] = source_reg + 1
return i + 1
def incf_assembly(source_reg,dest_reg):
machinecode = "1000010000" + source_reg + dest_reg + "\n"
print(machinecode)
def comf_simulator(instr): #Stores 1's complement of the given register
source_reg = str(reg[instr[10:13]])
s = ''
for ch in source_reg:
if ch == '1':
s += '0'
else:
s += '1'
dest_reg = instr[13:16]
reg[dest_reg] = int(s)
return i + 1
def comf_assembly(source_reg,dest_reg):
machinecode = "1000100000" + source_reg + dest_reg + "\n"
print(machinecode)
def decfz_simulator(instr): #Jumps to the memory address if register contains 1
reg1 = reg[instr[6:9]]
reg[reg1] = reg1 - 1
mem_addr = instr[10:]
if reg[reg1] == 0:
return decimal(mem_addr)
else:
return i + 1
def decfz_assembly(source_reg,mem_address):
machinecode = "100011" + source_reg + mem_address + "\n"
print(machinecode)
def incfz_simulator(instr): #Jumps to the memory address if register contains 0
reg1 = reg[instr[6:9]]
reg[reg1] = reg1 + 1
mem_addr = instr[10:]
if reg[reg1] == 1:
return decimal(mem_addr)
else:
return i + 1
def incfz_assembly(source_reg,mem_address):
machinecode = "100100" + source_reg + mem_address + "\n"
print(machinecode)
opcode = {"decf": "100000","incf": "100001","comf": "100010","decfz": "100011","incfz": "100000"}
registers_dict = {"R0": "000", "R1": "001", "R2": "010","R3": "011","R4": "100","R5": "101","R6": "110"}
for instr in command_list:
if instr != []:
op=instr[0]
if op == "decf":
decf_assembly(registers_dict.get(instr[1]),registers_dict.get(instr[2]))
elif op == "incf":
incf_assembly(registers_dict.get(instr[1]),registers_dict.get(instr[2]))
elif op == "mul":
comf_assembly(registers_dict.get(instr[1]),registers_dict.get(instr[2]))
elif op == "xor":
decfz_assembly(registers_dict.get(instr[1]),registers_dict.get(instr[2]))
elif op == "or":
incfz_assembly(registers_dict.get(instr[1]),registers_dict.get(instr[2]))