-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path64th.asm
2081 lines (1892 loc) · 46.8 KB
/
64th.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
BITS 64
sys_write EQU 1
extern _binary_rc_4_start
extern _binary_rc_4_size
SECTION stack nobits alloc noexec write align=8
stack RESB 1000
stacklen EQU $ - $$
SECTION rstack nobits alloc noexec write align=8
returnstack RESB 1000
returnstacklen EQU $ - $$
SECTION .bss
emitbuf RESB 1
SECTION .rodata
; Start of Dictionary
; The Dictionary is a key Forth datastructure.
; It is a linked list, with each element having the structure:
; - Link Field 1 QWord (8 bytes)
; - Name Field: Length 1 QWord
; - Name Field: String 8 Bytes
; - Code Field 1 QWord
; - Parameter Field N Qwords
;
; The Link Field holds the address of the previous Link Field.
; Thus to locate the previous Name Field, 8 must be added to this.
; Generally the Link Field points to a numerically lower address.
; A Link Field with contents of 0 marks the end of the dictionary.
;
; This (slightly unusual) organisation means that
; the following dictionary definitions in the assembler file
; are modular, in the sense that
; they can be moved and reordered with editor block operations.
;
; (an idea borrowed from [LOELIGER1981]) Note that in the
; Name Field only the first 8 bytes of a name are stored,
; even though the length of the whole name is stored.
; This means that the Name Field is fixed size,
; but can still distinguish between
; names of different lengths that share a prefix.
;
; The Length field also holds flags. It is a 64-bit word
; that holds the length in the least significant 32-bits,
; and flags in the upper 32 bits.
; The only flag that is used currently is bit 33 (2<<32),
; which is 1 when the word is marked as IMMEDIATE.
; Convert address of Code Field to address of Link Field
%define CtoL(a) DQ (a)-24
; Or (using «|») into Length Field to create IMMEDIATE word.
%define Immediate (2<<32)
; Assembler Style
; For Forth words defined in assembler,
; try and stick to the following guidelines:
; :asm:load-store:
; Pretend Intel 64 is a Load/Store architecture:
; prefer instructions that either just do load/store, or
; just do register to register computation.
; :asm:fetch-do-store:
; Words should be organised to do in order:
; - fetch data from stack to registers;
; - do computation;
; - store data from registers to stack.
; :asm:memseq:
; In general access memory sequentially from lower addresses to
; higher addresses.
; Sentinel value dictionary has a Link Field of 0
DQ 0 ; Link Field
DQ 7
DQ 'EXECUTE'
EXECUTE:
DQ $+8
; EXECUTE ( addr -- )
; execute the Forth word that has compilation address `addr`
sub rbp, 8
mov rdx, [rbp]
mov rax, [rdx]
jmp rax
CtoL(EXECUTE)
DQ 4
DQ 'EXIT'
EXIT: DQ $+8
sub r12, 8
mov rbx, [r12] ; Pop I from Return Stack
jmp next
CtoL(EXIT)
DQ 5
DQ 'semic'
semic: DQ $+8
sub r12, 8
mov rbx, [r12] ; Pop I from Return Stack
jmp next
CtoL(semic)
DQ 4
DQ 'QUIT'
QUIT: DQ reset
CtoL(QUIT)
DQ 5
DQ 'ABORT'
ABORT: DQ dreset
CtoL(ABORT)
DQ 3
DQ 'DUP'
DUP: DQ $+8
; DUP ( a -- a a )
mov rax, [rbp-8]
pushrax:
mov [rbp], rax
add rbp, 8
jmp next
CtoL(DUP)
DQ 4
DQ 'OVER'
OVER: DQ $+8
; OVER ( a b -- a b a )
mov rax, [rbp-16]
jmp pushrax
CtoL(OVER)
DQ 4
DQ 'DROP'
DROP: DQ $+8
; DROP ( a -- )
sub rbp, 8
jmp next
CtoL(DROP)
DQ 4
DQ 'SWAP'
SWAP: DQ $+8
; SWAP ( a b -- b a )
mov rax, [rbp-16]
mov rdx, [rbp-8]
mov [rbp-16], rdx
mov [rbp-8], rax
jmp next
CtoL(SWAP)
DQ 3
DQ 'ROT'
ROT: DQ $+8
mov rdx, [rbp-24]
mov rcx, [rbp-16]
mov rax, [rbp-8]
mov [rbp-24], rcx
mov [rbp-16], rax
mov [rbp-8], rdx
jmp next
CtoL(ROT)
DQ 3
DQ 'NIP' ; CORE-EXT
NIP: DQ $+8
; NIP ( a b -- b )
mov rax, [rbp-8]
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(NIP)
DQ 4
DQ '?DUP'
qDUP: DQ $+8
; ?DUP ( nz -- nz nz ) when not zero
; ( 0 -- 0 ) when zero
mov rax, [rbp-8]
test rax, rax
jz next
jmp pushrax
CtoL(qDUP)
DQ 5
DQ '2OVER'
twoOVER:
DQ $+8
; 2OVER ( p q r s -- p q r s p q )
mov rcx, [rbp-32]
mov rdx, [rbp-24]
add rbp, 16
mov [rbp-16], rcx
mov [rbp-8], rdx
jmp next
CtoL(twoOVER)
DQ 5
DQ '2SWAP'
twoSWAP:
DQ stdexe
; 2SWAP ( p q r s -- r s p q )
; Equivalent to: rot >r rot r>
DQ ROT
DQ toR
DQ ROT
DQ Rfrom
DQ EXIT
CtoL(twoSWAP)
DQ 2
DQ '>R'
toR: DQ $+8
mov rax, [rbp-8]
mov [r12], rax
add r12, 8
sub rbp, 8
jmp next
CtoL(toR)
DQ 2
DQ 'R>'
Rfrom: DQ $+8
mov rax, [r12-8]
sub r12, 8
jmp pushrax
CtoL(Rfrom)
DQ 2
DQ 'R@'
Rfetch: DQ $+8
mov rax, [r12-8]
jmp pushrax
CtoL(Rfetch)
DQ 3
DQ '2>R' ; CORE-EXT
twotoR:
DQ $+8
; 2>R ( x1 x2 -- ) ( r: -- x1 x2 )
mov rax, [rbp-16]
mov rcx, [rbp-8]
add r12, 16
mov [r12-16], rax
mov [r12-8], rcx
sub rbp, 16
jmp next
CtoL(twotoR)
DQ 3
DQ '2R@' ; CORE-EXT
twoRfetch:
DQ $+8
; 2R@ ( -- x1 x2 ) ( r: x1 x2 -- x1 x2 )
mov rcx, [r12-16]
mov rax, [r12-8]
add rbp, 8
mov [rbp-8], rcx
jmp pushrax
CtoL(twoRfetch)
DQ 5
DQ 'stack'
STACK:
DQ $+8
; STACK ( -- addr-stack addr-base capacity )
mov rax, rbp
mov rcx, stack
add rbp, 16
mov [rbp-16], rax
mov [rbp-8], rcx
mov rax, stacklen
jmp pushrax
CtoL(STACK)
DQ 11
DQ 'returnst'
RETURNSTACK:
DQ $+8
; returnstack ( -- addr-stack addr-base capacity )
mov rax, r12
mov rcx, returnstack
add rbp, 16
mov [rbp-16], rax
mov [rbp-8], rcx
mov rax, returnstacklen
jmp pushrax
CtoL(RETURNSTACK)
DQ 4
DQ 'TRUE' ; CORE-EXT
TRUE: DQ $+8
pushtrue:
mov rax, -1
jmp pushrax
CtoL(TRUE)
DQ 5
DQ 'FALSE' ; CORE-EXT
z: ; Alternate label for when 0 is intended
FALSE: DQ $+8
pushfalse:
xor rax, rax
jmp pushrax
CtoL(FALSE)
; This primitive is the simplest condition test.
; It illustrates a technique for converting conditions
; to Forth boolean flags:
; - copy condition to carry flag
; - propagate carry flag to entire word using SBB.
; It contains entry points that are also used by other definitions.
DQ 2
DQ '0='
zequals:
DQ $+8
; 0= (0 -- -1)
; (x -- 0) when x is not 0
mov rax, [rbp-8]
ztoc: sub rax, 1 ; is-zero now in Carry flag
cprop: sbb rax, rax ; C=0 -> 0; C=1 -> -1
mov [rbp-8], rax
jmp next
CtoL(zequals)
DQ 1
DQ '='
equals: DQ $+8
mov rax, [rbp-16]
mov rcx, [rbp-8]
sub rbp, 8
sub rax, rcx
jmp ztoc
CtoL(equals)
DQ 2
DQ '<>' ; CORE-EXT
notequals:
DQ stdexe
; <> (a b -- -1)
; (a a -- 0)
DQ XOR
DQ zequals
DQ zequals
DQ EXIT
CtoL(notequals)
DQ 2
DQ '0<'
zless: DQ $+8
; 0< (0|+n -- false)
; 0< (n -- true) when n < 0
mov rax, [rbp-8]
; Shift sign bit into carry flag.
shl rax, 1
jmp cprop
CtoL(zless)
DQ 1
DQ '<'
lessthan:
DQ $+8
mov rax, [rbp-16]
mov rcx, [rbp-8]
xor rdx, rdx
cmp rax, rcx ; V iff rax < rcx
setl dl ; :todo: seems super clumsy
neg rdx
sub rbp, 8
mov [rbp-8], rdx
jmp next
CtoL(lessthan)
DQ 2
DQ 'U<'
Ulessthan:
DQ $+8
; < ( u1 u2 -- flag )
; flag is -1 (TRUE) if u1 < u2;
mov rax, [rbp-16]
mov rcx, [rbp-8]
sub rbp, 8
cmp rax, rcx ; C iff B > A
jmp cprop
CtoL(Ulessthan)
DQ 2
DQ '0>' ; CORE-EXT
zgreater:
DQ stdexe
; 0> (n -- b)
DQ NEGATE
DQ zless
DQ EXIT
CtoL(zgreater)
DQ 1
DQ '>'
greaterthan:
DQ stdexe
DQ SWAP
DQ lessthan
DQ EXIT
CtoL(greaterthan)
DQ 6
DQ 'branch'
BRANCH: DQ $+8
; Read the next word as a relative offset (in bytes);
; branch by adding offset to current CODEPOINTER.
brarbx: mov rcx, [rbx]
lea rbx, [rbx + rcx]
jmp next
CtoL(BRANCH)
DQ 7
DQ '0branch'
zBRANCH:
DQ $+8
; Read the next word as a relative offset (in bytes);
; pop the TOS and test it;
; if it is 0 then
; branch by adding offset to current CODEPOINTER.
sub rbp, 8
mov rax, [rbp]
test rax, rax
jz brarbx
add rbx, 8
jmp next
CtoL(zBRANCH)
DQ 1
DQ '!'
store:
DQ $+8
; ! ( w addr -- )
mov rax, [rbp-16]
mov rcx, [rbp-8]
sub rbp, 16
mov [rcx], rax
jmp next
CtoL(store)
DQ 2
DQ 'C!'
Cstore:
DQ $+8
; C! ( ch buf -- )
mov rax, [rbp-16]
mov rdx, [rbp-8]
sub rbp, 16
mov [rdx], al
jmp next
CtoL(Cstore)
DQ 1
DQ '@'
fetch: DQ $+8
; @ ( addr -- w )
mov rax, [rbp-8]
mov rax, [rax]
mov [rbp-8], rax
jmp next
CtoL(fetch)
DQ 2
DQ 'C@'
Cfetch: DQ $+8
; C@ ( addr -- ch )
mov rdx, [rbp-8]
xor rax, rax
mov al, [rdx]
mov [rbp-8], rax
jmp next
CtoL(Cfetch)
DQ 5
DQ 'CMOVE' ; STRING
CMOVE:
DQ $+8
; CMOVE ( from to u -- )
mov r8, [rbp-24]
mov r9, [rbp-16]
mov rdx, [rbp-8]
sub rbp, 24
mov rcx, 0
.l: cmp rcx, rdx
jz next
mov al, [r8+rcx]
mov [r9+rcx], al
inc rcx
jmp .l
CtoL(CMOVE)
DQ 6
DQ 'CMOVE>' ; STRING
CMOVEup:
DQ $+8
; CMOVE> ( from to u -- )
mov r8, [rbp-24]
mov r9, [rbp-16]
mov rcx, [rbp-8]
sub rbp, 24
.l: cmp rcx, 0
jz next
dec rcx
mov al, [r8+rcx]
mov [r9+rcx], al
jmp .l
CtoL(CMOVEup)
DQ 4
DQ 'FILL'
FILL:
DQ $+8
; FILL ( c-addr u char -- )
mov r9, [rbp-24]
mov rcx, [rbp-16]
mov rax, [rbp-8]
sub rbp, 24
mov rdx, 0
.l:
cmp rcx, rdx
jz next
mov [r9+rdx], al
inc rdx
jmp .l
CtoL(FILL)
DQ 2
DQ '+!'
plusstore:
DQ stdexe
; +! ( n addr -- )
DQ SWAP, OVER ; a n1 a
DQ fetch ; a n1 n2
DQ PLUS ; a n3
DQ SWAP ; n3 a
DQ store
DQ EXIT
CtoL(plusstore)
DQ 2
DQ 'OR'
OR: DQ $+8
; OR ( a b -- bitwise-or )
mov rax, [rbp-16]
mov rcx, [rbp-8]
or rax, rcx
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(OR)
DQ 3
DQ 'AND'
AND: DQ $+8
; AND ( a b -- bitwise-and )
mov rax, [rbp-16]
mov rcx, [rbp-8]
and rax, rcx
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(AND)
DQ 3
DQ 'XOR'
XOR: DQ $+8
; XOR ( a b -- bitwise-xor )
mov rax, [rbp-16]
mov rcx, [rbp-8]
xor rax, rcx
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(XOR)
DQ 6
DQ 'INVERT'
INVERT:
DQ stdexe
DQ TRUE
DQ XOR
DQ EXIT
CtoL(INVERT)
DQ 3
DQ 'bic'
BIC:
DQ stdexe
; BIt Clear (name from Alpha Architecture)
; bic ( na nb -- nc )
DQ INVERT
DQ AND
DQ EXIT
CtoL(BIC)
DQ 6
DQ 'LSHIFT'
LSHIFT:
DQ $+8
mov rax, [rbp-16]
mov rcx, [rbp-8]
shl rax, cl
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(LSHIFT)
DQ 6
DQ 'RSHIFT'
RSHIFT:
DQ $+8
mov rax, [rbp-16]
mov rcx, [rbp-8]
shr rax, cl
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(RSHIFT)
DQ 6
DQ 'NEGATE'
NEGATE:
DQ $+8
mov rax, [rbp-8]
neg rax
mov [rbp-8], rax
jmp next
CtoL(NEGATE)
DQ 1
DQ '+'
PLUS: DQ $+8
; + ( a b -- sum )
mov rax, [rbp-16]
mov rcx, [rbp-8]
add rax, rcx
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(PLUS)
DQ 1
DQ '-'
MINUS: DQ $+8
; - ( a b -- difference )
mov rax, [rbp-16]
mov rcx, [rbp-8]
sub rax, rcx
sub rbp, 8
mov [rbp-8], rax
jmp next
CtoL(MINUS)
DQ 2
DQ '1-'
oneminus:
DQ $+8
mov rax, [rbp-8]
sub rax, 1
mov [rbp-8], rax
jmp next
CtoL(oneminus)
DQ 3
DQ 'MIN'
MIN:
DQ stdexe
DQ OVER
DQ OVER
DQ greaterthan ; a b flag
DQ zBRANCH
DQ .s-$
DQ SWAP
.s:
DQ DROP
DQ EXIT
CtoL(MIN)
DQ 2
DQ 'M*'
Mstar:
DQ $+8
; m* ( n1 n2 -- d )
mov rax, [rbp-16] ; multiplier
mov rdx, [rbp-8] ; multiplicand
imul rdx
mov [rbp-16], rax
mov [rbp-8], rdx
jmp next
CtoL(Mstar)
DQ 3
DQ 'UM*'
UMstar:
DQ $+8
; um* ( u1 u2 -- ud )
mov rax, [rbp-16] ; multiplier
mov rdx, [rbp-8] ; multiplicand
mul rdx
mov [rbp-16], rax
mov [rbp-8], rdx
jmp next
CtoL(UMstar)
DQ 1
DQ '*'
star: DQ stdexe
; * ( n1 n2 -- n3 )
DQ Mstar
DQ DROP
DQ EXIT
CtoL(star)
DQ 6
DQ 'UM/MOD'
UMslashMOD:
DQ $+8
; UM/MOD ( ud-dividend u-divisor -- u-r u-q )
; Note: Double Single -> Single Single.
mov rax, [rbp-24] ; Dividend, least significant.
mov rdx, [rbp-16] ; Dividend, most significant.
mov rcx, [rbp-8] ; Divisor.
sub rbp, 8
div rcx
mov [rbp-16], rdx ; Deposit remainder.
mov [rbp-8], rax ; Deposit quotient.
jmp next
CtoL(UMslashMOD)
DQ 6
DQ 'SM/REM'
SMslashREM:
DQ $+8
; SM/REM ( d-dividend n-divisor -- n-quotient n-remainder )
mov rax, [rbp-24]
mov rdx, [rbp-16]
mov rcx, [rbp-8]
sub rbp, 8
idiv rcx
mov [rbp-16], rdx
mov [rbp-8], rax
jmp next
CtoL(SMslashREM)
DQ 2
DQ '1+'
oneplus:
DQ stdexe
DQ LIT, 1
DQ PLUS
DQ EXIT
CtoL(oneplus)
DQ 6
DQ 'WITHIN' ; CORE-EXT
WITHIN:
DQ stdexe
; WITHIN ( t p q -- flag )
; Implementation as per [FORTH1994]
DQ OVER, MINUS ; t p u1
DQ toR ; t p r: u1
DQ MINUS, Rfrom ; u2 u1
DQ Ulessthan ; flag
DQ EXIT
CtoL(WITHIN)
DQ 2
DQ 'D+' ; DOUBLE
Dplus: DQ $+8
; D+ ( d1 d2 -- d3 )
mov rax, [rbp-32] ; least significant part of augend
mov rdx, [rbp-24] ; most
mov r8, [rbp-16] ; least significant part of addend
mov r9, [rbp-8] ; most
add rax, r8
adc rdx, r9
sub rbp, 16
mov [rbp-16], rax
mov [rbp-8], rdx
jmp next
CtoL(Dplus)
DQ 3
DQ 'D>S' ; DOUBLE
DtoS: DQ stdexe
DQ DROP
DQ EXIT
CtoL(DtoS)
DQ 7
DQ 'DNEGATE' ; DOUBLE
DNEGATE:
DQ $+8
; m+- ( d -- -d )
mov rax, [rbp-16] ; Least significant
mov rcx, [rbp-8] ; Most significant
mov rdx, 0
sub rdx, rax
; Negated least significant now in rdx.
mov rax, 0
sbb rax, rcx
; Negated most significant now in rax.
mov [rbp-16], rdx
mov [rbp-8], rax
.x: jmp next
CtoL(DNEGATE)
DQ 3
DQ 'd+-' ; acornsoft
Dplusminus:
DQ stdexe
; d+- ( d1 n -- d2 )
; d2 shares sign with n
DQ OVER, XOR ; d1 s \ s < 0 when d1 needs negating
DQ zless
DQ zBRANCH
DQ .x-$
DQ DNEGATE ; -d1
.x: DQ EXIT
CtoL(Dplusminus)
DQ 4
DQ 'DABS' ; DOUBLE
DABS: DQ stdexe
; DABS ( d -- ud )
DQ LIT, 7 ; Arbitrary, should be positive.
DQ Dplusminus
DQ EXIT
CtoL(DABS)
DQ 7
DQ 'syscall'
SYSCALL:
DQ $+8
; syscall ( n -- rax )
mov rax, [rbp-8]
syscall
mov [rbp-8], rax
jmp next
CtoL(SYSCALL)
DQ 8
DQ 'syscall3'
SYSCALL3:
DQ $+8
; SYSCALL3 ( a b c n -- rax )
; Call syscall n with 3 arguments;
; return with RAX left on stack.
mov rdi, [rbp-32]
mov rsi, [rbp-24]
mov rdx, [rbp-16]
; syscall number
mov rax, [rbp-8]
sub rbp, 24
syscall
mov [rbp-8], rax
jmp next
CtoL(SYSCALL3)
DQ 8
DQ 'syscall6'
SYSCALL6:
DQ $+8
; SYSCALL6 ( a b c d e f n -- rax )
; Call syscall n with 6 argument;
; return with RAX left on stack.
mov rdi, [rbp-56]
mov rsi, [rbp-48]
mov rdx, [rbp-40]
mov r10, [rbp-32]
mov r8, [rbp-24]
mov r9, [rbp-16]
mov rax, [rbp-8]
sub rbp, 48
syscall
mov [rbp-8], rax
jmp next
CtoL(SYSCALL6)
DQ 7
DQ 'sysexit'
sysEXIT:
DQ $+8
mov rdi, 0
mov rax, 60
syscall
CtoL(sysEXIT)
DQ 3
DQ 'rsp'
fRSP:
DQ $+8
; Push the RSP register onto the Forth stack.
mov rax, rsp
jmp pushrax
CtoL(fRSP)
DQ 7
DQ '>NUMBER'
toNUMBER:
DQ stdexe
; ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 )
.begin:
DQ DUP ; ud c-addr u u
DQ zBRANCH
DQ .x - $
DQ ASCIItoDIGIT ; ud c-addr u n
DQ DUP, zless ; ud c-addr u n bf
DQ zBRANCH
DQ .ok - $
DQ DROP
.x:
DQ EXIT
.ok:
; ud c-addr u n
DQ toR ; ud c-addr u
DQ twoSWAP ; c-addr u ud
DQ BASE, fetch ; c-addr u ud base
DQ UDstar ; c-addr u ud
DQ Rfrom ; c-addr u ud n
DQ z ; c-addr u ud n 0
DQ Dplus ; c-addr u ud
DQ twoSWAP ; ud c-addr u
DQ BRANCH
DQ .begin - $
ASCIItoDIGIT:
DQ stdexe
; factor of >NUMBER
; ( c-addr1 u1 -- c-addr2 u2 n )
; c-addr and u are updated
; n is the next digit;
; n is negative if there is no next digit.
DQ OVER, Cfetch ; c-addr u ch
DQ DUP ; c-addr u ch ch
DQ LIT, '0' ; c-addr u ch ch '0'
DQ LIT, '9'+1 ; c-addr u ch ch '0' ':'
DQ WITHIN ; c-addr u ch bf
; ok if '0' <= ch <= '9'
DQ zBRANCH
DQ .then - $
DQ LIT, '0'
DQ MINUS ; c-addr u c
DQ digitadvance
DQ EXIT
.then:
DQ LIT, 'A'-10 ; c-addr u c 'A'
DQ MINUS ; c-addr u c
; Now, A -> 10, B -> 11, and so on.
DQ DUP ; c-addr u c c
DQ LIT, 10 ; c-addr u c c 10
DQ BASE, fetch ; c-addr u c c 10 base
DQ WITHIN ; c-addr u c bf
DQ zBRANCH
DQ .then2 - $
DQ digitadvance
DQ EXIT
.then2:
DQ DROP
DQ TRUE
DQ EXIT
digitadvance:
DQ stdexe
; Factor of ASCIItoDIGIT
; ( c-addr u c -- c-addr+1 u-1 c )
DQ ROT ; u c c-addr
DQ oneplus ; u c c-addr
DQ ROT ; c c-addr u
DQ oneminus ; c c-addr u
DQ ROT ; c-addr u c
DQ EXIT
CtoL(toNUMBER)
DQ 3
DQ 'ud*'
UDstar: