-
Notifications
You must be signed in to change notification settings - Fork 1
/
forth.bas
1716 lines (1497 loc) · 55.6 KB
/
forth.bas
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
REM IA-32 Assembly Language Forth in BBC BASIC for Windows
REM Based on Linux JonesForth at http://annexia.org/forth/
REM Adapted by Richard Russell, http://www.rtrussell.co.uk
REM Version 0.16, 07-Sep-2009 Added ERR, ROLL, OSCLI, SYSCALL
REM Version 0.17, 08-Sep-2009 Added Load/FreeLibrary, GetProcAddress
REM Version 0.18, 09-Sep-2009 Added DO, LOOP, +LOOP, I, J
REM Version 0.19, 10-Sep-2009 Added ?DO, LEAVE, UNLOOP, VERSION$
REM Version 0.20, 11-Sep-2009 Added ESCape test in loops
REM Version 0.21, 12-Sep-2009 Added hwnd, U/MOD; fixed Divmod
REM Version 0.22, 13-Sep-2009 Added stack underflow check; Home, End
REM Version 0.23, 19-Sep-2009 Added INKEY, hfiles, BGET, BPUT, memhdc
REM Version 0.24, 20-Sep-2009 Added more 'system variables', UM*
REM Version 0.25, 21-Sep-2009 Suppress echo while EXECing
REM Version 0.26, 22-Sep-2009 Save reg's in OSCLI; test 'kill' flag
REM Version 0.27, 27-Sep-2009 Corrected ?DO, changed TRUE to -1
REM Version 0.28, 28-Sep-2009 Added M*, */, */MOD, U<, U>, S>D, 2*, 2/, U2/
REM Version 0.29, 29-Sep-2009 Added SOURCE, >IN ; correct LEAVE
REM Version 0.30, 30-Sep-2009 Added UM/MOD, SM/REM, FM/MOD, R@, D+, D-
REM Version 0.31, 01-Oct-2009 Backslash (\) changed to a word
REM Version 0.32, 02-Oct-2009 Added compliant CREATE...DOES>, EVALUATE
REM Version 0.33, 03-Oct-2009 Added FILL, ACCEPT, SETSRC, RESTORE
REM Version 0.34, 04-Oct-2009 Revert to alternative CREATE...DOES>
REM Version 0.35, 05-Oct-2009 Name change 'builds' -> 'dodoes'
REM Version 0.36, 06-Oct-2009 Re-arrange memory management; fix +LOOP
REM Version 0.37, 10-Oct-2009 Added POLL for Windows event handling
REM Version 0.38, 19-Oct-2009 Updated ROT,-ROT to Jonesforth v47
Version$ = "0.38"
JONES_VERSION = 47
SYS "SetWindowText", @hwnd%, "BB4Wforth version "+Version$
F_IMMED = &80
F_HIDDEN = &20
F_LENMASK = &1F
EXCEPTION_CONTINUE_EXECUTION = -1
DIM F% 3-(END AND 3), C% 3327, L% -1, G% 2047, D% (HIMEM-END-6000) OR 3, T% -1
Code% = C%
Limit% = L%
data_segment = D%
data_segment_top = T%
return_stack_top = T%
input_buffer = !332 + 32768
oscli_buffer = !336
optval = ^@hfile%(0)-6
exchan = ^@hfile%(0)+88
bflags = ^@flags%+3
ontime = ^!388
onclose = ^!392
onmove = ^!396
onsys = ^!400
onmouse = ^!404
errval = ^?418
eventq = @vdu%-144
evtqw = @vdu%+205
evtqr = @vdu%+206
PROCassemble
OSCLI "EXEC """+@dir$+"bb4wforth.f"""
SYS "SetUnhandledExceptionFilter", exception_filter
ON ERROR PRINT 'REPORT$
CALL _start
END
DEF PROCassemble
FOR Pass% = 8 TO 10 STEP 2
P% = Code%
Q% = data_segment
L% = Limit%
M% = data_segment_top
Link% = 0
SWAP P%,Q% : SWAP L%,M% : REM switch to .data
[OPT Pass%
; Forth variables (data segment):
.var_State dd 0
.var_Here dd LastData%
.var_Latest dd LastLink%
.var_Sz dd 0
.var_Base dd 10
.var_Pnsptr dd 0
.interpret_is_lit dd 0
.poll_latest dd 0
.srcbuff dd input_buffer
.srcchrs dd 0
.srccurr dd 0
.savbuff dd 0 ; used by Evaluate
.savchrs dd 0 ; used by Evaluate
.savcurr dd 0 ; used by Evaluate
._word_buffer db STRING$(32, " ")
.cold_start ; High-level code without a codeword.
dd Quit
]
SWAP P%,Q% : SWAP L%,M% : REM switch to .code
[OPT Pass%
.exception_filter
mov edx,[esp+4] ; lpEXCEPTION_POINTERS
mov edx,[edx+4] ; lpCONTEXT
mov dword [edx+184],_abort ; eip
mov eax,[var_Sz]
mov dword [edx+196],eax ; esp
mov eax,EXCEPTION_CONTINUE_EXECUTION
ret 4
._abortmsg
db 6 : db 3 : db 13 : db 10
db "Abort error: "
._abortmsgend
._stackmsg
db 6 : db 3 : db 13 : db 10
db "Stack error: "
._stackmsgend
._escmsg
db 6 : db 3 : db 13 : db 10
db "Escape"
db 13 : db 10
._escmsgend
; Assembler entry points:
._quit
mov byte [optval],0 ; zero optval
mov dword [exchan],0 ; zero exchan
call "osrdch" ; quits if kill flag set!
._escape
bt [bflags],0 ; test kill flag
jc _quit
btr [bflags],7 ; clear escape flag
mov byte [errval],17
mov edx,_escmsg ; error message
mov ecx,_escmsgend-_escmsg ; length of string
call _tell
jmps _start_nosz
._stackerr
mov byte [errval],57
mov edx,_stackmsg ; error message
mov ecx,_stackmsgend-_stackmsg ; length of string
jmps _rpt_error
._abort
mov byte [errval],58
mov edx,_abortmsg ; error message
mov ecx,_abortmsgend-_abortmsg ; length of string
._rpt_error
call _report_error
._start
cld
mov [var_Sz],esp ; Save the initial data stack pointer
._start_nosz
mov dword [srcbuff],input_buffer
mov dword [srcchrs],0
mov dword [srccurr],0
mov ebp,return_stack_top ; Initialise the return stack.
mov esi,cold_start ; Initialise interpreter.
lodsd : jmp [eax] ; Run interpreter!
; DOCOL - the interpreter!
.Docol
lea ebp,[ebp-4]
mov [ebp],esi ; push esi on to the return stack
lea esi,[eax+4] ; esi points to first data word
lodsd : jmp [eax]
;REM BUILT-IN WORDS ---------------------------------------------------
OPT FNdefcode("DROP", 0, Drop)
pop eax ; drop top of stack
lodsd : jmp [eax]
OPT FNdefcode("DUP", 0, Dup)
push dword [esp] ; duplicate top of stack
lodsd : jmp [eax]
OPT FNdefcode("SWAP", 0, Swap)
pop eax ; swap top two elements on stack
pop ebx
push eax
push ebx
lodsd : jmp [eax]
OPT FNdefcode("OVER", 0, Over)
push dword [esp+4] ; push second element of stack
lodsd : jmp [eax]
OPT FNdefcode("ROT", 0, Rot) ; n.b. Jonesforth v47
pop eax
pop ebx
pop ecx
push ebx
push eax
push ecx
lodsd : jmp [eax]
OPT FNdefcode("-ROT", 0, Nrot) ; n.b. Jonesforth v47
pop eax
pop ebx
pop ecx
push eax
push ecx
push ebx
lodsd : jmp [eax]
OPT FNdefcode("2DROP", 0, Twodrop)
pop eax ; drop top two elements of stack
pop eax
lodsd : jmp [eax]
OPT FNdefcode("2DUP", 0, Twodup)
push dword [esp+4] ; duplicate top two elements of stack
push dword [esp+4]
lodsd : jmp [eax]
OPT FNdefcode("2SWAP", 0, Twoswap)
pop eax ; swap top two pairs of elements of stack
pop ebx
pop ecx
pop edx
push ebx
push eax
push edx
push ecx
lodsd : jmp [eax]
OPT FNdefcode("?DUP", 0, Qdup)
mov eax,[esp] ; duplicate top of stack if non-zero
test eax,eax
jz _nodup
push eax
._nodup
lodsd : jmp [eax]
OPT FNdefcode("1+", 0, Incr)
inc dword [esp] ; increment top of stack
lodsd : jmp [eax]
OPT FNdefcode("1-", 0, Decr)
dec dword [esp] ; decrement top of stack
lodsd : jmp [eax]
OPT FNdefcode("4+", 0, Incr4)
add dword [esp],4 ; add 4 to top of stack
lodsd : jmp [eax]
OPT FNdefcode("4-", 0, Decr4)
sub dword [esp],4 ; subtract 4 from top of stack
lodsd : jmp [eax]
OPT FNdefcode("+", 0, Add)
pop eax ; get top of stack
add dword [esp],eax ; and add it to next word on stack
lodsd : jmp [eax]
OPT FNdefcode("D+", 0, Dadd) ; Added by RTR
pop edx ; MS 32-bits
pop eax ; LS 32-bits (Forth is big-endian)
add dword [esp+4],eax
adc dword [esp],edx
lodsd : jmp [eax]
OPT FNdefcode("-", 0, Sub)
pop eax ; get top of stack
sub dword [esp],eax ; and subtract it from next word on stack
lodsd : jmp [eax]
OPT FNdefcode("D-", 0, Dsub) ; Added by RTR
pop edx ; MS 32-bits
pop eax ; LS 32-bits (Forth is big-endian)
sub dword [esp+4],eax
sbb dword [esp],edx
lodsd : jmp [eax]
OPT FNdefcode("*", 0, Mul)
pop eax
pop ebx
imul ebx ; signed (or unsigned) multiply
push eax ; ignore MS 32 bits
lodsd : jmp [eax]
OPT FNdefcode("UM*", 0, Umul64) ; Added by RTR
pop eax
pop ebx
mul ebx ; unsigned multiply
push eax ; LS 32 bits (Forth is big-endian)
push edx ; MS 32 bits
lodsd : jmp [eax]
OPT FNdefcode("M*", 0, Mul64) ; Added by RTR
pop eax
pop ebx
imul ebx ; signed multiply
push eax ; LS 32 bits (Forth is big-endian)
push edx ; MS 32 bits
lodsd : jmp [eax]
OPT FNdefcode("/MOD", 0, Divmod)
pop ebx
pop eax
cdq ; RTR correction
idiv ebx
push edx ; push remainder
push eax ; push quotient
lodsd : jmp [eax]
OPT FNdefcode("U/MOD", 0, Udivmod) ; Added by RTR (unsigned)
pop ebx
pop eax
xor edx,edx
div ebx
push edx ; push remainder
push eax ; push quotient
lodsd : jmp [eax]
OPT FNdefcode("UM/MOD", 0, UMdivmod) ; Added by RTR (unsigned)
pop ebx
pop edx
pop eax
div ebx
push edx ; push remainder
push eax ; push quotient
lodsd : jmp [eax]
OPT FNdefcode("SM/REM", 0, SMdivrem) ; Added by RTR (signed)
pop ebx
pop edx
pop eax
idiv ebx
push edx ; push remainder
push eax ; push quotient
lodsd : jmp [eax]
OPT FNdefcode("FM/MOD", 0, FMdivmod) ; Added by RTR (floored)
pop ebx
pop edx
pop eax
idiv ebx
or edx,edx
jz _fmmodx ; No remainder
or eax,eax
jns _fmmodx ; Quotient is positive
dec eax
add edx,ebx
._fmmodx
push edx ; push remainder
push eax ; push quotient
lodsd : jmp [eax]
OPT FNdefcode("*/", 0, Muldiv) ; Added by RTR
pop ecx
pop ebx
pop eax
imul ebx
idiv ecx
push eax
lodsd : jmp [eax]
OPT FNdefcode("*/MOD", 0, Muldivmod) ; Added by RTR
pop ecx
pop ebx
pop eax
imul ebx
idiv ecx
push edx ; push remainder
push eax ; push quotient
lodsd : jmp [eax]
OPT FNdefcode("2*", 0, Twomul) ; Added by RTR
shl dword [esp],1
lodsd : jmp [eax]
OPT FNdefcode("2/", 0, Twodiv) ; Added by RTR
sar dword [esp],1
lodsd : jmp [eax]
OPT FNdefcode("U2/", 0, Utwodiv) ; Added by RTR
shr dword [esp],1
lodsd : jmp [eax]
OPT FNdefcode("=", 0, Equ)
pop eax ; top two words are equal?
pop ebx
cmp eax,ebx
sete al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("<>", 0, Nequ)
pop eax ; top two words are not equal?
pop ebx
cmp eax,ebx
setne al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("<", 0, Lt)
pop eax
pop ebx
cmp ebx,eax
setl al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("U<", 0, Ult) ; Added by RTR
pop eax
pop ebx
cmp ebx,eax
setb al
movzx eax,al
neg eax
push eax
lodsd : jmp [eax]
OPT FNdefcode(">", 0, Gt)
pop eax
pop ebx
cmp ebx,eax
setg al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("U>", 0, Ugt) ; Added by RTR
pop eax
pop ebx
cmp ebx,eax
seta al
movzx eax,al
neg eax
push eax
lodsd : jmp [eax]
OPT FNdefcode("<=", 0, Le)
pop eax
pop ebx
cmp ebx,eax
setle al
neg eax ; RTR correction (TRUE = -1)
movzx eax,al
push eax
lodsd : jmp [eax]
OPT FNdefcode(">=", 0, Ge)
pop eax
pop ebx
cmp ebx,eax
setge al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("0=", 0, Zequ)
pop eax ; top of stack equals 0?
test eax,eax
setz al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("0<>", 0, Znequ)
pop eax ; top of stack not 0?
test eax,eax
setnz al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("0<", 0, Zlt)
pop eax
test eax,eax
setl al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("0>", 0, Zgt)
pop eax
test eax,eax
setg al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("0<=", 0, Zle)
pop eax
test eax,eax
setle al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("0>=", 0, Zge)
pop eax
test eax,eax
setge al
movzx eax,al
neg eax ; RTR correction (TRUE = -1)
push eax
lodsd : jmp [eax]
OPT FNdefcode("S>D", 0, Stod) ; Added by RTR
pop eax
cdq
push eax ; LS 32 bits (Forth is big-endian)
push edx ; MS 32 bits
lodsd : jmp [eax]
OPT FNdefcode("AND", 0, And)
pop eax ; bitwise AND
and [esp],eax
lodsd : jmp [eax]
OPT FNdefcode("OR", 0, Or)
pop eax ; bitwise OR
or [esp],eax
lodsd : jmp [eax]
OPT FNdefcode("XOR", 0, Xor)
pop eax ; bitwise XOR
xor [esp],eax
lodsd : jmp [eax]
OPT FNdefcode("INVERT", 0, Invert)
not dword [esp] ; bitwise NOT
lodsd : jmp [eax]
OPT FNdefcode("EXIT", 0, Exit)
mov esi,[ebp]
lea ebp,[ebp+4]
lodsd : jmp [eax]
OPT FNdefcode("LIT", 0, Lit)
lodsd
push eax ; push the literal number onto stack
lodsd : jmp [eax]
OPT FNdefcode("ROLL", 0, Roll) ; Added by RTR
pop ecx
jecxz _roll_next
lea edi,[esp+ecx*4]
lea ebx,[edi-4]
mov eax,[edi]
std
xchg esi,ebx
rep movsD
xchg esi,ebx
cld
mov [esp],eax
._roll_next
lodsd : jmp [eax]
;REM MEMORY -----------------------------------------------------------
OPT FNdefcode("!", 0, Store)
pop ebx ; address to store at
pop eax ; data to store there
mov [ebx],eax ; store it
lodsd : jmp [eax]
OPT FNdefcode("@", 0, Fetch)
pop ebx ; address to fetch
mov eax,[ebx] ; fetch it
push eax ; push value onto stack
lodsd : jmp [eax]
OPT FNdefcode("+!", 0, Addstore)
pop ebx ; address
pop eax ; the amount to add
add [ebx],eax ; add it
lodsd : jmp [eax]
OPT FNdefcode("-!", 0, Substore)
pop ebx ; address
pop eax ; the amount to subtract
sub [ebx],eax ; subtract it
lodsd : jmp [eax]
OPT FNdefcode("C!", 0, Storebyte)
pop ebx ; address to store at
pop eax ; data to store there
mov [ebx],al ; store it
lodsd : jmp [eax]
OPT FNdefcode("C@", 0, Fetchbyte)
pop ebx ; address to fetch
movzx eax,byte [ebx] ; fetch byte
push eax ; push value onto stack
lodsd : jmp [eax]
OPT FNdefcode("C@C!", 0, Ccopy)
mov ebx,[esp+4] ; source address
mov al,[ebx] ; get source character
pop edi ; destination address
stosb ; copy to destination
push edi ; increment destination address
inc dword [esp+4] ; increment source address
lodsd : jmp [eax]
OPT FNdefcode("CMOVE", 0, Cmove)
mov edx,esi ; preserve esi
pop ecx ; length
pop edi ; destination address
pop esi ; source address
cmp edi,esi ; Added by RTR...
jc _cmove_down ; ...to ensure correct
lea edi,[edi+ecx-1] ; ...operation when
lea esi,[esi+ecx-1] ; ...source and dest
std ; ...are overlapping
._cmove_down
rep movsb ; copy source to destination
cld ; Added by RTR
mov esi,edx ; restore esi
lodsd : jmp [eax]
OPT FNdefcode("FILL", 0, Fill) ; Added by RTR
pop eax ; char
pop ecx ; count
pop edi ; c-addr
cmp ecx,0
jle _fill_next
rep stosb
._fill_next
lodsd : jmp [eax]
;REM BUILT-IN VARIABLES -----------------------------------------------
OPT FNdefconst("STATE", 0, State, var_State)
OPT FNdefconst("HERE", 0, Here, var_Here)
OPT FNdefconst("LATEST", 0, Latest, var_Latest)
OPT FNdefconst("S0", 0, Sz, var_Sz)
OPT FNdefconst("BASE", 0, Base, var_Base)
OPT FNdefconst("PNSPTR", 0, Pnsptr, var_Pnsptr); Added by RTR
OPT FNdefconst("ERR", 0, Err, errval) ; Added by RTR
OPT FNdefconst("hwnd", 0, Hwnd, ^@hwnd%) ; Added by RTR
OPT FNdefconst("memhdc", 0, Memhdc, ^@memhdc%) ; Added by RTR
OPT FNdefconst("prthdc", 0, Prthdc, ^@prthdc%) ; Added by RTR
OPT FNdefconst("hcsr", 0, Hcsr, ^@hcsr%) ; Added by RTR
OPT FNdefconst("hpal", 0, Hpal, ^@hpal%) ; Added by RTR
OPT FNdefconst("midiid", 0, Midiid, ^@midi%) ; Added by RTR
OPT FNdefconst("hfiles", 0, Hfiles, ^@hfile%(0)) ; Added by RTR
OPT FNdefconst("flags", 0, Flags, ^@flags%) ; Added by RTR
OPT FNdefconst("vduvar", 0, Vduvar, ^@vdu%) ; Added by RTR
OPT FNdefconst("msg", 0, Msg, ^@msg%) ; Added by RTR
OPT FNdefconst("wparam", 0, Wparam, ^@wparam%) ; Added by RTR
OPT FNdefconst("lparam", 0, Lparam, ^@lparam%) ; Added by RTR
OPT FNdefconst("ox", 0, Ox, ^@ox%) ; Added by RTR
OPT FNdefconst("oy", 0, Oy, ^@oy%) ; Added by RTR
;REM BUILT-IN READ-ONLY VALUES ----------------------------------------
OPT FNdefconst("VERSION", 0, Version, JONES_VERSION)
OPT FNdefconst("R0", 0, Rz, return_stack_top)
OPT FNdefconst("DOCOL", 0, Docol_, Docol)
OPT FNdefconst("F_IMMED", 0, Fimmed, F_IMMED)
OPT FNdefconst("F_HIDDEN", 0, Fhidden, F_HIDDEN)
OPT FNdefconst("F_LENMASK", 0, Flenmask, F_LENMASK)
OPT FNdefconst("SYS_EXIT", 0, Sys_exit, 1)
OPT FNdefconst("SYS_READ", 0, Sys_read, 3)
OPT FNdefconst("SYS_OPEN", 0, Sys_open, 5)
OPT FNdefconst("SYS_CLOSE", 0, Sys_close, 6)
OPT FNdefconst("SYS_BRK", 0, Sys_brk, 45)
OPT FNdefconst("O_RDONLY", 0, O_rdonly, 0)
OPT FNdefconst("O_RDWR", 0, O_rdwr, 2)
OPT FNdefconst("O_CREAT", 0, O_creat, %0001000000)
OPT FNdefconst("O_TRUNC", 0, O_trunc, %1000000000)
OPT FNdefcode("VERSION$", 0, VersionS) ; Added by RTR
push !^Version$ ; String address
push LEN(Version$) ; String length
lodsd : jmp [eax]
OPT FNdefcode("cmd$", 0, CmdS) ; Added by RTR
push !^@cmd$ ; String address
push LEN(@cmd$) ; String length
lodsd : jmp [eax]
OPT FNdefcode("dir$", 0, DirS) ; Added by RTR
push !^@dir$ ; String address
push LEN(@dir$) ; String length
lodsd : jmp [eax]
OPT FNdefcode("lib$", 0, LibS) ; Added by RTR
push !^@lib$ ; String address
push LEN(@lib$) ; String length
lodsd : jmp [eax]
OPT FNdefcode("tmp$", 0, TmpS) ; Added by RTR
push !^@tmp$ ; String address
push LEN(@tmp$) ; String length
lodsd : jmp [eax]
OPT FNdefcode("usr$", 0, UsrS) ; Added by RTR
push !^@usr$ ; String address
push LEN(@usr$) ; String length
lodsd : jmp [eax]
;REM RETURN STACK -----------------------------------------------------
OPT FNdefcode(">R", 0, Tor)
pop eax ; pop parameter stack into eax
lea ebp,[ebp-4]
mov [ebp],eax ; push it on to the return stack
lodsd : jmp [eax]
OPT FNdefcode("R>", 0, Fromr)
mov eax,[ebp] ; pop return stack on to eax
lea ebp,[ebp+4]
push eax ; and push on to parameter stack
lodsd : jmp [eax]
OPT FNdefcode("R@", 0, Rfetch) ; Added by RTR
mov eax,[ebp]
push eax
lodsd : jmp [eax]
OPT FNdefcode("RSP@", 0, Rspfetch)
push ebp
lodsd : jmp [eax]
OPT FNdefcode("RSP!", 0, Rspstore)
pop ebp
lodsd : jmp [eax]
OPT FNdefcode("RDROP", 0, Rdrop)
lea ebp,[ebp+4] ; pop return stack and throw away
lodsd : jmp [eax]
;REM PARAMETER (DATA) STACK -------------------------------------------
OPT FNdefcode("DSP@", 0, Dspfetch)
mov eax,esp
push eax
lodsd : jmp [eax]
OPT FNdefcode("DSP!", 0, Dspstore)
pop esp
lodsd : jmp [eax]
;REM INPUT AND OUTPUT -------------------------------------------------
OPT FNdefcode("KEY", 0, Key)
call _inp
push eax ; push char value on stack
lodsd : jmp [eax]
._inp
mov eax,[srccurr]
cmp eax,[srcchrs] ; Anything remaining in input buffer?
jz _inp_eol
ja _inp_refill
mov eax,[srcbuff] ; Address of input buffer
add eax,[srccurr] ; Add offset to current input
movzx eax,byte [eax]
inc dword [srccurr]
ret
._inp_refill
mov edx,input_buffer
cmp edx,[srcbuff] ; Evaluating ?
jnz _inp
call _osline
mov [srcchrs],eax
mov dword,[srccurr],0
jmps _inp
._inp_eol
inc dword [srccurr]
mov eax,13
ret
OPT FNdefcode("EMIT", 0, Emit)
pop eax
call "oswrch"
lodsd : jmp [eax]
OPT FNdefcode("WORD", 0, Word_)
call _word
push edi ; push base address
push ecx ; push length
lodsd : jmp [eax]
._word
call _inp ; get next char, returned in al
cmp al,ASC" " ; is <= blank?
jbe _word ; if so, keep looking
mov edi,_word_buffer ; pointer to return buffer
._word_loop
stosb ; add character to return buffer
call _inp ; get next char, returned in al
cmp al,ASC" " ; is <= blank?
ja _word_loop ; if not, keep looping
mov ecx,_word_buffer
sub edi,ecx ; get length
xchg edi,ecx
ret
OPT FNdefcode("\", F_IMMED, Backslash) ; Added by RTR
._word_comment
call _inp
cmp al,13
jnz _word_comment ; loop until input buffer exhausted
lodsd : jmp [eax]
OPT FNdefcode("NUMBER", 0, Number)
pop ecx ; length of string
pop edi ; start address of string
call _number
push eax ; parsed number
push ecx ; number of unparsed characters (0 = no error)
lodsd : jmp [eax]
._number
xor eax,eax
xor ebx,ebx
test ecx,ecx ; trying to parse a zero-length string return 0.
jz _number_ret
mov edx,[var_Base] ; get BASE (in edx)
mov bl,[edi] ; bl = first character in string
inc edi
push eax ; push 0 on stack
cmp bl,ASC"-" ; negative number?
jnz _number_pos
pop eax
push ebx ; push <> 0 on stack, indicating negative
dec ecx
jnz _number_next
pop ebx ; error, string is only '-'.
mov ecx,1
ret
._number_next
imul eax,edx ; eax *= BASE
mov bl,[edi] ; bl = next character in string
inc edi
._number_pos
sub bl,ASC"0" ; bl < '0'?
jb _number_end
cmp bl,10 ; bl <= '9'?
jb _number_dig
sub bl,17 ; bl < 'A'? (17 is 'A'-'0')
jb _number_end
add bl,10
._number_dig
cmp bl,dl ; bl >= BASE?
jge _number_end
add eax,ebx
loop _number_next
._number_end
pop ebx ; sign flag
test ebx,ebx
jz _number_ret
neg eax
._number_ret
ret
OPT FNdefcode("SOURCE", 0, Source) ; Added by RTR
push dword [srcbuff]
push dword [srcchrs]
lodsd : jmp [eax]
OPT FNdefcode(">IN", 0, Inptr) ; Added by RTR
push srccurr
lodsd : jmp [eax]
OPT FNdefcode("SETSRC", 0, Setsrc) ; Added by RTR
pop ecx ; string length
pop edx ; string address
mov eax,[srcbuff]
mov [savbuff],eax
mov eax,[srcchrs]
mov [savchrs],eax
mov eax,[srccurr]
mov [savcurr],eax
mov [srcbuff],edx
mov [srcchrs],ecx
mov dword [srccurr],0
lodsd : jmp [eax]
OPT FNdefcode("RESTORE", 0, Restore) ; Added by RTR
mov eax,[savcurr]
mov [srccurr],eax
mov eax,[savchrs]
mov [srcchrs],eax
mov eax,[savbuff]
mov [srcbuff],eax
lodsd : jmp [eax]
OPT FNdefcode("ACCEPT", 0, Accept) ; Added by RTR
pop eax ; max chars (ignored!!)
pop edx ; buffer address
push dword [exchan]
mov dword [exchan],0 ; temp disable EXECing
call _osline
pop dword [exchan]
push eax ; characters entered
lodsd : jmp [eax]
OPT FNdefcode("GET", 0, Get) ; Added by RTR
call "osrdch"
movzx eax,al
push eax ; push char value on stack
lodsd : jmp [eax]
OPT FNdefcode("INKEY", 0, Inkey) ; Added by RTR
pop eax
call "oskey"
movzx eax,al
jc _inkey_ok
cmc
sbb eax,eax
._inkey_ok
push eax ; push key value on stack
lodsd : jmp [eax]
OPT FNdefcode("BGET", 0, Bget) ; Added by RTR
pop ebx ; Channel number
call "osbget"
movzx eax,al
jc _bget_ok
cmc
sbb eax,eax
._bget_ok
push eax ; push value on stack
lodsd : jmp [eax]
OPT FNdefcode("BPUT", 0, Bput) ; Added by RTR
pop ebx ; Channel number
pop eax ; Data
call "osbput"
lodsd : jmp [eax]
;REM DICTIONARY LOOK UPS ----------------------------------------------
OPT FNdefcode("FIND", 0, Find)
pop ecx ; ecx = length
pop edi ; edi = address
call _find
push eax ; eax = address of dictionary entry (or NULL)
lodsd : jmp [eax]
._find
push esi ; Save esi so we can use it in string comparison.
mov edx,[var_Latest] ; LATEST points to name header of the latest word
._find_loop
test edx,edx ; NULL pointer? (end of the linked list)
je _find_bad ; not found
movzx eax,byte [edx+4] ; al = flags+length field
and al,F_HIDDEN OR F_LENMASK ; al = name length
cmp al,cl ; Length is the same?
jne _find_next
push ecx ; Save the length
push edi ; Save the address
lea esi,[edx+5] ; Dictionary string we are checking against.
repe cmpsb ; Compare the strings.
pop edi
pop ecx
jne _find_next ; Not the same.
pop esi
mov eax,edx
ret ; Found
._find_next
mov edx,[edx] ; Move to the previous word
jmps _find_loop
._find_bad
pop esi
xor eax,eax ; Return zero to indicate not found.
ret
OPT FNdefcode(">CFA", 0, Tcfa)
pop edi
call _tcfa
push edi
lodsd : jmp [eax]
._tcfa
lea edi,[edi+4] ; Skip link pointer