forked from vygr/OSX-Forth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forth.nasm
2841 lines (2571 loc) · 50.9 KB
/
forth.nasm
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A Forth by Chris Hinsley
;; nasm -f macho forth.nasm
;; ld -o forth -e _main forth.o
;; ./forth
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%define VERSION_NUM 30
; various buffer area sizes
%define DATA_STACK_SIZE 1024
%define USER_DEFS_SIZE (64*1024)
%define NUM_HASH_CHAINS 64
%define MAX_LINE_SIZE 128
%define SYS_exit 1
%define SYS_read 3
%define SYS_write 4
%define SYS_open 5
%define SYS_close 6
%define SYS_unlink 10
%define SYS_mprotect 74
%define SYS_fsync 95
%define SYS_rename 128
%define SYS_stat 188
%define SYS_lseek 199
%define SYS_fstat 189
%define SYS_ftruncate 201
%define PROT_READ 0x01 ;pages can be read
%define PROT_WRITE 0x02 ;pages can be written
%define PROT_EXEC 0x04 ;pages can be executed
%define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
%define PAGE_SIZE 4096
;;;;;;;;;;;;;;;;;;;;;;;;;;
; some NASM codeing macros
;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro loopstart 0
%push loopstart
%$loop_start:
%endmacro
%macro break 0
jmp %$loop_exit
%endmacro
%macro breakif 1
j%+1 %$loop_exit
%endmacro
%macro loopend 0
jmp %$loop_start
%$loop_exit:
%pop
%endmacro
%macro repeat 0
%push repeat
%$loop_start:
%endmacro
%macro until 1
j%-1 %$loop_start
%$loop_exit:
%pop
%endmacro
%macro if 1
%push if
j%-1 %$ifnot
%endmacro
%macro else 0
%ifctx if
%repl else
jmp %$ifend
%$ifnot:
%else
%error "expected `if' before `else'"
%endif
%endmacro
%macro endif 0
%ifctx if
%$ifnot:
%pop
%elifctx else
%$ifend:
%pop
%else
%error "expected `if' or `else' before `endif'"
%endif
%endmacro
;;;;;;;;;;;;;;;;
; base VM macros
;;;;;;;;;;;;;;;;
; eip Forths IP
; esp Forths R
; ebp Forths S
; ebx Forths TOS
; push on to return stack
%macro PUSHRSP 1
push %1
%endm
; pop top of return stack
%macro POPRSP 1
pop %1
%endm
; save into return stack
%macro PUTRSP 2
%if (%2 = 0)
mov [esp], %1
%elif ((%2 >= -128) && (%2 < 128))
mov [byte esp + %2], %1
%else
mov [long esp + %2], %1
%endif
%endm
; load from return stack
%macro PICKRSP 2
%if (%2 = 0)
mov %1, [esp]
%elif ((%2 >= -128) && (%2 < 128))
mov %1, [byte esp + %2]
%else
mov %1, [long esp + %2]
%endif
%endm
; set return stack
%macro SETRSP 1
mov esp, %1
%endm
; get return stack
%macro GETRSP 1
mov %1, esp
%endm
; adjust return stack
%macro ADDRSP 1
%if ((%1 >= -128) && (%1 < 128))
add esp, byte %1
%else
add esp, %1
%endif
%endm
; push on to data stack
%macro PUSHDSP 1
sub ebp, byte 4
mov [ebp], %1
%endm
; pop top of data stack
%macro POPDSP 1
mov %1, [ebp]
add ebp, byte 4
%endm
; save into data stack
%macro PUTDSP 2
%if (%2 = 0)
mov [ebp], %1
%elif ((%2 >= -128) && (%2 < 128))
mov [byte ebp + %2], %1
%else
mov [long ebp + %2], %1
%endif
%endm
; load from data stack
%macro PICKDSP 2
%if (%2 = 0)
mov %1, [ebp]
%elif ((%2 >= -128) && (%2 < 128))
mov %1, [byte ebp + %2]
%else
mov %1, [long ebp + %2]
%endif
%endm
; set data stack
%macro SETDSP 1
mov ebp, %1
%endm
; get data stack
%macro GETDSP 1
mov %1, ebp
%endm
; adjust data stack
%macro ADDDSP 1
%if ((%1 >= -128) && (%1 < 128))
add ebp, byte %1
%else
add ebp, %1
%endif
%endm
; load value onto data stack
%macro LOADTOS 1
PUSHDSP ebx
mov ebx, %1
%endm
; move from data to return stack
%macro TORSP 0
PUSHRSP ebx
POPDSP ebx
%endm
; move from return to data stack
%macro FROMRSP 0
PUSHDSP ebx
POPRSP ebx
%endm
; copy from return to data stack
%macro FETCHRSP 0
PUSHDSP ebx
PICKRSP ebx, 0
%endm
; align reg
%define DP_ALIGN 3
%macro ALIGNREG 1
add %1, byte DP_ALIGN
and %1, byte ~DP_ALIGN
%endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; dictionary building macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; format of dictionary entry flag byte
%define F_IMMED 0x80
%define F_HIDDEN 0x20
%define F_LENMASK 0x1f
%define NULL 0
%define H_LLINK 0
%define H_HLINK 4
%define H_NSIZE 8
%define H_NAME 9
%define XT_BODY -12
%define XT_LENGTH -8
%define XT_COMPILE -4
%define XT_SIZE 12
%macro defword 4
%push newword
%strlen len %1
align 4
dic_%3:
dd NULL ; LATEST list link
dd NULL ; hash chain link
db len + %2 ; flags + length byte
db %1 ; the name
dd %3 ; body pointer
dd %$code_end - %3 ; code length
dd %4 ; compile action word
%3:
%endm ; assembler code follows
%macro defword_end 0
%$code_end:
%pop
%endm
%macro defvar 4
defword %1, %2, %3, WORD_INLINE_COMMA
LOADTOS var_%3
ret
defword_end
align 4
var_%3:
dd %4
%endm
%macro defvar2 5
defword %1, %2, %3, WORD_INLINE_COMMA
LOADTOS var_%3
ret
defword_end
align 4
var_%3:
dd %4
dd %5
%endm
%macro defconst 4
defword %1, %2, %3, WORD_INLINE_COMMA
LOADTOS %4
ret
defword_end
%endm
;;;;;;;;;;;;;;;;;;;;;;;;;;
; entry point
;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION .text
global _main
_main:
; use mprotect to allow read/write/execute of the data section
mov edx, forth_start
and edx, -PAGE_SIZE ;start address
mov ecx, forth_end
sub ecx, edx ;length
mov ebx, PROT_ALL ;flags
push ebx
push ecx
push edx
push 0 ;padding
mov eax, SYS_mprotect
int 0x80
add esp, 16
jmp forth_start
SECTION .data
forth_start:
; init data and return stacks, saving initial positions
; in Forth vars R0 and S0
cld
GETRSP [var_WORD_SZ]
SETDSP [var_WORD_SZ]
ADDRSP -DATA_STACK_SIZE
GETRSP [var_WORD_RZ]
; link built in dictionary
mov esi, dictionary_start
xor edi, edi
repeat
lodsd
mov [eax + H_LLINK], edi
mov edi, eax
push esi
mov cl, [eax + H_NSIZE]
and ecx, F_LENMASK
lea esi, [eax + H_NAME]
call strhashi
and ebx, NUM_HASH_CHAINS-1
mov esi, hash_buckets
mov eax, [esi + (ebx * 4)]
mov [esi + (ebx * 4)], edi
mov [edi + H_HLINK], eax
pop esi
cmp esi, dictionary_end
until z
mov [var_WORD_LATEST], edi
; run temp interpreter loop till we can get into the real QUIT word
call WORD_LBRAC ; interpret state
LOADTOS 666q ; octal !
TORSP
LOADTOS 0
TORSP
LOADTOS bootfile
TORSP
call WORD_SYS_OPEN
call WORD_SYSCALL
ADDRSP 12
TORSP ; ( fd ) of "forth.f"
loopstart
LOADTOS tib_buffer
LOADTOS MAX_LINE_SIZE
FETCHRSP ; ( c-addr len fd )
call WORD_READLINE ; ( num flag flag )
call WORD_DROP2
LOADTOS tib_buffer
call WORD_SWAP
call WORD_INHASH
call WORD_STORE2
LOADTOS 0
call WORD_TOIN
call WORD_STORE
call WORD_INTERPRET
loopend ; and loop till QUIT takes over
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; a few case insensative string operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%macro to_lower 1
; lower case check
cmp %1, 'A'
if ge
cmp %1, 'Z'
if le
; make it lower case
add %1, byte 'a' - 'A'
endif
endif
%endm
strcpyi:
test ecx, ecx
if nz
strcpyi_l1:
lodsb
to_lower al
stosb
loop strcpyi_l1
endif
ret
strcmpi:
test ecx, ecx
if nz
strcmpi_l1:
lodsb
mov bl, [edi]
lea edi, [edi + 1]
to_lower al
to_lower bl
cmp bl, al
if z
loop strcmpi_l1
endif
endif
ret
;;;;;;;;;;;;;;;
; hash function
;;;;;;;;;;;;;;;
strhashi:
mov ebx, 5381
test ecx, ecx
if nz
mov edx, 33
strhashi_l1:
lodsb
movzx eax, al
to_lower eax
imul ebx, edx
add ebx, eax
loop strhashi_l1
endif
ret
;;;;;;;;;;;;;;;;;;;
; syscall functions
;;;;;;;;;;;;;;;;;;;
_syscall:
int 0x80
if c
neg eax
endif
ret
_lsyscall:
int 0x80
if c
not eax
not edx
add eax, 1
adc edx, 0
endif
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; built in variables
; STATE Is the interpreter executing code (0) or compiling a word (non-zero)?
; LATEST Points to the latest (most recently defined) word in the dictionary.
; DP Points to the next free byte of memory. When compiling, compiled words go here.
; S0 Stores the address of the top of the parameter stack.
; R0 The address of the top of the return stack.
; BASE The current base for printing and reading numbers.
; #IN The current input buffer descriptor.
; >IN The current input offset.
; SOURCEFD The current input source file descriptor.
; BLK The current block number.
; CHARBUF Single char buffer.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
defvar "state", 0, WORD_STATE, 0
defvar "dp", 0, WORD_DP, dictionary_start
defvar "latest", 0, WORD_LATEST, 0
defvar "s0", 0, WORD_SZ, 0
defvar "r0", 0, WORD_RZ, 0
defvar "base", 0, WORD_BASE, 10
defvar2 "#IN", 0, WORD_INHASH, 0, 0
defvar ">in", 0, WORD_TOIN, 0
defvar "sourcefd", 0, WORD_SOURCEFD, 0
defvar "blk", 0, WORD_BLK, 0
defvar "charbuf", 0, WORD_CHARBUF, 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; built in constants
; VERSION The current version of this FORTH.
; WORDBUF The address of the buffer WORD uses.
; LINESIZE The line buffer size.
; F_IMMED The IMMEDIATE flag's actual value.
; F_HIDDEN The HIDDEN flag's actual value.
; F_LENMASK The length mask in the flags/len byte.
; H_NSIZE The flags/len field offset.
; H_NAME The name field offset.
; XT_BODY The xt body pointer.
; XT_LENGTH The xt length field offset.
; XT_COMPILE The xt compile field offset.
; XT_SIZE The xt size offset.
; SYS_* The numeric codes of various syscalls.
; O_* Various sycall flags/modes.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
defconst "version", 0, WORD_VERSION, VERSION_NUM
defconst "wordbuf", 0, WORD_WORDBUF, word_buf
defconst "linesize", 0, WORD_LINESIZE, MAX_LINE_SIZE
defconst "f_immed", 0, WORD__F_IMMED, F_IMMED
defconst "f_hidden", 0, WORD__F_HIDDEN, F_HIDDEN
defconst "f_lenmask", 0, WORD__F_LENMASK, F_LENMASK
defconst "h_nsize", 0, WORD__H_NSIZE, H_NSIZE
defconst "h_name", 0, WORD__H_NAME, H_NAME
defconst "xt_body", 0, WORD__XT_BODY, XT_BODY
defconst "xt_length", 0, WORD__XT_LENGTH, XT_LENGTH
defconst "xt_compile", 0, WORD__XT_COMPILE, XT_COMPILE
defconst "xt_size", 0, WORD__XT_SIZE, XT_SIZE
defconst "sys_exit", 0, WORD_SYS_EXIT, SYS_exit
defconst "sys_open", 0, WORD_SYS_OPEN, SYS_open
defconst "sys_close", 0, WORD_SYS_CLOSE, SYS_close
defconst "sys_read", 0, WORD_SYS_READ, SYS_read
defconst "sys_write", 0, WORD_SYS_WRITE, SYS_write
defconst "sys_unlink", 0, WORD_SYS_UNLINK, SYS_unlink
defconst "sys_rename", 0, WORD_SYS_RENAME, SYS_rename
defconst "sys_ftruncate", 0, WORD_SYS_FTRUNCATE, SYS_ftruncate
defconst "sys_fsync", 0, WORD_SYS_FSYNC, SYS_fsync
defconst "sys_lseek", 0, WORD_SYS_LSEEK, SYS_lseek
defconst "sys_fstat", 0, WORD_SYS_FSTAT, SYS_fstat
defconst "sys_stat", 0, WORD_SYS_STAT, SYS_stat
defconst "o_rdonly", 0, WORD_O_RDONLY, 0x0
defconst "o_wronly", 0, WORD_O_WRONLY, 0x1
defconst "o_rdwr", 0, WORD_O_RDWR, 0x2
defconst "o_creat", 0, WORD_O_CREAT, 0x100
defconst "o_excl", 0, WORD_O_EXCL, 0x200
defconst "o_trunc", 0, WORD_O_TRUNC, 0x1000
defconst "o_append", 0, WORD_O_APPEND, 0x2000
defconst "o_nonblock", 0, WORD_O_NONBLOCK, 0x4000
;;;;;;;;;;;;;;;;;;;;;;;;;;;
; data stack ordering words
;;;;;;;;;;;;;;;;;;;;;;;;;;;
defword "dsp@", 0, WORD_DSPFETCH, WORD_INLINE_COMMA
PUSHDSP ebx
GETDSP ebx
ret
defword_end
defword "dsp!", 0, WORD_DSPSTORE, WORD_INLINE_COMMA
SETDSP ebx
POPDSP ebx
ret
defword_end
defword "drop", 0, WORD_DROP, WORD_INLINE_COMMA
POPDSP ebx
ret
defword_end
defword "swap", 0, WORD_SWAP, WORD_INLINE_COMMA
xchg ebx, [ebp]
ret
defword_end
defword "dup", 0, WORD_DUP, WORD_INLINE_COMMA
PUSHDSP ebx
ret
defword_end
defword "over", 0, WORD_OVER, WORD_INLINE_COMMA
PUSHDSP ebx
PICKDSP ebx, 4
ret
defword_end
defword "rot", 0, WORD_ROT, WORD_INLINE_COMMA
mov eax, ebx
PICKDSP ecx, 0
PICKDSP ebx, 4
PUTDSP eax, 0
PUTDSP ecx, 4
ret
defword_end
defword "-rot", 0, WORD_NROT, WORD_INLINE_COMMA
mov eax, ebx
PICKDSP ebx, 0
PICKDSP ecx, 4
PUTDSP ecx, 0
PUTDSP eax, 4
ret
defword_end
defword "2drop", 0, WORD_DROP2, WORD_INLINE_COMMA
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "2dup", 0, WORD_DUP2, WORD_INLINE_COMMA
PICKDSP eax, 0
ADDDSP -8
PUTDSP eax, 0
PUTDSP ebx, 4
ret
defword_end
defword "2swap", 0, WORD_SWAP2, WORD_INLINE_COMMA
mov eax, ebx
PICKDSP ecx, 0
PICKDSP ebx, 4
PICKDSP edx, 8
PUTDSP edx, 0
PUTDSP eax, 4
PUTDSP ecx, 8
ret
defword_end
defword "2rot", 0, WORD_ROT2, WORD_INLINE_COMMA
mov eax, ebx
PICKDSP ecx, 16
PICKDSP ebx, 12
PICKDSP edx, 8
PICKDSP edi, 4
PICKDSP esi, 0
PUTDSP edx, 16
PUTDSP edi, 12
PUTDSP esi, 8
PUTDSP eax, 4
PUTDSP ecx, 0
ret
defword_end
defword "?dup", 0, WORD_QDUP, WORD_INLINE_COMMA
test ebx, ebx
if nz
PUSHDSP ebx
endif
ret
defword_end
defword "!?dup", 0, WORD_NQDUP, WORD_INLINE_COMMA
test ebx, ebx
if z
PUSHDSP ebx
endif
ret
defword_end
defword "nip", 0, WORD_NIP, WORD_INLINE_COMMA
ADDDSP 4
ret
defword_end
defword "tuck", 0, WORD_TUCK, WORD_INLINE_COMMA
PICKDSP eax, 0
PUTDSP ebx, 0
PUSHDSP eax
ret
defword_end
defword "pick", 0, WORD_PICK, WORD_INLINE_COMMA
mov ebx, [ebp + (ebx * 4)]
ret
defword_end
defword "2tuck", 0, WORD_TUCK2, WORD_INLINE_COMMA
PICKDSP eax, 0
PICKDSP ecx, 4
PICKDSP edx, 8
ADDDSP -8
PUTDSP eax, 0
PUTDSP ecx, 4
PUTDSP edx, 8
PUTDSP ebx, 12
PUTDSP eax, 16
ret
defword_end
defword "2nip", 0, WORD_NIP2, WORD_INLINE_COMMA
PICKDSP eax, 0
ADDDSP 8
PUTDSP eax, 0
ret
defword_end
defword "2over", 0, WORD_OVER2, WORD_INLINE_COMMA
ADDDSP -8
PUTDSP ebx, 4
PICKDSP ebx, 16
PUTDSP ebx, 0
PICKDSP ebx, 12
ret
defword_end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; return stack ordering words
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
defword ">r", 0, WORD_TOR, WORD_INLINE_COMMA
TORSP
ret
defword_end
defword "r>", 0, WORD_FROMR, WORD_INLINE_COMMA
FROMRSP
ret
defword_end
defword "2>r", 0, WORD_TOR2, WORD_INLINE_COMMA
ADDRSP -8
PICKDSP ecx, 0
PUTRSP ebx, 0
PUTRSP ecx, 4
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "2r>", 0, WORD_FROMR2, WORD_INLINE_COMMA
ADDDSP -8
PUTDSP ebx, 4
PICKRSP ebx, 0
PICKRSP ecx, 4
PUTDSP ecx, 0
ADDRSP 8
ret
defword_end
defword "rsp@", 0, WORD_RSPFETCH, WORD_INLINE_COMMA
PUSHDSP ebx
GETRSP ebx
ret
defword_end
defword "r@", 0, WORD_RFETCH, WORD_INLINE_COMMA
PUSHDSP ebx
PICKRSP ebx, 0
ret
defword_end
defword "r!", 0, WORD_RSTORE, WORD_INLINE_COMMA
PUTRSP ebx, 0
POPDSP ebx
ret
defword_end
defword "2r@", 0, WORD_RFETCH2, WORD_INLINE_COMMA
ADDDSP -8
PUTDSP ebx, 4
PICKRSP ebx, 4
PICKRSP ecx, 0
PUTDSP ecx, 0
ret
defword_end
defword "rsp!", 0, WORD_RSPSTORE, WORD_INLINE_COMMA
SETRSP ebx
POPDSP ebx
ret
defword_end
defword "rdrop", 0, WORD_RDROP, WORD_INLINE_COMMA
ADDRSP 4
ret
defword_end
defword "2rdrop", 0, WORD_RDROP2, WORD_INLINE_COMMA
ADDRSP 8
ret
defword_end
defword "n>r", 0, WORD_NTOR, WORD_CALL_COMMA
PUSHDSP ebx
PICKRSP eax, 0
mov ecx, ebx
inc ecx
neg ebx
lea esp, [esp + (ebx * 4)]
mov esi, ebp
mov edi, esp
rep movsd
mov ebp, esi
POPDSP ebx
jmp eax
defword_end
defword "nr>", 0, WORD_NFROMR, WORD_CALL_COMMA
PUSHDSP ebx
POPRSP eax
PICKRSP ebx, 0
inc ebx
mov ecx, ebx
neg ebx
lea ebp, [ebp + (ebx * 4)]
mov esi, esp
mov edi, ebp
rep movsd
mov esp, esi
POPDSP ebx
jmp eax
defword_end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; memory fetch and store words
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
defword "!", 0, WORD_STORE, WORD_INLINE_COMMA
PICKDSP eax, 0
mov [ebx], eax
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "@", 0, WORD_FETCH, WORD_INLINE_COMMA
mov ebx, [ebx]
ret
defword_end
defword "+!", 0, WORD_ADDSTORE, WORD_INLINE_COMMA
PICKDSP eax, 0
add [ebx], eax
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "-!", 0, WORD_SUBSTORE, WORD_INLINE_COMMA
PICKDSP eax, 0
sub [ebx], eax
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "c!", 0, WORD_STOREBYTE, WORD_INLINE_COMMA
PICKDSP eax, 0
mov [ebx], al
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "c+!", 0, WORD_ADDBYTE, WORD_INLINE_COMMA
PICKDSP eax, 0
add [ebx], al
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "c@", 0, WORD_FETCHBYTE, WORD_INLINE_COMMA
mov eax, ebx
xor ebx, ebx
mov bl, [eax]
ret
defword_end
defword "w!", 0, WORD_STORESHORT, WORD_INLINE_COMMA
PICKDSP eax, 0
mov [ebx], ax
PICKDSP ebx, 4
ADDDSP 8
ret
defword_end
defword "w@", 0, WORD_FETCHSHORT, WORD_INLINE_COMMA
mov eax, ebx
xor ebx, ebx
mov bx, [eax]
ret
defword_end
defword "2!", 0, WORD_STORE2, WORD_INLINE_COMMA
PICKDSP ecx, 4
PICKDSP edx, 0
mov [ebx + 4], ecx
mov [ebx], edx
PICKDSP ebx, 8
ADDDSP 12
ret
defword_end
defword "2@", 0, WORD_FETCH2, WORD_INLINE_COMMA
ADDDSP -4
mov ecx, [ebx +4]
mov ebx, [ebx]
PUTDSP ecx, 0
ret
defword_end
defword "blank", 0, WORD_BLANK, WORD_CALL_COMMA
mov ecx, ebx
PICKDSP ebx, 4
PICKDSP edi, 0
ADDDSP 8
mov eax, 0x20
rep stosb
ret
defword_end
defword "erase", 0, WORD_ERASE, WORD_CALL_COMMA
mov ecx, ebx
PICKDSP ebx, 4
PICKDSP edi, 0
ADDDSP 8
xor eax, eax
rep stosb
ret
defword_end
defword "fill", 0, WORD_FILL, WORD_CALL_COMMA
mov eax, ebx
PICKDSP ebx, 8
PICKDSP edi, 4
PICKDSP ecx, 0
ADDDSP 12
rep stosb
ret
defword_end
defword "cmove>", 0, WORD_CMOVEB, WORD_CALL_COMMA
mov ecx, ebx
PICKDSP ebx, 8
PICKDSP esi, 4
PICKDSP edi, 0
ADDDSP 12
lea esi, [esi + ecx - 1]
lea edi, [edi + ecx - 1]
std
rep movsb
cld
ret
defword_end
defword "cmove", 0, WORD_CMOVE, WORD_CALL_COMMA
mov ecx, ebx
PICKDSP ebx, 8
PICKDSP esi, 4
PICKDSP edi, 0
ADDDSP 12
rep movsb
ret
defword_end
defword "move", 0, WORD_MOVE, WORD_CALL_COMMA
mov ecx, ebx
PICKDSP ebx, 8
PICKDSP esi, 4
PICKDSP edi, 0
ADDDSP 12
cmp esi, edi
if a
rep movsb
else
lea esi, [esi + ecx -1]
lea edi, [edi + ecx -1]
std
rep movsb
cld
endif
ret
defword_end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; single precision alu words
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
defword "+", 0, WORD_ADD, WORD_INLINE_COMMA
add ebx, [ebp]
ADDDSP 4
ret
defword_end
defword "-", 0, WORD_SUB, WORD_INLINE_COMMA
mov eax, ebx
POPDSP ebx
sub ebx, eax
ret
defword_end
defword "*", 0, WORD_MULL, WORD_INLINE_COMMA
imul ebx, [ebp]
ADDDSP 4