-
Notifications
You must be signed in to change notification settings - Fork 4
/
assembler.rb
executable file
·396 lines (323 loc) · 6.58 KB
/
assembler.rb
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
#!/usr/bin/ruby
Text = []
Labels = []
Statics = {}
Consts = {}
Vars = {}
Regs = {}
Literals = {}
def make_label(name = "")
name = name.to_s
name = "label_%d" % Labels.size if name.empty?
Labels.push name
return name
end
def label(name) Text.push "#" + name.to_s end
def code(value) Text.push " %s" % value end
def literal(value)
name = "str_%d" % Literals.size
Literals[name] = value
return name
end
def static(*names)
names.each { |name| Statics[name] = 0 }; names
end
def var(count = 1)
names = []
count.times { Vars[(names.push "var_%d" % Vars.size).last] = 0 }
return names
end
def init_register(name,value)
Regs[name] = value
return name
end
def const(value)
return Consts[value] if Consts.has_key? value
Consts[value] = "const_%d" % Consts.size
end
IP = init_register("ip",0)
SHIFT_REG = init_register("shift_reg",1)
SP = init_register("sp",2)
REG0 = init_register("reg0",3)
REG1 = init_register("reg1",4)
REG2 = init_register("reg2",5)
REG3 = init_register("reg3",6)
CARRY_REG, ZERO_REG = var 2
def NOR(a, b, r)
code a
code b
code r
end
def NOT(a, r)
NOR a, a, r
end
def OR(a, b, r)
t = static :OR_t
NOR a, b, t
NOT t, r
end
def AND(a, b, r)
t1, t2 = static :AND_t1, :AND_t2
NOT a, t1
NOT b, t2
OR t1, t2, t1
NOT t1, r
end
def ANDi(a, imm, r)
t = static :ANDi_t
MOVi imm, t
AND a, t, r
end
def XOR(a, b, r)
t1, t2 = static :XOR_t1, :XOR_t2
NOT a, t1
NOT b, t2
AND a, t2, t2
AND b, t1, t1
OR t1, t2, r
end
def XORi(a, imm, r)
t = static :XORi_t
MOVi imm, t
XOR a, t, r
end
def MOV(a, b)
OR a, a, b
end
def JMP(a)
MOV a, IP
end
def JMPi(a)
JMP const(a)
end
def MOVi(imm, a)
MOV const(imm), a
end
# [a] -> b
def PEEK(a, b)
l1 = make_label
l2 = make_label
MOV a, l1
MOV a, l2
t = static :PEEK_t
# BEGIN: NOR 0, 0, t
label l1
code const(0) # <- a [initially '0']
label l2
code const(0) # <- a [initialli '0']
code t
# END: NOT 0, 0, t
NOT t, b
end
# a -> [b]
def POKE(a, b)
l = make_label
MOV b, l
t = static :POKE_t
NOT a, t
# BEGIN: NOR t, t, 0
code t
code t
label l
code const(0) # <- b [initially '0']
# END: NOR t, t, 0
end
def PUSH(a)
POKE a,SP
ADDi SP, 1, SP
end
def POP(a)
ADDi SP, -1, SP
PEEK SP, a
end
def CALL(prog)
label_call = make_label
call_t = static :CALL_t
MOV SP, call_t
PUSH const(label_call)
PUSH call_t
JMPi prog
label label_call
end
def RET
ret_t1,ret_t2 = static :RET_t1, :RET_t2
POP ret_t1
POP ret_t2
MOV ret_t1, SP
MOV ret_t2, IP
end
def EXITi
MOVi 0xFFFF, IP
end
def FADD(mask, carry, a, b, r)
tmp_a, tmp_b, bit_r = static :FADD_a, :FADD_b, :FADD_bit_r
t1, t2 = static :FADD_t1, :FADD_t2
AND a, mask, tmp_a # zeroing bits in 'a' except mask'ed
AND b, mask, tmp_b # zeroing bits in 'b' except mask'ed
AND carry, mask, carry # zeroing bits in 'carry' except mask'ed
# SUM = (a ^ b) ^ carry
XOR a, b, t1
XOR t1, carry, bit_r
# Leave only 'mask'ed bit in bit_r.
AND bit_r, mask, bit_r
# Add current added bit to the result.
OR bit_r, r, r
# CARRY = (a & b) | (carry & (a ^ b))
AND a, b, t2
AND carry, t1, t1
# CARRY is calculated, and 'shift_reg' contains the same value
# but shifted to the left by 1 bit.
OR t2, t1, carry
# CARRY is shifted to the left by 1 bit for the next round.
MOV SHIFT_REG, carry
# shift_reg = mask << 1
MOV mask, mask
# mask = shift (in fact, "mask = mask << 1")
MOV SHIFT_REG, mask
AND carry, mask, carry
end
def ZERO(a)
XOR a, a, a
end
def ADC(a, b, r)
mask, t = static :ADC_mask, :ADC_t
ZERO t
MOVi 0x0001, mask
16.times { FADD mask, CARRY_REG, a, b, t }
MOV t, r
ZERO t
16.times do
OR t, CARRY_REG, t
MOV CARRY_REG, CARRY_REG
MOV SHIFT_REG, CARRY_REG
end
MOV t, CARRY_REG
end
def ADD(a, b, r)
ZERO CARRY_REG
ADC a, b, r
end
def ADDi(a, imm, r)
t = static :ADDi_t
MOVi imm, t
ADD a, t, r
end
# Jump 'a', if cond = FFFF, and 'b' if cond = 0000
def BRANCH(a, b, cond)
t1, t2 = static :BRANCH_t1, :BRANCH_t2
AND a, cond, t1 # t1 = a & cond
NOT cond, t2 # t2 = !cond
AND b, t2, t2 # t2 = b & t2 = b & !cond
OR t1, t2, IP # ip = (a & cond) | (b & !cond)
end
# Jump 'a', if cond = FFFF, and 'b' if conf = 0000
def BRANCHi(a, b, cond)
BRANCH const(a), const(b), cond
end
# if a != 0 -> zero = FFFF else zero = 0000
def IS_0(a)
t = static :IS_0_t
ZERO CARRY_REG
ADC a, const(0xFFFF), t
NOT CARRY_REG, ZERO_REG
end
def JEQi(a,imm,target)
ADD a, const(-imm), REG1
IS_0(REG1)
JZi(target)
end
def JNEQi(a,imm,target)
ADD a, const(-imm), REG1
IS_0(REG1)
JNZi(target)
end
# ip = (zero_reg == FFFF ? a : ip)
def JZi(a)
bypass = make_label
BRANCHi a, bypass, ZERO_REG
label bypass
end
# ip = (zero_reg == FFFF ? a : ip)
def JNZi(a)
bypass = make_label
BRANCHi bypass, a, ZERO_REG
label bypass
end
def ROL(a, b)
MOV a, a # shift_reg = a << 1
MOV SHIFT_REG, b
end
def ROR(a, b)
t = static :ROR_t
MOV a, t
15.times { ROL t, t }
MOV t, b
end
def SHL(a, b)
ROL a, b
ANDi b, 0xFFFE, b
end
def SHR(a, b)
ROR a, b
ANDi b, 0x7FFF, b
end
class Assembler
def self.parse(asm_file)
load asm_file
end
def self.dump(obj_file)
assembly = []
Regs.each {|k,v| assembly.push 0 }
Text.each { |x| assembly.push x }
Vars.each { |k, v| assembly.push "#%s" % k, v }
Statics.each { |k, v| assembly.push "#%s" % k, v }
Consts.each { |k, v| assembly.push "#%s" % v, k }
Literals.each do |k, v|
assembly.push "#%s" % k
v.each_byte { |x| assembly.push x }
end
offset = 0
labels = {}
assembly.each do |x|
x = x.to_s
if x.start_with? '#' then
labels[x[1..-1]] = offset
else
offset = offset + 1
end
end
#debugger
# Remove all list having labels.
assembly.delete_if { |x| x.to_s.start_with? "#" }
Regs.each { |k, v| labels[k]=v }
assembly[Regs[SP]] = assembly.size
# Substitute labels by values.
assembly.collect! do |x|
if x.class==String || x.class==Symbol
labels[x.to_s.strip]
else
x
end
end
#debugger
#assembly[labels[SP]] = assembly.size
#puts assembly
File.open(obj_file,"wb") do |f|
f << assembly.pack("S*")
end
end
def self.asm(asm_file, obj_file="a.out")
Assembler.parse(asm_file)
Assembler.dump(obj_file)
end
end
unless $embedded
if ARGV.size==0
puts "usage: ruby #{__FILE__} asm_file [obj_file]"
exit
elsif ARGV.size==1
Assembler.asm(ARGV[0])
else
Assembler.asm(ARGV[0],ARGV[1])
end
end