-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm.py
More file actions
305 lines (237 loc) · 8.6 KB
/
asm.py
File metadata and controls
305 lines (237 loc) · 8.6 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
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
import re
import struct
import sys
REGS = {f"R{i}": i for i in range(16)} | {"SP": 14, "PC": 15}
OPCODES = {
"NOP":0x00,
"MOV":0x01,"LD":0x02,"ST":0x03,"LDI":0x04,
"ADD":0x05,"SUB":0x06,
"JMP":0x07,"JZ":0x08,"JNZ":0x09,
"PUSH":0x0A,"POP":0x0B,
"CALL":0x0C,"RET":0x0D,
"INT":0x0E,"IRET":0x0F,"EI":0x10,"DI":0x11,
"AND":0x12,"OR":0x13,"XOR":0x14,
"SHL":0x15,"SHR":0x16,
"CMP":0x17,"TEST":0x18,
"NOT":0x19,"INC":0x1A,"DEC":0x1B,"NEG":0x1C,
"PUSHI":0x1D,
"JR":0x1E,"JZR":0x1F,"JNZR":0x20,"JC":0x21,"JNC":0x22,"JRI":0x23,
"LEA":0x24,"STI":0x25,
"LDB":0x26,"STB":0x27,
"CPUID":0xFD,"RESET":0xFE,"HALT":0xFF
}
def parse_number(x):
x = x.strip()
if x.startswith("0x"):
return int(x,16)
if x.startswith("0b"):
return int(x,2)
return int(x)
def tokenize(line):
line = line.split(";")[0].strip()
if not line:
return []
return re.findall(r'\[.*?\]|".*?"|[^,\s]+', line)
class Assembler:
def __init__(self):
self.symbols={}
self.output=bytearray()
self.pc=0
self.lines=[]
def load(self, text):
self.lines=text.splitlines()
def set_pc(self,newpc):
if newpc < len(self.output):
raise Exception("ORG overlaps existing code")
while len(self.output) < newpc:
self.output.append(0)
self.pc=newpc
def estimate_db_size(self,items):
size=0
for it in items:
if it.startswith('"') and it.endswith('"'):
size+=len(it.strip('"').encode())
else:
size+=1
return size
def pass1(self):
self.pc=0
for lineno,line in enumerate(self.lines,1):
line=line.split(";")[0].strip()
if not line:
continue
if line.endswith(":"):
label=line[:-1]
if label in self.symbols:
raise Exception(f"line {lineno}: duplicate label {label}")
self.symbols[label]=self.pc
continue
tokens=tokenize(line)
if not tokens:
continue
op=tokens[0].upper()
if op==".ORG":
self.pc=parse_number(tokens[1])
continue
if op==".WORD":
self.pc+=2
continue
if op==".BYTE":
self.pc+=1
continue
if op==".DB":
self.pc+=self.estimate_db_size(tokens[1:])
continue
if op==".STRING":
s=line.split(".STRING",1)[1].strip().strip('"')
self.pc+=len(s.encode())
continue
self.pc+=4
def reg(self,r,lineno):
r=r.upper()
if r not in REGS:
raise Exception(f"line {lineno}: invalid register {r}")
return REGS[r]
def imm(self,x,lineno):
if x in self.symbols:
return self.symbols[x]
try:
return parse_number(x)
except ValueError:
raise Exception(f"line {lineno}: undefined symbol {x}")
def encode_R(self,op,d,s1=0,s2=0):
return (op<<24)|(d<<20)|(s1<<16)|(s2<<12)
def encode_I(self,op,d,s1,imm):
if not -32768 <= imm <= 65535:
raise Exception(f"immediate out of range: {imm}")
return (op<<24)|(d<<20)|(s1<<16)|(imm & 0xFFFF)
def emit32(self,val):
self.output += struct.pack(">I",val)
self.pc+=4
def emit16(self,val):
self.output += struct.pack(">H",val)
self.pc+=2
def emit8(self,val):
self.output.append(val & 0xFF)
self.pc+=1
def pass2(self):
self.pc=0
for lineno,line in enumerate(self.lines,1):
line=line.split(";")[0].strip()
if not line:
continue
if line.endswith(":"):
continue
tokens=tokenize(line)
if not tokens:
continue
op=tokens[0].upper()
if op==".ORG":
self.set_pc(parse_number(tokens[1]))
continue
if op==".WORD":
self.emit16(self.imm(tokens[1],lineno))
continue
if op==".BYTE":
self.emit8(self.imm(tokens[1],lineno))
continue
if op==".DB":
for item in tokens[1:]:
if item.startswith('"') and item.endswith('"'):
for c in item.strip('"').encode():
self.emit8(c)
else:
self.emit8(self.imm(item,lineno))
continue
if op==".STRING":
s=line.split(".STRING",1)[1].strip().strip('"')
for c in s.encode():
self.emit8(c)
continue
if op not in OPCODES:
raise Exception(f"line {lineno}: unknown instruction {op}")
opcode=OPCODES[op]
if op in ["NOP","RET","IRET","EI","DI","CPUID","RESET","HALT"]:
self.emit32(self.encode_R(opcode,0,0,0))
elif op=="MOV":
d=self.reg(tokens[1],lineno)
s=self.reg(tokens[2],lineno)
self.emit32(self.encode_R(opcode,d,s,0))
elif op in ["LD","LDB"]:
d=self.reg(tokens[1],lineno)
s=self.reg(tokens[2].strip("[]"),lineno)
self.emit32(self.encode_R(opcode,d,s,0))
elif op in ["ST","STB"]:
d=self.reg(tokens[1].strip("[]"),lineno)
s=self.reg(tokens[2],lineno)
self.emit32(self.encode_R(opcode,d,s,0))
elif op in ["ADD","SUB","AND","OR","XOR"]:
d=self.reg(tokens[1],lineno)
s1=self.reg(tokens[2],lineno)
s2=self.reg(tokens[3],lineno)
self.emit32(self.encode_R(opcode,d,s1,s2))
elif op in ["CMP","TEST"]:
s1=self.reg(tokens[1],lineno)
s2=self.reg(tokens[2],lineno)
self.emit32(self.encode_R(opcode,0,s1,s2))
elif op in ["NOT","NEG"]:
d=self.reg(tokens[1],lineno)
s=self.reg(tokens[2],lineno)
self.emit32(self.encode_R(opcode,d,s,0))
elif op in ["INC","DEC"]:
d=self.reg(tokens[1],lineno)
self.emit32(self.encode_R(opcode,d,0,0))
elif op=="PUSH":
s=self.reg(tokens[1],lineno)
self.emit32(self.encode_R(opcode,0,s,0))
elif op=="POP":
d=self.reg(tokens[1],lineno)
self.emit32(self.encode_R(opcode,d,0,0))
elif op in ["JR","JZR","JNZR","JC","JNC"]:
s=self.reg(tokens[1],lineno)
self.emit32(self.encode_R(opcode,0,s,0))
elif op in ["JMP","JZ","JNZ","CALL","INT"]:
imm=self.imm(tokens[1],lineno)
self.emit32(self.encode_I(opcode,0,0,imm))
elif op=="LDI":
d=self.reg(tokens[1],lineno)
imm=self.imm(tokens[2],lineno)
self.emit32(self.encode_I(opcode,d,0,imm))
elif op=="PUSHI":
imm=self.imm(tokens[1],lineno)
self.emit32(self.encode_I(opcode,0,0,imm))
elif op=="JRI":
target=tokens[1]
if target in self.symbols:
offset=self.symbols[target]-self.pc
else:
offset=self.imm(target,lineno)
self.emit32(self.encode_I(opcode,0,0,offset))
elif op in ["SHL","SHR"]:
d=self.reg(tokens[1],lineno)
s=self.reg(tokens[2],lineno)
imm=self.imm(tokens[3],lineno)
self.emit32(self.encode_I(opcode,d,s,imm))
elif op=="LEA":
d=self.reg(tokens[1],lineno)
s=self.reg(tokens[2],lineno)
imm=self.imm(tokens[3],lineno)
self.emit32(self.encode_I(opcode,d,s,imm))
elif op=="STI":
d=self.reg(tokens[1],lineno)
imm=self.imm(tokens[2],lineno)
self.emit32(self.encode_I(opcode,d,0,imm))
def assemble(self,text):
self.load(text)
self.pass1()
self.pass2()
return self.output
if __name__=="__main__":
asm=Assembler()
with open(sys.argv[1],"r") as f:
code=f.read()
binary=asm.assemble(code)
out=sys.argv[1]+".bin"
with open(out,"wb") as f:
f.write(binary)
print(f"Assembled code written to {out}")