summaryrefslogtreecommitdiff
path: root/start.asm
blob: b2bef32fbe6e3f172de396beff47399a66c05afb (plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# Copyright (C) 2026 Sebastian G. Kirmayer <gloria@gloria-mundi.eu>
#
#  This file is part of sysf-i386.
#
#  sysf-i386 is free software: you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option)
#  any later version.
#
#  sysf-i386 is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  details.
#
#  You should have received a copy of the GNU General Public License along
#  with sysf-i386. If not, see <https://www.gnu.org/licenses/>.
#

# Syscall numbers
NR_exit=1
NR_read=3
NR_write=4
NR_open=5
NR_close=6
NR_brk=45

text
label _start
	call init_heap

	# option parsing
	leal @%esp+4 %ebp

L loop
	addl 4 %ebp
	movl @%ebp %ebx
	testl %ebx %ebx
	jz done.
	movb @%ebx %cl
	cmpb 0x2D %cl # -
	jne filename.
	incl %ebx
	movb @%ebx %cl
	cmpb 0x00 %cl
	je filename.
	cmpb 0x2D %cl # -
	je longopt.
	cmpb 0x3F %cl # ?
	je usage
	cmpb 0x56 %cl # V
	je print_version
	jmp bad_usage

L longopt
	incl %ebx
	movb @%ebx %cl
	cmpb 0x00 %cl
	je opts_end.
	movl %ebx %eax
	call strnul
	pushl %eax
	pushl %eax
	movl flag_version %eax
	movl flag_version_end %ecx
	movl %ebx %edx
	call streq
	je print_version
	movl flag_help %eax
	movl flag_help_end %ecx
	movl %ebx %edx
	call streq
	je usage
	jmp bad_usage

L opts_end
	addl 4 %ebp
	movl @%ebp %ebx
	testl %ebx %ebx
	jz done.
L filename
	movl @%ebp+4 %eax
	testl %eax %eax
	jnz bad_usage
	movl @%ebp %ebx
	movb @%ebx %cl
	cmpb 0x2D %cl
	jne open_file.
	movb @%ebx %cl
	cmpb 0x00 %cl
	je done.
L open_file
	movl $NR_open %eax
	xorl %ecx %ecx
	int 0x80
	testl %eax %eax
	js open_failed.
	movl %eax %edi
	movl @heap_ptr %esi
	call slurp
	movl %edi %ebx
	movl $NR_close %eax
	int 0x80
	movl %esi %ebx
	jmp slurped.

L open_failed
	stderr 'Failed to open '
	movl %ebx %eax
	call strnul
	movl %eax %ecx
	movl %ebx %eax
	call stderr
	stderr '
'
	jmp error

L done
	# slurp(stdin)
	xorl %eax %eax
	movl @heap_ptr %ebx
	call slurp
L slurped
	# parse(slurped code)
	movl %ebx %eax
	movl @heap_ptr %ecx
	call parse
	call ty_top
	movl %eax %ebx
	call ty_main
	call ty_top
	movl %ebx %ecx
	call ty_eq
	jne bad_main.
	movl @heap_ptr %eax
	jmp execute

L bad_main
	stderr 'Bad main type: '
	movl %ebx %eax
	call ty_stderr
	stderr '
'
	jmp error

label ty_main
		# fix
						movl 0 %eax
					call ty_var
						movl 1 %eax
					call ty_var
				call ty_arrow
					movl 0 %eax
				call ty_var
					movl 1 %eax
				call ty_var
			call ty_arrow
			call ty_arrow
				movl 0 %eax
			call ty_var
				movl 1 %eax
			call ty_var
		call ty_arrow
		call ty_arrow
		call ty_forall
		# read
				movl 0 %eax
			call ty_var
				movl 0 %eax
			call ty_var
				movl 0 %eax
			call ty_var
				movl 0 %eax
			call ty_var
		call ty_arrow
		call ty_arrow
		call ty_arrow
		# write0
				movl 0 %eax
			call ty_var
				movl 0 %eax
			call ty_var
		call ty_arrow
		# write1
				movl 0 %eax
			call ty_var
				movl 0 %eax
			call ty_var
		call ty_arrow
		# exit
			movl 0 %eax
		call ty_var
			movl 0 %eax
		call ty_var
	call ty_arrow
	call ty_arrow
	call ty_arrow
	call ty_arrow
	call ty_arrow
	call ty_forall
	ret

label slurp # (fd: i32) -> ()
	pushl %ebx
	movl %eax %ebx
	# %ebx: file descriptor
L start
	movl @heap_ptr %ecx
	movl @heap_end %edx
L loop
	subl %ecx %edx
	jz alloc.
	movl $NR_read %eax
	int 0x80
	addl %ecx %edx
	testl %eax %eax
	js fail.
	jz done.
	addl %eax %ecx
	jmp loop^
L alloc
	movl %ecx @heap_ptr
	call alloc
	jmp start^
L done
	movl %ecx @heap_ptr
	popl %ebx
	ret
L fail
	stderr 'Failed to read program
'
	jmp error

bss
align
label heap_ptr
	long 0
label heap_end
	long 0

text
label init_heap # () -> ()
	movl %ebx %ecx
	xorl %ebx %ebx
	movl $NR_brk %eax
	int 0x80
	movl %eax @heap_ptr
	movl %eax @heap_end
	movl %ecx %ebx
	ret

label align_heap # () -> ()
	movl @heap_ptr %eax
	addl 3 %eax
	andl $((~3)) %eax
	movl %eax @heap_ptr
	ret

label alloc # () -> ()
	movl %ebx %ecx
	movl @heap_end %ebx
	addl 0x1000 %ebx
	movl $NR_brk %eax
	int 0x80
	cmpl %eax %ebx
	jne error
	movl %eax @heap_end
	movl %ecx %ebx
	ret

bss
label buffer_start
	skip 0x100
label buffer_cap buffer_start
align
label buffer_pos
	long 0
label buffer_len
	long 0
text

# -1: EOF
# 0: 0
# 1: 1
label read # () -> (bit: i32)
	movl @buffer_pos %ecx
	movl %ecx %edx
	shrl 3 %edx
	cmpl @buffer_len %edx
	je read.
	xorl %eax %eax
	movb @buffer_start+%edx %al
	andl 7 %ecx
	shrl %cl %eax
	andl 1 %eax
	incl @buffer_pos
	ret

L read
	pushl %ebx
	xorl %ebx %ebx
	movl buffer_start %ecx
	movl buffer_cap %edx
	movl $NR_read %eax
	int 0x80
	popl %ebx
	testl %eax %eax
	js error.
	jz eof.
	movl %eax @buffer_len
	movl 0 @buffer_pos
	jmp read

L eof
	movl -1 %eax
	ret

L error
	stderr 'Error reading from stdin
'
	jmp error

bss
label write_buf
	byte 0
label write_bit
	byte 0
text

label write0
	movl 0 %eax
	jmp write.
label write1
	movl 1 %eax
L write
	xorl %ecx %ecx
	movb @write_bit %cl
	shll %cl %eax
	orb %al @write_buf
	incb %cl
	cmpb 8 %cl
	je do_write.
	movb %cl @write_bit
	ret
L do_write
	movb 0 @write_bit
	pushl %ebx
	movl 1 %ebx
	movl write_buf %ecx
	movl 1 %edx
	movl $NR_write %eax
	int 0x80
	popl %ebx
	movb 0 @write_buf
	ret

label exit
	movl $NR_exit %eax
	xorl %ebx %ebx
	int 0x80

label usage
	call print_usage
	jmp exit

label bad_usage
	call print_usage
	jmp error

label print_usage
	movl 1 %ebx
	movl usage_info %ecx
	movl usage_info_len %edx
	movl $NR_write %eax
	int 0x80
	ret

label print_version
	movl 1 %ebx
	movl version_info %ecx
	movl version_info_len %edx
	movl $NR_write %eax
	int 0x80
	jmp exit

label error
	movl $NR_exit %eax
	movl 1 %ebx
	int 0x80

label stderr
	pushl %ebx
	movl %ecx %edx
	movl %eax %ecx
	subl %eax %edx
	movl 2 %ebx
	movl $NR_write %eax
	int 0x80
	popl %ebx
	ret

label stderr_lineno
	pushl %ebx
	pushl %ebp
	movl %esp %ebp
	movl %esp %ebx
	subl 0x20 %esp
	subl 2 %ebx
	movb 0x3A @%ebx
	movb 0x20 @%ebx+1
	movl @lineno %eax
	movl 10 %ecx
L loop
	xorl %edx %edx
	divl %ecx
	addl 0x30 %edx
	decl %ebx
	movb %dl @%ebx
	testl %eax %eax
	jnz loop^
	subl 5 %ebx
	movb 0x4C @%ebx
	movb 0x69 @%ebx+1
	movb 0x6E @%ebx+2
	movb 0x65 @%ebx+3
	movb 0x20 @%ebx+4
	movl %ebx %eax
	movl %ebp %ecx
	call stderr
	movl %ebp %esp
	popl %ebp
	popl %ebx
	ret

label report_error
	pushl %eax
	pushl %ecx
	call stderr_lineno
	popl %ecx
	popl %eax
	call stderr
	ret

rodata
string flag_version "version"
string flag_help "help"

string version_info "\
sysf-i386 $SYSF_VERSION
Copyright (C) 2026 Sebastian G. Kirmayer, GNU GPLv3+
"

string usage_info "\
Usage:
   sysf FILENAME
   sysf --version
"