-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilo.s
405 lines (388 loc) · 9.4 KB
/
ilo.s
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
396
397
398
399
400
401
402
403
404
405
/* AMD64 ilo, (c) 2023 Christopher Leonard, MIT License */
.global _start
/* rax: top of stack */
/* rbx: data stack */
/* rbp: jump table address */
/* r12: address stack */
/* r13: instruction pointer */
/* r14: opcode shift register */
/* r15: memory */
.bss
.align 8
blocks: .skip 8 /* name of blocks file (ilo.blocks) */
rom: .skip 8 /* name of image (ilo.rom) */
dstack: .skip 32*4
astack: .skip 256*4
a: .skip 4 /* other variables for misc. purposes */
b: .skip 4
f: .skip 4
s: .skip 4
d: .skip 4
l: .skip 4
memory: .skip 65536*4
.section .rodata
default_blocks:
.asciz "ilo.blocks"
default_rom:
.asciz "ilo.rom"
io_table:
.byte 0
.byte iob-ioa
.byte ioc-ioa
.byte iod-ioa
.byte ioe-ioa
.byte iof-ioa
.byte iog-ioa
.byte ioh-ioa
.text
rdonly: mov $2, %eax /* sys_open */
xor %esi, %esi /* O_RDONLY */
mov $0666, %edx
syscall
mov %eax, %edi
ret
wronly: mov $2, %eax /* sys_open */
mov $1, %esi /* O_WRONLY */
mov $0666, %edx
syscall
mov %eax, %edi
ret
close: mov $3, %eax /* sys_close */
syscall
ret
load_image:
mov rom(%rip), %rdi
call rdonly
or %eax, %eax
jz 1f
mov %r15, %rsi
mov $65536 * 4, %edx
xor %eax, %eax /* sys_read */
syscall
call close
xor %eax, %eax
lea dstack-4(%rip), %rbx
lea astack-4(%rip), %r12
xor %r13d, %r13d
1: ret
save_image:
push %rax
mov rom(%rip), %rdi
call wronly
or %eax, %eax
jz 1f
mov %r15, %rsi
mov $65536 * 4, %edx
mov $1, %eax
syscall
call close
1: pop %rax
ret
block_common:
mov $8, %eax /* sys_lseek */
mov (%rbx), %esi
shl $12, %esi
xor %edx, %edx /* SEEK_SET */
syscall
xor %eax, %eax /* sys_read */
or %r10b, %r10b
jz 1f
mov $1, %al /* sys_write */
1: lea (%r15,%r8,4), %rsi
mov $4096, %edx
syscall
mov -4(%rbx), %esi
sub $8, %rbx
ret
.align 32
table: ret
.align 32
li: add $4, %rbx
inc %r13d
mov %eax, (%rbx)
mov (%r15,%r13,4), %eax
ret
.align 32
du: add $4, %rbx
mov %eax, (%rbx)
ret
.align 32
dr: mov (%rbx), %eax
sub $4, %rbx
ret
.align 32
sw: xchg (%rbx), %eax
ret
.align 32
pu: add $4, %r12
mov %eax, (%r12)
mov (%rbx), %eax
sub $4, %rbx
ret
.align 32
po: add $4, %rbx
mov %eax, (%rbx)
mov (%r12), %eax
sub $4, %r12
ret
.align 32
ju: lea -1(%eax), %r13d
mov (%rbx), %eax
sub $4, %rbx
ret
.align 32
ca: add $4, %r12
mov %r13d, (%r12)
lea -1(%eax), %r13d
mov (%rbx), %eax
sub $4, %rbx
ret
.align 32
cc: cmpl $0, (%rbx)
jz 1f
add $4, %r12
mov %r13d, (%r12)
lea -1(%eax), %r13d
1: mov -4(%rbx), %eax
sub $8, %rbx
ret
.align 32
cj: cmpl $0, (%rbx)
jz 1f
lea -1(%eax), %r13d
1: mov -4(%rbx), %eax
sub $8, %rbx
ret
.align 32
re: mov (%r12), %r13d
sub $4, %r12
ret
.align 32
eq: cmp %eax, (%rbx)
sete %al
movzbl %al, %eax
neg %eax
sub $4, %rbx
ret
.align 32
ne: cmp %eax, (%rbx)
setne %al
movzbl %al, %eax
neg %eax
sub $4, %rbx
ret
.align 32
lt: cmp %eax, (%rbx)
setl %al
movzbl %al, %eax
neg %eax
sub $4, %rbx
ret
.align 32
gt: cmp %eax, (%rbx)
setg %al
movzbl %al, %eax
neg %eax
sub $4, %rbx
ret
.align 32
fe: mov (%r15,%rax,4), %eax
ret
.align 32
st: mov (%rbx), %ecx
mov %ecx, (%r15,%rax,4)
mov -4(%rbx), %eax
sub $8, %rbx
ret
.align 32
ad: add (%rbx), %eax
sub $4, %rbx
ret
.align 32
su: sub (%rbx), %eax
neg %eax
sub $4, %rbx
ret
.align 32
mu: mull (%rbx)
sub $4, %rbx
ret
.align 32
di: mov %eax, %ecx
mov (%rbx), %eax
cdq
idiv %ecx
mov %edx, (%rbx)
ret
.align 32
an: and (%rbx), %eax
sub $4, %rbx
ret
.align 32
or: or (%rbx), %eax
sub $4, %rbx
ret
.align 32
xo: xor (%rbx), %eax
sub $4, %rbx
ret
.align 32
sl: mov %eax, %ecx
mov (%rbx), %eax
sub $4, %rbx
shl %cl, %eax
ret
.align 32
sr: mov %eax, %ecx
mov (%rbx), %eax
sub $4, %rbx
sar %cl, %eax
ret
.align 32
cp: mov %eax, %ecx
mov (%rbx), %edi
mov -4(%rbx), %esi
sub $8, %rbx
lea (%r15,%rdi,4), %rdi
lea (%r15,%rsi,4), %rsi
cmp %eax, %eax
repe cmpsd
sete %al
movzbl %al, %eax
neg %eax
ret
.align 32
cy: mov %eax, %ecx
mov (%rbx), %edi
mov -4(%rbx), %esi
mov -8(%rbx), %eax
sub $12, %rbx
lea (%r15,%rdi,4), %rdi
lea (%r15,%rsi,4), %rsi
repe movsd
ret
.align 32
io: mov %eax, %ecx
mov (%rbx), %eax
sub $4, %rbx
cmp $7, %ecx
ja 1f
lea io_table(%rip), %rdx
movzbl (%rdx,%rcx), %ecx
lea ioa(%rip), %rdx
add %rdx, %rcx
jmp *%rcx
1: ret
ioa: push %rax
mov $1, %eax /* sys_write */
mov %eax, %edi
mov %rsp, %rsi
mov %eax, %edx
syscall
pop %rax
mov (%rbx), %eax
sub $4, %rbx
ret
iob: add $4, %rbx
mov %eax, (%rbx)
xor %eax, %eax /* sys_read */
push %rax
xor %edi, %edi
mov %rsp, %rsi
mov $1, %edx
syscall
pop %rax
ret
ioc: mov %eax, %r8d
mov blocks(%rip), %rdi
call rdonly
xor %r10d, %r10d
call block_common
call close
mov %esi, %eax
ret
iod: mov %eax, %r8d
mov blocks(%rip), %rdi
call wronly
mov $1, %r10b
call block_common
call close
mov %esi, %eax
ret
ioe: jmp save_image
iof: push %rax
call load_image
dec %r13d
pop %rax
ret
iog: mov $65536, %r13d
ret
ioh: add $8, %rbx
mov %eax, -4(%rbx)
lea astack-4(%rip), %rdx
neg %edx
lea 4*32-8(%rbx,%rdx), %rax
shr $2, %eax
mov %eax, (%rbx)
lea (%r12,%rdx), %rax
shr $2, %eax
ret
_start: xor %eax, %eax
lea memory(%rip), %r15
lea dstack-4(%rip), %rbx
lea astack-4(%rip), %r12
xor %r13d, %r13d
lea table(%rip), %rbp
mov 8(%rsp), %rcx
or %rcx, %rcx
jz 1f
mov 16(%rsp), %rcx
or %rcx, %rcx
jz 1f
mov %rcx, blocks(%rip)
mov 24(%rsp), %rcx
or %rcx, %rcx
jz 2f
mov %rcx, rom(%rip)
jmp 3f
1: lea default_blocks(%rip), %rcx
mov %rcx, blocks(%rip)
2: lea default_rom(%rip), %rcx
mov %rcx, rom(%rip)
3: call load_image
jmp 3f
.align 64
2: mov (%r15,%r13,4), %r14d
movzbl %r14b, %edi
shr $8, %r14d
cmp $29, %edi
ja 1f
shl $5, %edi
add %rbp, %rdi
call *%rdi
1: movzbl %r14b, %edi
shr $8, %r14d
cmp $29, %edi
ja 1f
shl $5, %edi
add %rbp, %rdi
call *%rdi
1: movzbl %r14b, %edi
shr $8, %r14d
cmp $29, %edi
ja 1f
shl $5, %edi
add %rbp, %rdi
call *%rdi
1: mov %r14d, %edi
cmp $29, %edi
ja 1f
shl $5, %edi
add %rbp, %rdi
call *%rdi
1: inc %r13
3: cmp $65536, %r13
jl 2b
mov $60, %eax /* sys_exit */
xor %edi, %edi
syscall