-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assember.py
executable file
·233 lines (202 loc) · 7.38 KB
/
Assember.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
import re
import instfile
class Entry:
def __init__(self, string, token, attribute):
self.string = string
self.token = token
self.att = attribute
symtable = []
# print(symtable[12].string + ' ' + str(symtable[12].token) + ' ' + str(symtable[12].att))
def lookup(s):
for i in range(0,symtable.__len__()):
if s == symtable[i].string:
return i
return -1
def insert(s, t, a):
symtable.append(Entry(s,t,a))
return symtable.__len__()-1
def init():
for i in range(0,instfile.inst.__len__()):
insert(instfile.inst[i], instfile.token[i], instfile.opcode[i])
for i in range(0,instfile.directives.__len__()):
insert(instfile.directives[i], instfile.dirtoken[i], instfile.dircode[i])
file = open('input.sic', 'r')
filecontent = []
bufferindex = 0
tokenval = 0
lineno = 1
pass1or2 = 1
locctr = 0
lookahead = ''
startLine = True
total_size = 0
startAddress = -1
Xbit4set = 0x800000
Bbit4set = 0x400000
Pbit4set = 0x200000
Ebit4set = 0x100000
Nbitset = 2
Ibitset = 1
Xbit3set = 0x8000
Bbit3set = 0x4000
Pbit3set = 0x2000
Ebit3set = 0x1000
def is_hex(s):
if s[0:2].upper() == '0X':
try:
int(s[2:], 16)
return True
except ValueError:
return False
else:
return False
def lexan():
global filecontent, tokenval, lineno, bufferindex, locctr, startLine
while True:
# if filecontent == []:
if len(filecontent) == bufferindex:
return 'EOF'
elif filecontent[bufferindex] == '#':
startLine = True
while filecontent[bufferindex] != '\n':
bufferindex = bufferindex + 1
lineno += 1
bufferindex = bufferindex + 1
elif filecontent[bufferindex] == '\n':
startLine = True
# del filecontent[bufferindex]
bufferindex = bufferindex + 1
lineno += 1
else:
break
if filecontent[bufferindex].isdigit():
tokenval = int(filecontent[bufferindex]) # all number are considered as decimals
# del filecontent[bufferindex]
bufferindex = bufferindex + 1
return ('NUM')
elif is_hex(filecontent[bufferindex]):
tokenval = int(filecontent[bufferindex][2:], 16) # all number starting with 0x are considered as hex
# del filecontent[bufferindex]
bufferindex = bufferindex + 1
return ('NUM')
elif filecontent[bufferindex] in ['+', '#', ',']:
c = filecontent[bufferindex]
# del filecontent[bufferindex]
bufferindex = bufferindex + 1
return (c)
else:
# check if there is a string or hex starting with C'string' or X'hex'
if (filecontent[bufferindex].upper() == 'C') and (filecontent[bufferindex+1] == '\''):
bytestring = ''
bufferindex += 2
while filecontent[bufferindex] != '\'': # should we take into account the missing ' error?
bytestring += filecontent[bufferindex]
bufferindex += 1
if filecontent[bufferindex] != '\'':
bytestring += ' '
bufferindex += 1
bytestringvalue = "".join("%02X" % ord(c) for c in bytestring)
bytestring = '_' + bytestring
p = lookup(bytestring)
if p == -1:
p = insert(bytestring, 'STRING', bytestringvalue) # should we deal with literals?
tokenval = p
elif (filecontent[bufferindex] == '\''): # a string can start with C' or only with '
bytestring = ''
bufferindex += 1
while filecontent[bufferindex] != '\'': # should we take into account the missing ' error?
bytestring += filecontent[bufferindex]
bufferindex += 1
if filecontent[bufferindex] != '\'':
bytestring += ' '
bufferindex += 1
bytestringvalue = "".join("%02X" % ord(c) for c in bytestring)
bytestring = '_' + bytestring
p = lookup(bytestring)
if p == -1:
p = insert(bytestring, 'STRING', bytestringvalue) # should we deal with literals?
tokenval = p
elif (filecontent[bufferindex].upper() == 'X') and (filecontent[bufferindex+1] == '\''):
bufferindex += 2
bytestring = filecontent[bufferindex]
bufferindex += 2
# if filecontent[bufferindex] != '\'':# should we take into account the missing ' error?
bytestringvalue = bytestring
if len(bytestringvalue)%2 == 1:
bytestringvalue = '0'+ bytestringvalue
bytestring = '_' + bytestring
p = lookup(bytestring)
if p == -1:
p = insert(bytestring, 'HEX', bytestringvalue) # should we deal with literals?
tokenval = p
else:
p=lookup(filecontent[bufferindex].upper())
if p == -1:
if startLine == True:
p=insert(filecontent[bufferindex].upper(),'ID',locctr) # should we deal with case-sensitive?
else:
p=insert(filecontent[bufferindex].upper(),'ID',-1) #forward reference
else:
if (symtable[p].att == -1) and (startLine == True):
symtable[p].att = locctr
tokenval = p
# del filecontent[bufferindex]
bufferindex = bufferindex + 1
return (symtable[p].token)
def error(s):
global lineno
print('line ' + str(lineno) + ': '+s)
def match(token):
global lookahead
if lookahead == token:
lookahead = lexan()
else:
error('Syntax error')
def checkindex():
global bufferindex, symtable, tokenval
if lookahead == ',':
match(',')
if symtable[tokenval].att != 1:
error('index regsiter should be X')
match('REG')
return True
return False
def parse():
# write the parser here
header()
body()
# Header function
def header():
global locctr, tokenval,total_size, symtable, pass1or2, lookahead, startLine
lookahead = lexan()
position_in_symtable = tokenval
match('ID')
match('START')
# here we will set the location as att in symtable and where we're going to start in address
startAddress = locctr = symtable[position_in_symtable].att = tokenval
match('NUM')
if pass1or2 == 2:
print('H ', symtable[position_in_symtable].string, format(startAddress, '06X'), format(total_size, '06X'))
def main():
global file, filecontent, locctr, pass1or2, bufferindex, lineno
init()
w = file.read()
filecontent=re.split("([\W])", w)
i=0
while True:
while (filecontent[i] == ' ') or (filecontent[i] == '') or (filecontent[i] == '\t'):
del filecontent[i]
if len(filecontent) == i:
break
i += 1
if len(filecontent) <= i:
break
if filecontent[len(filecontent)-1] != '\n': #to be sure that the content ends with new line
filecontent.append('\n')
for pass1or2 in range(1,3):
parse()
bufferindex = 0
locctr = 0
lineno = 1
file.close()
main()