-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.asm
14426 lines (10408 loc) · 218 KB
/
main.asm
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; M1 MICRO FORTH by Alban
;; This is a small non-standard FORTH like interpreter for Apple Silicon
;; implemented in assembly language
;; Copyright 2022 Alban Read
;; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
;; 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
;; 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;; reserved registers.
;; X18 reserved for OS
;; X29 frame ptr for OS/C
;; X30 aka LR (the return register)
;; X28 data section pointer
;; X0-X7 and D0-D7, are used to pass arguments
;; X19-X28 callee
;; X8 indirect result
;; related to the interpreter
; X16 is the data stack
; X26 is the locals stack
; X15 is the interpreter pointer/dictionary pointer
; X14 is the return stack
; X13
; X12 is the tertiary pointer
; X6 is the FORTH flags status register
; 11 - Trace On/Off
; 10 - Parsing Float
; 09 - Redefining word
; tracks various on/off states
;; X27 dend (start of dictionary)
;; X28 dictionary - current word header in the dictionary searcj=h
;; X22 word (text)
;; X23 also used for text
.macro reset_data_stack
ADRP X0, dsp@PAGE
ADD X0, X0, dsp@PAGEOFF
LDR X1, [X0]
ADD X1, X1, #48
MOV X16, X1
.endm
.macro reset_return_stack
ADRP X1, rp1@PAGE
ADD X1, X1, rp1@PAGEOFF
ADRP X0, rsp@PAGE
ADD X0, X0, rsp@PAGEOFF
STR X1, [X0]
MOV X14, X1
.endm
.macro save_registers
STP X12, X13, [SP, #-16]!
STP X14, X15, [SP, #-16]!
STP LR, X16, [SP, #-16]!
STP X6, X7, [SP, #-16]!
STP X4, X5, [SP, #-16]!
STP X2, X3, [SP, #-16]!
STP X22, X24, [SP, #-16]!
.endm
.macro restore_registers
LDP X22, X24, [SP], #16
LDP X2, X3, [SP], #16
LDP X4, X5, [SP], #16
LDP X6, X7, [SP], #16
LDP LR, X16, [SP], #16
LDP X14, X15, [SP], #16
LDP X12, X13, [SP], #16
.endm
.macro save_registers_not_stack
STP X12, X13, [SP, #-16]!
STP X14, X15, [SP, #-16]!
STP LR, XZR, [SP, #-16]!
.endm
.macro restore_registers_not_stack
LDP LR, XZR, [SP], #16
LDP X14, X15, [SP], #16
LDP X12, X13, [SP], #16
.endm
; dictionary headers
; These are separate from the token space, or literal pools
; contain the name of a word, the runtime and compile time functions
; and a small ammount of data for the functions use.
; data (argument for runtime)
; runtime function address when running
; data (argument for compile time)
; comptime function address when compiling
; data spare data
; data
; name the words name
.macro makeword name:req, runtime=-1, comptime=-1, datavalue=1, dvalue2=0, dvalue3=0, dvalue4=0
.quad \datavalue
.quad \runtime
.quad \dvalue2 ; data
.quad \comptime
.quad \dvalue3
.quad \dvalue4
10:
.asciz "\name"
20:
.zero 16 - ( 20b-10b )
.endm
.macro makevarword name:req, v1=1, v2=0, v3=0, v4=0
.quad \v1
.quad dvaraddz
.quad \v2
.quad dvaraddc
.quad \v3
.quad \v4
10:
.asciz "\name"
20:
.zero 16 - ( 20b-10b )
.endm
.macro makebword name:req, runtime=-1, comptime=-1, datavalue=1
.quad \datavalue
.quad \runtime
.quad 0
.quad \comptime
.quad 0
.quad 0
.byte \name
.zero 15
.endm
.macro makeqvword name:req
.quad 8 * \name + ivars
.quad dvaraddz
.quad 0
.quad dvaraddc
.quad 0
.quad 0
.byte \name
.zero 15
.endm
.macro makeemptywords n=32
.rept \n
.quad -1
.quad -1
.quad 0
.quad 0
.quad 0
.quad 0
.quad -1
.quad -1
.endr
.endm
.macro copy_word_name
; copy words name text over
LDR X0, [X22]
STR X0, [X28, #48]
ADD X22, X22, #8
LDR X0, [X22]
STR X0, [X28, #56]
.endm
; trace words, check for X6 TRACE flag
; and print a trace of the inner interpreter
.macro trace_show_word
TBZ X6, 11, 999f ; TRON?
STP LR, X0, [SP, #-16]!
ADD X2, X1, #48
MOV X0, X1
BL X0addrprln
LDRH W0, [X15]
BL X0halfpr
MOV X0, X2
LDRH W1, [X15,#-2]
ADRP X0, literal_name@PAGE
ADD X0, X0, literal_name@PAGEOFF
20:
BL X0prname
BL ddotsz
998:
LDP LR, X0, [SP], #16
999:
.endm
.macro do_trace
TBZ X6, 11, 999f ; TRON?
STP LR, X0, [SP, #-16]!
MOV X0, X15
BL X0addrprln
LDRH W0, [X15]
BL X0halfpr
LDRH W0, [X15]
LSL W0, W0, #6 ; TOKEN*64
ADD X0, X0, X27 ; + dend
ADD X0, X0, #48
ADRP X2, startdict@PAGE
ADD X2, X2, startdict@PAGEOFF
CMP X0, X2
B.gt 999f
LDR X2, [X0]
LDRH W1, [X15,#-2]
CBZ W1, 20f
CMP W1, 16
B.gt 20f
ADRP X0, literal_name@PAGE
ADD X0, X0, literal_name@PAGEOFF
20:
BL X0prname
MOV X0, X15
BL X0prip
BL ddotsz
BL ddotrz
998:
LDP LR, X0, [SP], #16
999:
.endm
.data
data_base:
.align 8
;; VERSION OF THE APP
ver: .double 0.842
tver: .ascii "M1 MICRO FORTH %2.2f JAN 2022\n"
.zero 4
.text
.global main
.align 8
;
; Unix system call
; e.g. ' ls -l' SYSTEM
dsystem:
LDR X0, [X16, #-8]
SUB X16, X16, #8
save_registers
bl _system
restore_registers
RET
beloud:
ADRP X0, bequiet@PAGE
ADD X0, X0, bequiet@PAGEOFF
MOV X1, #0
STR X1, [X0]
RET
derrnoz:
LDR X0, [X16, #-8]
SUB X16, X16, #8
save_registers
BL _get_errno
restore_registers
STR X0, [X16], #8
RET
derrstrz:
LDR X0, [X16, #-8]
SUB X16, X16, #8
save_registers
BL _get_errstr
restore_registers
STR X0, [X16], #8
RET
; load loads a file
; and then reverts to stdin
; The three file related things that happen are that
; On startup forth.forth is loaded.
; Later files can be loaded with FLOAD file.name
; And otherwise the interpreter loops on getline from stdin.
dfloadfromstackz:
10:
LDR X0, [X16, #-8] ; string filename
SUB X16, X16, #8
dfloadx0:
save_registers
ADRP X1, mode_read@PAGE
ADD X1, X1, mode_read@PAGEOFF
BL _fopen
CBZ X0, 90f ; failed
ADRP X1, input_file@PAGE
ADD X1, X1, input_file@PAGEOFF
STR X0, [X1]
B 99f
90:
BL _get_errno
BL _get_errstr
BL sayit
99:
restore_registers
STR X0, [X16], #8
RET
dfload:
save_registers
save_registers
BL advancespaces
BL collectwordnoalias
BL get_word
BL empty_wordQ
restore_registers
B.eq 190f
ADRP X0, zword@PAGE
ADD X0, X0, zword@PAGEOFF
B dfloadx0
190:
RET
from_startup:
ADRP X0, startup_file@PAGE
ADD X0, X0, startup_file@PAGEOFF
save_registers
ADRP X1, mode_read@PAGE
ADD X1, X1, mode_read@PAGEOFF
BL _fopen
ADRP X1, input_file@PAGE
ADD X1, X1, input_file@PAGEOFF
STR X0, [X1]
restore_registers
RET
dfrom_startup:
B from_startup
from_stdin:
ADRP X8, ___stdinp@GOTPAGE
LDR X8, [X8, ___stdinp@GOTPAGEOFF]
LDR X2, [X8]
ADRP X0, input_file@PAGE
ADD X0, X0, input_file@PAGEOFF
STR X2, [X0]
ADRP X0, bequiet@PAGE
ADD X0, X0, bequiet@PAGEOFF
MOV X1, #0
STR X1, [X0]
RET
; get line from terminal into PAD
getline:
ADRP X0, input_file@PAGE
ADD X0, X0, input_file@PAGEOFF
LDR X2, [X0]
ADRP X0, zpadsz@PAGE
ADD X1, X0, zpadsz@PAGEOFF
ADRP X0, zpadptr@PAGE
ADD X0, X0, zpadptr@PAGEOFF
save_registers
BL _getline
CMP X0, #-1 ; end of file
B.ne 10f
; close file on end of file.
ADRP X0, input_file@PAGE
ADD X0, X0, input_file@PAGEOFF
LDR X0, [X0]
BL _fclose
BL from_stdin ; revert to stdin
10:
; store count read in accepted
ADRP X1, accepted@PAGE
ADD X1, X1, accepted@PAGEOFF
STR X0, [X1]
restore_registers
RET
; accept text from user
dacceptz:
ADRP X0, input_file@PAGE
ADD X0, X0, input_file@PAGEOFF
LDR X2, [X0]
ADRP X1, acceptcap@PAGE
ADD X1, X1, acceptcap@PAGEOFF
ADRP X0, acceptptr@PAGE ; ** accept_string
ADD X0, X0, acceptptr@PAGEOFF
save_registers
BL _getline
CMP X0, #-1 ; end of file
B.ne 10f
ADRP X1, acceptlen@PAGE
ADD X1, X1, acceptlen@PAGEOFF
STR X0, [X1]
; close file on end of file.
ADRP X0, input_file@PAGE
ADD X0, X0, input_file@PAGEOFF
LDR X0, [X0]
BL _fclose
BL from_stdin ; revert to stdin
10:
; store count read in accepted
restore_registers
STP LR, XZR, [SP, #-16]!
STP X12, X13, [SP, #-16]!
STP X3, X5, [SP, #-16]!
B intern_string_from_buffer
RET
cls:
dpagez:
ADRP X0, clear_screen@PAGE
ADD X0, X0, clear_screen@PAGEOFF
B sayit
datxyz:
ADRP X0, screen_at@PAGE
ADD X0, X0, screen_at@PAGEOFF
save_registers
LDP X1, X2, [X16,#-16]
STP X1, X2, [SP, #-16]!
BL _printf
ADD SP, SP, #16
restore_registers
SUB X16, X16, #16
RET
datcolr:
ADRP X0, screen_textcolour@PAGE
ADD X0, X0, screen_textcolour@PAGEOFF
save_registers
MOV X2, #0
MOV X1, #33
LDR X1, [X16, #-8]
STP X1, XZR, [SP, #-16]!
BL _printf
ADD SP, SP, #16
restore_registers
SUB X16, X16, #8
RET
; intern a copy of a string.
intern:
; string assumed to be in string_buffer
STP LR, XZR, [SP, #-16]!
STP X12, X13, [SP, #-16]!
STP X3, X5, [SP, #-16]!
B intern_string_from_buffer
; Lots of little messages
; Ok prompt
; only say OK when reading stdin.
sayok:
ADRP X1, input_file@PAGE
ADD X1, X1, input_file@PAGEOFF
LDR X0, [X1]
ADRP X8, ___stdinp@GOTPAGE
LDR X8, [X8, ___stdinp@GOTPAGEOFF]
LDR X2, [X8]
CMP X0, X2
B.ne quietly
ADRP X0, tok@PAGE
ADD X0, X0, tok@PAGEOFF
B sayit
quietly:
RET
saycr:
ADRP X0, tcr@PAGE
ADD X0, X0, tcr@PAGEOFF
B sayit
sayforgetting:
ADRP X0, tforget@PAGE
ADD X0, X0, tforget@PAGEOFF
B sayit
saycompfin:
ADRP X0, tcomer6@PAGE
ADD X0, X0, tcomer6@PAGEOFF
B sayit
saywordexists:
ADRP X0, texists@PAGE
ADD X0, X0, texists@PAGEOFF
B sayit
saycreatedword:
ADRP X0, tcomer7@PAGE
ADD X0, X0, tcomer7@PAGEOFF
B sayit
saylb:
ADRP X0, tlbr@PAGE
ADD X0, X0, tlbr@PAGEOFF
B sayit
sayrb:
ADRP X0, trbr@PAGE
ADD X0, X0, trbr@PAGEOFF
B sayit
saybye:
ADRP X0, tbye@PAGE
ADD X0, X0, tbye@PAGEOFF
B sayit
saynotfound:
ADRP X0, tcomer4@PAGE
ADD X0, X0, tcomer4@PAGEOFF
B sayit_err
sayerrlength:
ADRP X0, tlong@PAGE
ADD X0, X0, tlong@PAGEOFF
B sayit
sayerrwordlong:
ADRP X0, tcomer8@PAGE
ADD X0, X0, tcomer8@PAGEOFF
B sayit
sayerrpoolfullquad:
ADRP X0, poolfullerr@PAGE
ADD X0, X0, poolfullerr@PAGEOFF
B sayit
sayword:
ADRP X0, zword@PAGE
ADD X0, X0, zword@PAGEOFF
B sayit
; calloc is used to allocate memory for word tokens, and variable data storage.
; the word token size is resized once we know how long a word is with realloc.
; Forget makes some effort to reclaim space.
; This was not slower than managing larger pools, and is more flexible.
; It means FORTH programs can use as much memory as it likes, within reason.
calloc_failed: ; yes it can fail and yes that is totally fatal.
STP LR, XZR, [SP, #-16]!
ADRP X0, calloc_error@PAGE
ADD X0, X0, calloc_error@PAGEOFF
BL sayit
MOV X0, #-1
BL _exit ; thats that.
; ... this never happens.
LDP LR, XZR, [SP], #16
RET
; The overflow checks are run when control returns to the interpreter.
sayoverflow:
STP LR, XZR, [SP, #-16]!
BL saycr
BL saylb
LDR X0, [X26, #-72] ; self
ADD X0, X0, #48
BL sayit
BL sayrb
BL saylb
MOV X0, X16
BL X0addrpr
BL sayrb
BL saylb
ADRP X0, spu@PAGE
ADD X0, X0, spu@PAGEOFF
LDR X0, [X0]
BL X0addrpr
BL sayrb
BL saylb
ADRP X0, spo@PAGE
ADD X0, X0, spo@PAGEOFF
LDR X0, [X0]
BL X0addrpr
BL sayrb
LDP LR, XZR, [SP], #16
ADRP X0, tovflr@PAGE
ADD X0, X0, tovflr@PAGEOFF
B sayit_err_word
sayunderflow:
STP LR, XZR, [SP, #-16]!
BL saycr
BL saylb
LDR X0, [X26, #-72] ; self
ADD X0, X0, #48
BL sayit
BL sayrb
BL saylb
MOV X0, X16
BL X0addrpr
BL sayrb
BL saylb
ADRP X0, dsp@PAGE
ADD X0, X0, dsp@PAGEOFF
LDR X0, [X0]
BL X0addrpr
BL sayrb
LDP LR, XZR, [SP], #16
ADRP X0, tunder@PAGE
ADD X0, X0, tunder@PAGEOFF
B sayit_err_word
sayeol:
ADRP X0, texit@PAGE
ADD X0, X0, texit@PAGEOFF
BL sayit
B finish
sayliteral:
ADRP X0, tliteral@PAGE
ADD X0, X0, tliteral@PAGEOFF
BL sayit
B finish
; WORDS - displays words in the dictionary.
; not typically used as redefined in startup to add colour.
alldotwords:
ADRP X8, ___stdoutp@GOTPAGE
LDR X8, [X8, ___stdoutp@GOTPAGEOFF]
LDR X1, [X8]
MOV X2, X27 ; dend
B 20f
dotwords:
ADRP X8, ___stdoutp@GOTPAGE
LDR X8, [X8, ___stdoutp@GOTPAGEOFF]
LDR X1, [X8]
ADRP X2, hashdict@PAGE
ADD X2, X2, hashdict@PAGEOFF
ADRP X8, rmargin@PAGE
ADD X8, X8, rmargin@PAGEOFF
LDR X3, [X8]
20: ADD X2, X2, #64
LDR X0, [X2, #48]
CMP X0, #-1
B.eq 10f
CMP X0, #0
B.eq 15f
LDRB W0, [X2, #48]
CBZ W0, 10f
LDRB W0, [X2, #48]
ADD X0, X2, #48
MOV X4, X0
MOV X5, X0
510:
LDRB W0, [X5], #1
CBZ W0, 520f
B 510b
520:
SUB X8, X5, X4
SUB X3, X3, X8
MOV X0, X4
530:
STP X2, X1, [SP, #-16]!
save_registers
BL _fputs
restore_registers
MOV X0, #32
save_registers
BL _putchar
restore_registers
CMP X3, #0
B.gt 540f
save_registers
BL saycr
restore_registers
ADRP X8, rmargin@PAGE
ADD X8, X8, rmargin@PAGEOFF
LDR X3, [X8]
540:
LDP X2, X1, [SP], #16
10: ; skip non word
B 20b
15:
RET
; displays a message
sayit:
ADRP X8, ___stdoutp@GOTPAGE
LDR X8, [X8, ___stdoutp@GOTPAGEOFF]
LDR X1, [X8]
save_registers
BL _fputs
restore_registers
RET
; displays message and sets the error flag
sayit_err:
ADRP X8, ___stdoutp@GOTPAGE
LDR X8, [X8, ___stdoutp@GOTPAGEOFF]
LDR X1, [X8]
save_registers
BL _fputs
restore_registers
MOV X0, #-1 ; flag err for compiler
RET
sayit_err_word:
save_registers
MOV X5, X0
BL saycr
BL saylb
LDR X0, [X26, #-72] ; self
ADD X0, X0, #48
BL sayit
BL sayrb
ADRP X8, ___stdoutp@GOTPAGE
LDR X8, [X8, ___stdoutp@GOTPAGEOFF]
LDR X1, [X8]
MOV X0, X5
BL _fputs
restore_registers
MOV X0, #-1 ; flag err for compiler
RET
; WORD scanning routines
resetword: ; clear word return x22
ADRP X22, zword@PAGE
ADD X22, X22, zword@PAGEOFF
STP XZR, XZR, [X22]
STP XZR, XZR, [X22, #48]
RET
; zaps the line storage for the terminal input
resetline:
; get zpad address in X23
ADRP X0, zpad@PAGE
ADD X23, X0, zpad@PAGEOFF
MOV X0, X23
.rept 384
MOV W1, #32
STRB W1, [X0], #1
MOV W1, #0
STRB W1, [X0], #1
.endr
RET
; Skip over spaces in the input
advancespaces: ; byte ptr in x23, advance past spaces until zero
10: LDRB W0, [X23]
CMP W0, #0
B.eq 90f
CMP W0, #32
b.gt 90f
ADD X23, X23, #1
B 10b
90: RET
; some simple maths operations
absz:
RET
addz: ; add tos to 2os leaving result tos
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
ADD X3, X1, X2
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
subz: ; add tos to 2os leaving result tos
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
SUB X3, X2, X1
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
mulz: ; mul tos with 2os leaving result tos
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
MUL X3, X2, X1
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
dstarslshz: ; famous */ but only 64 bit not using 128 intermediate
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
LDR X3, [X16, #-24]
SUB X16, X16, #24
MUL X0, X3, X2
SDIV X0, X0, X1
STR X0, [X16], #8
RET
dstarslshzmod: ; */MOD
LDP X1, X2, [X16, #-16]
LDR X3, [X16, #-24]
SUB X16, X16, #24
MUL X0, X3, X2
SDIV X0, X0, X1
MSUB X3, X2, X3, X0
STR X0, [X16], #8
STR X3, [X16], #8
RET
; some simple logical operations
andz: ; and tos with 2os leaving result tos
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
AND X3, X2, X1
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
dnegz: ; negate
LDR X1, [X16, #-8]
NEG X1, X1
STR X1, [X16, #-8]
RET
orz: ; or tos with 2os leaving result tos
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
ORR X3, X2, X1
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
; maths operations
sdivz: ; div tos by 2os leaving result tos
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
SDIV X3, X2, X1
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
udivz: ; div tos by 2os leaving result tos - ??? not clear this is correct.
LDR X1, [X16, #-8]
LDR X2, [X16, #-16]
SDIV X3, X2, X1
STR X3, [X16, #-16]
SUB X16, X16, #8
RET
; char output
emitz: ; output tos as char
LDR X1, [X16, #-8]
SUB X16, X16, #8
12: MOV X0, X1
X0emit:
save_registers
BL _putchar
; we need to flush
ADRP X8, ___stdoutp@GOTPAGE
LDR X8, [X8, ___stdoutp@GOTPAGEOFF]
LDR X0, [X8]
BL _fflush
restore_registers
RET
; lazy buffered version
emitchz: ; output X0 as char
save_registers
BL _putchar
restore_registers
RET
emitchc: ; output X0 as char
RET
; flush the stdout stream
dflushz:
save_registers
ADRP X8, ___stdoutp@GOTPAGE