-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipCodeConverter.py
More file actions
121 lines (118 loc) · 4.05 KB
/
BipCodeConverter.py
File metadata and controls
121 lines (118 loc) · 4.05 KB
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
'''Código criado para interpretação de comandos do BIP2'''
class BipCodeConverter(object):
def __init__(self,fileName= 'codigo.b2'):
self.fileName= fileName
self.variables=[]
self.data =[]
self.file = open('codigo.b2', 'r')
def start(self):
temp=[]
for line in self.file:
line=line.replace('\n','')#.replace(' ','')
if(line=='.data'):
temp=[]
continue
elif(line=='.text'):
self.setVariableAddress(temp)
temp=[]
continue
temp.append(line)
code =self.findProcedure(temp) #procura procedimentos
self.generateBinaryCode(code)
# print(self.variables)
# print(code)
def generateBinaryCode(self,code):
listTemp=[]
opcode=''
value=''
file = open('programMemory.list', 'a')
for cmd in code:
temp= cmd.split(' ')
for i in temp:
if(i != ''):
listTemp.append(i)
opcode,value=self.generateBinaryValues(listTemp)
print('OP:%s |Value:%s'%(str(opcode),str(value)))
file.write(opcode+value+'\n')
listTemp=[]
# print(len(code))
# self.completeFile(len(code))
def completeFile(self,sizeActual,sizeDefault=1024):
quant= sizeDefault-sizeActual
file = open('programMemory.list', 'a')
for i in range(quant):
file.write(('0'*16)+'\n')
def generateBinaryValues(self,list):
# print(list)
# print(self.variables)
op=''
value=self.searchProcedure(list[1])
if(list[0]=='HLT'):
op='00000'
if (list[0] == 'STO'):
op = '00001'
if (list[0] == 'LD'):
op = '00010'
if (list[0] == 'LDI'):
op = '00011'
if (list[0] == 'ADD'):
op = '00100'
if (list[0] == 'ADDI'):
op = '00101'
if (list[0] == 'SUB'):
op = '00110'
if (list[0] == 'SUBI'):
op = '00111'
if (list[0] == 'BEQ'):
op = '01000'
if (list[0] == 'BNE'):
op = '01001'
if (list[0] == 'BGT'):
op = '01010'
if (list[0] == 'BGE'):
op = '01011'
if (list[0] == 'BLT'):
op = '01100'
if (list[0] == 'BLE'):
op = '01101'
if (list[0] == 'JMP'):
op = '01110'
return op,value
def searchProcedure(self,search):
for elemento in self.variables:
temp=elemento.get(search)
if(temp!=None):
# print('Procedimento: %s local: %s'%(str(search),str(temp)))
return temp
return self.convertToBinaryFromDec(int(search))
def findProcedure(self,lista):
code=[]
for posicao in range(len(lista)):
if(lista[posicao].find(':')>=1):
self.setProcedureAddress(lista[posicao],(posicao-1))
continue
code.append(lista[posicao])
return code
def setVariableAddress(self,variables):
# >> > ' '.join(format(ord(x), 'b') for x in st)
for temp in variables:
name,value= str(temp).split(':')
name=name.replace(' ','')
value= self.convertToBinaryFromDec(int(value))
self.variables.append({name:str(value)})
def setProcedureAddress(self,name,value):
name= str(name).replace(':','').replace(' ', '')
value= self.convertToBinaryFromDec(value)
self.variables.append({name:str(value)})
def convertToBinaryFromHex(self,value):
value=int(value, 16)#converte de hexadecimal para decimal
return format(value, 'b') #covnerte para binario
def convertToBinaryFromDec(self,value):
# print(value)
if(value==-1):
value=int('0',16)
retorno= format(value, 'b')
retorno = ((11-len(retorno))*'0')+retorno
return retorno#converte para binario
# temp = BipCodeInterpreter()
# temp.start()