-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
1963 lines (1683 loc) · 30.8 KB
/
kernel.asm
File metadata and controls
1963 lines (1683 loc) · 30.8 KB
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 16
org 0x0500
; dx = A register
; bp = PSP (parameter stack pointer)
; sp = RSP (return stack pointer) ; FORTH MACROS
; USEFUL MACROS
%macro spush 1 ; src
sub bp, 2
mov word [bp], %1
%endmacro
%macro spop 1 ; dst
mov word %1, [bp]
add bp, 2
%endmacro
%macro litb 1
mov byte [di], %1
inc di
%endmacro
%macro litw 1
mov word [di], %1
add di, 2
%endmacro
; WORD STRUCTURE
%macro wordinit 2
db %1 ; name (n bytes)
dw 0 ; link (2 bytes)
db %2 ; len (1 byte)
%push dict
%$link:
%endmacro
%macro wordlink 2
db %1 ; name (n bytes)
dw %$link ; link (2 bytes)
db %2 ; len (1 byte)
%pop
%push dict
%$link:
%endmacro ; load (n bytes)
%define FLAG_IMMEDIATE 0x80 ; immediate flag
%define MASK_LENGTH 0x7f ; namelen mask
%define BOOTDRIVE 7c3eh
%macro const 3 ; name, len, lit
wordlink %1, %2
spush %3
ret
%endmacro
start: mov bp, 0xff00 ; initialize SP
mov sp, 0x0000 ; initialize RS
jmp interpret
; SYSTEM VARIABLES
state: db 0 ; interpret or compile mode
latest: dw noop ; last word of the dictionary
here: dw end ; next available space in dictionary
in: dw buffer ; current char read in buffer
buffer: times 65 db 0 ; input buffer
curchar: dw 0 ; current word addr
curlen: db 0 ; current word len
blk: dw 0x7e00 ; pointer to current line
line: dw 16*3 ; number of lines to read
; THE DICTIONARY
; key ( -- c )
; fetch char c from direct input
wordinit 'key', 3
key: mov ah, 0x00
int 0x16
spush ax ; ah: scan code, al: ascii char
ret
; emit ( c -- )
; spit char c to output stream
wordlink 'emit', 4
emit: spop ax
mov ah, 0x0e
int 0x10
ret
; abort ( -- )
; reset PS and jump to quit
wordlink 'abort', 5
abort: mov bp, 0xff00
jmp quit
; quit ( -- )
; reset RS and return to interpreter prompt
wordlink 'quit', 4
quit: mov sp, 0x0000
mov byte [state], 0
mov word [line], 0
mov word [in], buffer+64
jmp interpret
; interpret ( -- )
; main interpret loop
wordlink 'interpret', 9
interpret: ; read next word
call toword ; ( -- sa sl )
; is a number?
call parse ; ( sa sl -- n? f )
spop ax ; ( n? f -- n? )
test ax, ax
jnz .num
; is a word?
call curword ; ( -- sa sl )
call find ; ( sa sl -- w? f )
spop ax ; ( w? f -- w? )
test ax, ax
jnz .word
; not a word
jmp notfound
.num: mov al, [state] ; ( n -- n )
test al, al
jz interpret
call litn ; ( n -- )
jmp interpret
.word: mov al, [state]
test al, al
jz .run
; immediate word?
mov bx, [bp]
mov al, [bx-1]
and al, FLAG_IMMEDIATE
test al, al
jnz .run
call compile_comma ; ( w -- )
jmp interpret ; ( -- )
.run: call execute ; ( w -- )
jmp interpret ; ( -- )
notfound: call nl_out ; ( -- )
call curword ; ( -- sa sl )
call type ; ( sa sl -- )
spush .err ; ( -- sa )
spush 15 ; ( -- sa sl )
call type ; ( sa sl -- )
jmp abort
.err: db ' word not found'
notnumber: call nl_out
call curword
call type
spush .err ; ( -- sa )
spush 13 ; ( -- sa sl )
call type ; ( sa sl -- )
jmp abort
.err: db ' not a number'
underflow: spush .err ; ( -- sa )
spush 21 ; ( -- sa sl )
call type ; ( sa sl -- )
jmp abort
.err: db 0x0d, 0x0a, 'stack out of bounds'
; type ( sa sl -- )
; emit all chars of string
wordlink 'type', 4
type: spop cx
spop si
mov ah, 0x0e
.next: lodsb
int 0x10
loop .next
ret
; word ( -- sa sl )
; read one word from buffered input
; update curchar and curlen
wordlink 'word', 4
toword: call tochar
spop ax
cmp ax, 0x20
jbe toword
mov ax, [in]
dec ax
spush ax
mov [curchar], ax
xor cx, cx
.next: inc cx
call tochar
spop ax
cmp ax, 0x20
ja .next
spush cx
mov [curlen], cl
ret
; curword ( -- sa sl )
; yield the last read word
wordlink 'curword', 7
curword: mov ax, [curchar]
spush ax
mov al, [curlen]
xor ah, ah
spush ax
ret
; in< ( -- c )
; read one char from buffered input
; if end of line, read new line
wordlink 'in<', 3
tochar: mov bx, [in]
cmp bx, buffer+64
jbe .skip
call ln_in
mov bx, buffer
.skip: mov al, [bx]
xor ah, ah
spush ax
inc bx
mov [in], bx
ret
; ln< ( -- )
; routine that feeds lines to the interpreter
wordlink 'ln<', 3
ln_in: call scnt
spop ax
test ax, ax
js underflow
mov ax, [line]
test ax, ax
jz rdln
dec ax
mov [line], ax
jmp rdbln
; read block line
wordlink 'rdbln', 5
rdbln: mov si, [blk]
mov di, buffer
mov cx, 64
rep movsb
litb 0
mov word [blk], si
mov word [in], buffer
ret
; rdln ( -- )
; feed a line to the buffered input
wordlink 'rdln', 4
rdln: spush .ok
spush 5
call type
mov di, buffer
.next cmp di, buffer+64 ; buffer full?
je .cr ; submit
mov ah, 0x00 ; call key
int 0x16
cmp al, 0x0d ; submit?
je .cr
cmp al, 0x08 ; backspace?
je .bs
cmp al, 0x20 ; control character?
jb .next ; do nothing
stosb ; store string byte
mov ah, 0x0e ; emit char
int 0x10
jmp .next
.bs: cmp di, buffer ; empty buffer?
je .next ; do nothing
dec di ; clear previous char
mov byte [di], 0
call bs_out
jmp .next
.cr: call spc_out ; emit space after enter
xor al, al ; flush rest of line
.null: stosb
cmp di, buffer+64
jbe .null
mov word [in], buffer
ret
.ok db ' ok', 0x0d, 0x0a ; CR, LF
; parse ( sa sl -- n? f )
; convert string as a number
wordlink 'parse', 5
parse: spop cx
spop si
xor ax, ax ; current char
xor bx, bx ; result
lodsb
dec cx
cmp al, "'"
je .char
cmp al, '$'
je .hex
cmp al, '-'
je .neg
inc cx
jmp .skip
; character literal
.char: cmp cx, 2
jne .nan
lodsb
mov bl, al
lodsb
cmp al, "'"
jne .nan
jmp .num
; hexadecimal
.hex: lodsb
shl bx, 4 ; multiply by 16
cmp al, '0'
jb .nan
cmp al, '9'
jbe .digit
cmp al, 'a'
jb .nan
cmp al, 'f'
jbe .alpha
jmp .nan
.digit: sub al, '0'
jmp .add
.alpha: sub al, 'a'-10
.add: add bx, ax
loop .hex
jmp .num
; negative decimal
.neg: lodsb
imul bx, 10
sub al, '0'
jb .nan
cmp al, 10
jae .nan
sub bx, ax
loop .neg
jmp .num
; unsigned decimal
.pos: lodsb
imul bx, 10
.skip: sub al, '0'
jb .nan
cmp al, 10
jae .nan
add bx, ax
loop .pos
.num: spush bx
spush -1
ret
.nan: spush 0
ret
; lit ( -- n ) runtime
wordlink 'lit', 3
lit: pop si
mov word di, [si]
add si, 2
spush di
push si
ret
; litn ( n -- )
; compile n as a literal
wordlink 'litn', 4
litn: mov di, [here]
mov byte [di], 0xe8
inc di
mov si, lit ; relative addressing
sub si, di
sub si, 2
mov word [di], si
add di, 2
spop si ; store n
mov word [di], si
add di, 2
mov [here], di
ret
; wordlink 'litn', 4
; litn: mov di, [here]
; litb 0x83 ; sub bp, 2
; litb 0xed
; litb 0x02
; litb 0xc7 ; mov [bp], imm16
; litb 0x46
; litb 0x00
; mov word si, [bp]
; mov [di], si
; add di, 2
; add bp, 2
; mov [here], di
; ret
; find ( sa sl -- w? f )
; search for counted string in dictionary
wordlink 'find', 4
find: spop ax ; len
spop dx ; addr
mov bx, [latest] ; curr
.loop: mov cl, [bx-1] ; same length?
and cl, 0x7f ; ignore immediate flag
cmp al, cl
jne .skip
mov si, dx
mov di, bx
sub di, 3
sub di, ax ; beginning of name
mov cx, ax
repz cmpsb
jz .found
.skip: mov bx, [bx-3] ; next entry
cmp bx, 0
jnz .loop
spush 0
ret
.found: spush bx
spush -1
ret
; execute ( w -- )
; jump to addr w
wordlink 'execute', 7
execute: spop ax
jmp ax
; compile, ( w -- )
; append a call to wordref w to here
wordlink 'compile,', 8
compile_comma: mov di, [here]
litb 0xe8 ; call opcode
spop ax ; wordref
sub ax, di
sub ax, 2
stosw
mov [here], di
ret
; header ( sa sl -- )
; append a new header with name s
wordlink 'header', 6
header: spop bx
mov cx, bx
spop si
mov di, [here]
rep movsb
mov ax, [latest]
stosw
mov al, bl
stosb
mov [latest], di
mov [here], di
ret
; create x ( -- )
; append a new header with name x
wordlink 'create', 6
create: call toword ; ( -- sa sl )
call header ; ( sa sl -- )
ret
; immediate ( -- )
; make latest word an immediate word
wordlink 'immediate', 9
immediate: mov bx, [latest]
dec bx
mov al, [bx]
xor al, FLAG_IMMEDIATE
mov [bx], al
ret
; [ ( -- ) IMMEDIATE
; to immediate mode
wordlink '[', 1 | FLAG_IMMEDIATE
to_immediate: mov byte [state], 0
ret
; ] ( -- )
; to compile mode
wordlink ']', 1
to_compile: mov byte [state], -1
ret
; exit ( -- )
; compile exit from a word
wordlink 'exit', 4 | FLAG_IMMEDIATE
exit: mov di, [here]
litb 0xc3 ; ret
mov [here], di
ret
; : ... ( -- )
; create a new word definition with name ...
wordlink ':', 1
colon: call create
call to_compile
ret
; ; ( -- )
; end current word definition
wordlink ';', 1 | FLAG_IMMEDIATE
semicolon: call exit
call to_immediate
ret
; spc> ( -- )
; emit space character
wordlink 'spc>', 4
spc_out: spush 0x20 ; SP
call emit
ret
; nl> ( -- )
; emit newline
wordlink 'nl>', 3
nl_out: spush .nl
spush 2
call type
ret
.nl: db 0x0d, 0x0a ; CR, LF
; bs> ( -- )
; delete prev char in TTY mode
wordlink 'bs>', 3
bs_out: spush .bs
spush 3
call type
ret
.bs: db 0x08, 0x20, 0x08 ; BS, SPC, BS
; FLOW
; ( -- )
; ignore input until ')' is read
wordlink '(', 1 | FLAG_IMMEDIATE
paren: call tochar
spop ax
cmp ax, ')'
jne paren
ret
; ( -- )
; ignore input until end of line
wordlink '\', 1 | FLAG_IMMEDIATE
backslash: mov word [in], buffer+64
ret
; abort" ..." ( -- )
; compiles a ." followed by an abort"
wordlink 'abort"', 6 | FLAG_IMMEDIATE
abort_quote: call dot_quote
spush abort
call compile_comma
ret
wordlink 'if', 2 | FLAG_IMMEDIATE
if: spush .if
call compile_comma
mov di, [here]
litb 0xe9 ; jmp rel16
spush di
add di, 2
mov [here], di
ret
.if: spop ax
test ax, ax
jz .jump
pop ax
add ax, 3
jmp ax
.jump ret
; compile-time ( a1 -- a2 )
; run-time ( -- )
wordlink 'else', 4 | FLAG_IMMEDIATE
else: mov di, [here] ; compile
litb 0xe9 ; jmp rel16
spush di
add di, 2 ; leave 2 byte space
mov [here], di
call swap
jmp then ; compile rel16 to 'if'
; compile-time ( a -- )
; run-time ( -- )
wordlink 'then', 4 | FLAG_IMMEDIATE
then: spop bx
mov ax, [here]
sub ax, bx
sub ax, 2
mov [bx], ax
ret
; compile-time ( -- a )
; run-time ( -- )
wordlink 'begin', 5 | FLAG_IMMEDIATE
begin: mov ax, [here]
spush ax
ret
; compile-time ( a -- )
; run-time ( -- )
wordlink 'again', 5 | FLAG_IMMEDIATE
again: mov di, [here]
litb 0xe9 ; jmp xxx
spop ax
sub ax, di
sub ax, 2
stosw
mov [here], di
ret
; compile-time ( a -- )
; run-time ( f -- )
wordlink 'until', 5 | FLAG_IMMEDIATE
until: spush .until
call compile_comma
jmp again
.until: spop ax
test ax, ax
jz .loop
pop ax ; skip jmp rel16 (3 bytes)
add ax, 3
jmp ax
.loop ret
; compile-time ( a -- )
; run-time ( R: n -- )
wordlink 'next', 4 | FLAG_IMMEDIATE
next: spush .next
call compile_comma
jmp again
.next: pop bx
pop ax
dec ax
push ax
test ax, ax
jnz .loop
pop ax
add bx, 3
.loop: jmp bx
; leave ( -- )
; in a begin..next loop, exit at the next jump
wordlink 'leave', 5
leave: pop bx
pop ax
push 1
push bx
ret
; [if] ( f -- )
; works outside of definitions
; all [if] leads to the first [then]
wordlink '[if]', 4
meta_if: spop ax
test ax, ax
jz .jump
ret
.jump: call toword
spop ax
cmp ax, 6
je .next
add bp, 2 ; drop
jmp .jump
.next: spush .then
spush ax
call scmp
spop ax
test ax, ax
jz .jump
ret
.then: db '[then]'
; [then] ( -- )
; reference for [if], on its own does nothing
wordlink '[then]', 6
meta_then: ret
; SYSTEM VARIABLES
; ( -- a ) currently selected line
const 'blk', 3, blk
; ( -- a ) current line count
const 'line', 4, line
; ( -- a ) last word of the directionary
const 'latest', 6, latest
; ( -- a ) next available space in dictionary
const 'here', 4, here
; ( -- a ) beginning of the input buffer
const 'in(', 3, buffer
; ( -- a ) end of the input buffer
const 'in)', 3, buffer+64
; ( -- a ) current position in the input buffer
const 'in>', 3, in
; ENTRY MANAGEMENT
; '? ... ( -- f )
; find ... in dictionary, return true if found
wordlink "'?", 2
tick?: call toword ; ( -- sa sl )
call find ; ( sa sl -- w? f )
mov ax, [bp]
test ax, ax
jz .done
call nip
.done: ret
; ' ... ( -- w )
; find addr of word ..., abort if fail
wordlink "'", 1
tickw: call toword ; ( -- sa sl )
call find ; ( sa sl -- w? f )
spop ax
test ax, ax
jz notfound
ret
; ['] ... runtime ( -- w )
; similar to "'", but store addr as a literal
wordlink "[']", 3 | FLAG_IMMEDIATE
tickimm: call toword ; ( -- sa sl )
call find ; ( sa sl -- w? f )
spop ax
test ax, ax
jz notfound
call litn ; ( w -- )
ret
; forget ... ( -- )
; rewind dict up to ...'s prev entry
wordlink "forget", 6
forget: call toword
call find
spop ax
test ax, ax
jz notfound
spop bx
mov ax, [bx-3] ; next entry
mov word [latest], ax
mov al, [bx-1]
and al, MASK_LENGTH
xor ah, ah
sub bx, ax
sub bx, 3
mov word [here], bx
ret
; DEFINING WORDS
; value ... ( n -- )
; create cell ... that returns its value
wordlink 'value', 5
value: call create
call litn
call exit
ret
; to ... ( n -- )
; reassign value of ... to n
wordlink 'to', 2 | FLAG_IMMEDIATE
to_value: call toword
call find
spop ax
test ax, ax
jz notfound
mov al, [state]
test al, al
jz .imm
call litn
spush .imm
call compile_comma
ret
.imm: spop bx ; wordref
spop ax ; number
add bx, 3
mov [bx], ax
ret
; alias x y ( -- )
; define an alias y to x
wordlink 'alias', 5
alias: call toword
call find
spop ax
test ax, ax
jz notfound
call create
mov di, [here]
litb 0xe9 ; jmp rel16
spop ax ; wordref
sub ax, di
sub ax, 2
stosw
mov [here], di
ret
; [compile] ... ( -- )
; compile word ... and write it to here
wordlink '[compile]', 9 | FLAG_IMMEDIATE
compile_imm: call tickw
call compile_comma
ret
; ASCII CONSTANTS
const 'BS', 2, 0x08 ; backspace
const 'CR', 2, 0x0d ; carriage return
const 'LF', 2, 0x0a ; line feed
const 'SPC', 3, 0x20 ; space character
; PARAMETER STACK
; ( a -- )
wordlink 'drop', 4
drop: add bp, 2
ret
; ( a -- a a )
wordlink 'dup', 3
dup: mov ax, [bp]
spush ax
ret
; ( a -- a a? )
; dup if a is nonzero
wordlink '?dup', 4
?dup: mov ax, [bp]
test ax, ax
jz .zero
spush ax
.zero: ret
; ( a b -- b )
wordlink 'nip', 3
nip: spop ax
mov [bp], ax
ret
; ( a b -- a b a )
wordlink 'over', 4
over: mov bx, bp
add bx, 2
mov bx, [bx]
spush bx
ret
; ( a b c -- b c a )
wordlink 'rot', 3
rot: spop ax
spop bx
mov cx, [bp]
mov [bp], bx
spush ax
spush cx
ret
; ( a b c -- c a b )
wordlink 'nrot', 4
nrot: spop ax
spop bx
mov cx, [bp]
mov [bp], ax
spush cx
spush bx
ret
; ( a b -- b a )
wordlink 'swap', 4
swap: spop ax
mov bx, [bp]
mov [bp], ax
spush bx
ret
; ( a b -- b a b )
wordlink 'tuck', 4
tuck: spop ax
mov bx, [bp]
mov [bp], ax
spush bx
spush ax
ret
; ( a a -- )
wordlink '2drop', 5
twodrop: add bp, 4
ret
; ( a b -- a b a b )
wordlink '2dup', 4
twodup: spop ax
mov bx, [bp]
sub bp, 4
mov [bp], bx
spush ax
ret
; RETURN STACK
; ( n -- R:n )
wordlink '>r', 2
to_r: spop ax
pop bx
push ax
push bx
ret
; ( R:n -- n )
wordlink 'r>', 2
r_from: pop ax
pop bx
push ax
spush bx
ret
; ( R:n -- n R:n )
wordlink 'r@', 2
r_fetch: pop ax
pop bx
spush bx
push bx
push ax
ret
; ( R:n -- )
wordlink 'rdrop', 5
rdrop: pop ax
pop bx
push ax
ret
; STACK META
; .s ( -- )
; prints contents of the stack
wordlink '.s', 2
dot_s: call scnt
call shiftr
spush '<'
call emit
call dup
call dot
spush '>'