summaryrefslogtreecommitdiff
path: root/ifileIO3.s
blob: 6b7dc5e255dac733e88598263d80a6474d6c25bd (plain) (blame)
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#define d0 %eax
#define d1 %ebx
#define a0 %ecx
#define a1 %edx
#define a2 %ebp
#define a3 %esi
#define a4 %edi
#define a5 %esp
#define sp %esp

// # saved registers: %ebx %esi %edi %ebp
// #                   d1   a3   a4   a2

	.data
#if defined (DOS) || defined (_WINDOWS_) || defined (ELF)
	.align	8
#else
	.align	3
#endif

tmp_real:	.double	0
freadstring_error:
	.ascii	"Error in freadsubstring parameters."
	.byte	10,0
	.byte	0,0,0
fwritestring_error:
	.ascii	"Error in fwritesubstring parameters."
	.byte	10,0
	.byte	0,0
	.text

	.globl	stdioF
	.globl	stderrF
	.globl	openF
	.globl	closeF
	.globl	reopenF
	.globl	readFC
	.globl	readFI
	.globl	readFR
	.globl	readFS
#ifndef OLD_READ_STRING
	.globl	readFString
#endif
	.globl	readLineF
	.globl	writeFC
	.globl	writeFI
	.globl	writeFR
	.globl	writeFS
#ifndef OLD_WRITE_STRING
	.globl	writeFString
#endif
	.globl	endF
	.globl	errorF
	.globl	positionF
	.globl	seekF
	.globl	shareF
#ifndef LINUX
	.globl	flushF
#endif	
	.globl	openSF
	.globl	readSFC
	.globl	readSFI
	.globl	readSFR
	.globl	readSFS
	.globl	readLineSF
	.globl	endSF
	.globl	positionSF
	.globl	seekSF

// # imports

	.globl	@open_file
	.globl	@open_stdio
	.globl	@open_stderr
	.globl	@re_open_file
	.globl	@close_file
	.globl	@file_read_char
	.globl	@file_read_int
	.globl	@file_read_real
#ifdef OLD_READ_STRING
	.globl	@file_read_string
#else
	.globl	@file_read_characters
#endif
	.globl	@file_read_line
	.globl	@file_write_char
	.globl	@file_write_int
	.globl	@file_write_real
#ifdef OLD_WRITE_STRING
	.globl	@file_write_string
#else
	.globl	@file_write_characters
#endif
	.globl	@file_end
	.globl	@file_error
	.globl	@file_position
	.globl	@file_seek
	.globl	@file_share
#ifndef LINUX
	.globl	@flush_file_buffer
#endif
	.globl	@open_s_file
	.globl	@file_read_s_char
	.globl	@file_read_s_int
	.globl	@file_read_s_real
	.globl	@file_read_s_string
	.globl	@file_read_s_line
	.globl	@file_s_end
	.globl	@file_s_position
	.globl	@file_s_seek

	.globl	collect_0
	.globl	collect_1

	.globl	__STRING__

stdioF:	call	@open_stdio
	movl	d0,d1
	movl	$-1,d0
	ret

stderrF:	call	@open_stderr
	movl	d0,d1
	movl	$-1,d0
	ret

openF:	pushl	d0
	addl	$4,a0
	pushl	a0
	call	@open_file
	addl	$8,sp

	xorl	d1,d1
	testl	d0,d0
	setns	%bl
	movl	(sp),a2
	movl	$-1,(sp)
	jmp	*a2

closeF:	pushl	d1
	call	@close_file
	addl	$4,sp
	ret

reopenF:
// #	popl	d0
// #	pushl	d0
	pushl	d1
	call	@re_open_file
	addl	$8,sp

	xchg	d0,d1
	
	movl	(sp),a2
	movl	$-1,(sp)
	jmp	*a2

readFC:
	pushl	d1

	pushl	d1
	call	@file_read_char
	addl	$4,sp

	movl	4(sp),a2
	movl	$-1,4(sp)

	cmpl	$-1,d0
	je	readFC_eof

	movl	$1,d1
	jmp	*a2

readFC_eof:
	xorl	d0,d0
	xorl	d1,d1
	jmp	*a2

readFI:
	pushl	d1

	subl	$8,sp
	lea	4(sp),a2
	movl	a2,(sp)
	pushl	d1
	call	@file_read_int
	addl	$8,sp

	movl	d0,d1
	popl	d0

	movl	4(sp),a2
	movl	$-1,4(sp)
	jmp	*a2

readFR:
	pushl	$tmp_real
	pushl	d1
	finit
	call	@file_read_real
	addl	$8,sp

	fldl	tmp_real
	fstp	%st(1)
	
	xchg	d0,d1

	movl	(sp),a2
	movl	$-1,(sp)	
	jmp	*a2

#ifndef OLD_READ_STRING
readFString:
	movl	4(a0),a2
	cmpl	a2,d1
	jae	readFString_error

	subl	d1,a2
	cmpl	a2,d0
	ja	readFString_error

	movl	(sp),a1
	pushl	a0

	pushl	d0
	movl	sp,a2
	lea	8(a0,d1),a0

	pushl	a0
	pushl	a2
	pushl	a1
	call	@file_read_characters
	addl	$12+4,sp

	popl	a0
	
	movl	d0,d1
	popl	d0
	
	addl	$4,sp
	popl	a2
	pushl	$-1
	jmp	*a2

readFString_error:
	movl	$freadstring_error,a2
	jmp	print_error
#endif

readFS:	popl	a1
	lea	3(a1),a2
	andl	$-4,a2
	lea	-32+8(a4,a2),a2
	cmpl	end_heap,a2
	ja	readFS_gc
readFS_r_gc:

#ifdef OLD_READ_STRING
	movl	$__STRING__+2,(a4)
	addl	$4,a4
	
	pushl	a4
	pushl	a1
	pushl	d1
	call	@file_read_string
	addl	$12,sp
#else
	movl	$__STRING__+2,(a4)

	lea	8(a4),a2
	addl	$4,a4

	pushl	a2
	movl	a1,(a4)
	pushl	a4	
	pushl	d1
	call	@file_read_characters
	addl	$12,sp	
#endif
readFS_end:
	lea	-4(a4),a0

	addl	$3,d0
	andl	$-4,d0
	lea	4(a4,d0),a4

	movl	$-1,d0
	ret

readFS_gc:	pushl	a1
	call	collect_0l
	popl	a1
	jmp	readFS_r_gc

readLineF:
	lea	-32+(4*(32+2))(a4),a2
	cmpl	end_heap,a2
	ja	readLineF_gc

readLineF_r_gc:
	movl	$__STRING__+2,(a4)
	lea	8(a4),a0
	addl	$4,a4

	pushl	a0
	movl	end_heap,a1
	addl	$32-4,a1
	subl	a4,a1
	pushl	a1
	pushl	d1
	call	@file_read_line
	addl	$12,sp

	movl	d0,(a4)

	testl	d0,d0
	jns	readFS_end

	lea	-4(a4),a0

readLineF_again:
	movl	end_heap,a1
	addl	$32,a1
	lea	-8(a1),d0
	subl	a0,d0
	movl	d0,4(a0)
	movl	a1,a4

	lea	-32+4*(32+2)(a4,d0),a2
	call	collect_1l

	movl	4(a0),d0
	lea	8(a0),a1
	
	pushl	a4

	movl	$__STRING__+2,(a4)

	lea	3(d0),a0
	shr	$2,a0

	movl	d0,4(a4)
	addl	$8,a4
	jmp	st_copy_string1

copy_st_lp1:
	movl	(a1),a2
	addl	$4,a1
	movl	a2,(a4)
	addl	$4,a4
st_copy_string1:
	subl	$1,a0
	jnc	copy_st_lp1

	pushl	a4
	movl	end_heap,a2
	addl	$32,a2
	subl	a4,a2
	pushl	a2
	pushl	d1
	call	@file_read_line
	addl	$12,sp

	popl	a0

	testl	d0,d0
	js	readLineF_again

	addl	d0,4(a0)
	addl	$3,d0
	andl	$-4,d0
	addl	d0,a4

	movl	$-1,d0
	ret

readLineF_gc:
	call	collect_0l
	jmp	readLineF_r_gc

writeFC:
	movl	d0,(sp)
	pushl	d1
	movl	d0,d1
	call	@file_write_char
	addl	$8,sp

	movl	$-1,d0
	ret

writeFI:
	movl	d0,(sp)
	pushl	d1
	movl	d0,d1
	call	@file_write_int
	addl	$8,sp

	movl	$-1,d0
	ret

writeFR:
	pushl	d1
	subl	$8,sp
	fstpl	(sp)
	finit
 	call	@file_write_real
	addl	$12,sp

	movl	$-1,d0
	ret

writeFS:
	pushl	d1
#ifdef OLD_WRITE_STRING
	addl	$4,a0
	pushl	a0
	call	@file_write_string
	addl	$8,sp
#else
	pushl	4(a0)
	addl	$8,a0
	pushl	a0
	call	@file_write_characters
	addl	$12,sp
#endif
	movl	$-1,d0
	ret

#ifndef OLD_WRITE_STRING
writeFString:
	movl	4(a0),a2
	cmpl	a2,d1
	jae	writeFString_error

	subl	d1,a2
	cmpl	a2,d0
	ja	writeFString_error

	lea	8(a0,d1),a0
	movl	(sp),d1

	pushl	d0
	pushl	a0
	call	@file_write_characters
	addl	$12+4,sp

	movl	$-1,d0

	ret

writeFString_error:
	movl	$fwritestring_error,a2
	jmp	print_error
#endif

endF:
	pushl	d1
	call	@file_end
	addl	$4,sp

	xchg	d0,d1

	movl	(sp),a2
	movl	$-1,(sp)
	jmp	*a2

errorF:
	pushl	d1
	call	@file_error
	addl	$4,sp

	xchg	d0,d1

	movl	(sp),a2
	movl	$-1,(sp)
	jmp	*a2

positionF:
	pushl	d1
	call	@file_position
	addl	$4,sp

	xchg	d0,d1

	movl	(sp),a2
	movl	$-1,(sp)
	jmp	*a2

seekF:
	pushl	d1
	call	@file_seek
	addl	$12,sp

	xchg	d0,d1

	movl	(sp),a2
	movl	$-1,(sp)
	jmp	*a2

shareF:
	pushl	d1
	call	@file_share
	addl	$4,sp
	
	movl	$-1,d0
	ret

#ifndef LINUX
flushF:
	pushl	d1
	call	@flush_file_buffer
	movl	4(sp),a2
	xchg	d0,d1
	movl	$-1,4(sp)
	movl	a2,(sp)
	ret
#endif

openSF:	pushl	d0
	addl	$4,a0
	pushl	a0
	call	@open_s_file
	addl	$8,sp

	xorl	d1,d1
	testl	d0,d0
	setns	%bl

	movl	(sp),a2
	movl	$0,(sp)
	jmp	*a2

readSFC:
	pushl	d0
	movl	sp,a2
	pushl	a2
	pushl	d1
	call	@file_read_s_char
	addl	$8,sp

	popl	a0
	popl	a2

	pushl	a0
	pushl	d1

	cmpl	$-1,d0
	je	readSFC_eof

	movl	$1,d1
	jmp	*a2

readSFC_eof:
	xorl	d0,d0
	xorl	d1,d1
	jmp	*a2

readSFI:
	pushl	d0
	movl	sp,a2
	subl	$4,sp
	pushl	a2
	subl	$4,a2
	pushl	a2
	pushl	d1
	call	@file_read_s_int
	addl	$12,sp

	popl	a0
	popl	a1
	popl	a2

	pushl	a1
	pushl	d1
	movl	d0,d1
	movl	a0,d0
	jmp	*a2

readSFR:
	pushl	d0
	movl	sp,a2
	pushl	a2
	pushl	$tmp_real
	pushl	d1
	finit
	call	@file_read_s_real
	addl	$12,sp

	fldl	tmp_real
	xchg	d0,d1
	fstp	%st(1)

	popl	a0
	movl	(sp),a2
	movl	a0,(sp)
	jmp	*a2

readSFS:
	popl	a1
	lea	3(a1),a2
	andl	$-4,a2
	lea	-32+8(a4,a2),a2
	cmpl	end_heap,a2
	ja	readSFS_gc

readSFS_r_gc:
	movl	$__STRING__+2,(a4)
	addl	$4,a4

	pushl	d0
	movl	sp,a2
	pushl	a2
	pushl	a4
	pushl	a1
	pushl	d1
	call	@file_read_s_string
	addl	$16,sp

readSFS_end:
	lea	-4(a4),a0

	addl	$3,d0
	andl	$-4,d0
	lea	4(a4,d0),a4

	popl	d0
	ret

readSFS_gc:	pushl	a1
	call	collect_0l
	popl	a1
	jmp	readSFS_r_gc

readLineSF:
	lea	-32+(4*(32+2))(a4),a2
	cmpl	end_heap,a2
	ja	readLineSF_gc

readLineSF_r_gc:
	movl	$__STRING__+2,(a4)
	lea	8(a4),a0
	addl	$4,a4

	pushl	d0
	movl	sp,a2
	pushl	a2
	pushl	a0
	movl	end_heap,a1
	addl	$32-4,a1
	subl	a4,a1
	pushl	a1
	pushl	d1
	call	@file_read_s_line
	addl	$16,sp

	movl	d0,(a4)

	testl	d0,d0
	jns	readSFS_end

	lea	-4(a4),a0

readLineSF_again:
	movl	end_heap,a1
	addl	$32,a1
	lea	-8(a1),d0
	subl	a0,d0
	movl	d0,(a4)
	movl	a1,a4

	lea	-32+4*(32+2)(a4,d0),a2
	call	collect_1l

	movl	4(a0),d0
	lea	8(a0),a1

	pushl	a4

	movl	$__STRING__+2,(a4)

	lea	3(d0),a0
	shr	$2,a0

	movl	d0,4(a4)
	addl	$8,a4
	jmp	st_copy_string2

copy_st_lp2:
	movl	(a1),a2
	addl	$4,a1
	movl	a2,(a4)
	addl	$4,a4
st_copy_string2:
	subl	$1,a0
	jnc	copy_st_lp2

	lea	4(sp),a2
	pushl	a2
	pushl	a4
	movl	end_heap,a2
	addl	$32,a2
	subl	a4,a2
	pushl	a2
	pushl	d1
	call	@file_read_s_line
	addl	$16,sp

	popl	a0

	testl	d0,d0
	js	readLineSF_again
	
	addl	d0,4(a0)
	addl	$3,d0
	andl	$-4,d0
	addl	d0,a4

	popl	d0
	ret

readLineSF_gc:
	call	collect_0l
	jmp	readLineSF_r_gc

endSF:
	pushl	d0
	pushl	d1
	call	@file_s_end
	addl	$8,sp
	ret

positionSF:
	pushl	d0
	pushl	d1
	call	@file_s_position
	addl	$8,sp
	ret

seekSF:
	popl	a1
	popl	a0

	pushl	d0
	movl	sp,a2
	pushl	a2
	pushl	a0
	pushl	a1
	pushl	d1
	call	@file_s_seek
	addl	$16,sp

	popl	a0

	xchg	d0,d1

	movl	(sp),a2
	movl	a0,(sp)
	jmp	*a2