-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsector.asm
246 lines (215 loc) · 3.07 KB
/
sector.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
bits 16
%ifdef COMPATIBLE_8088
cpu 8086
%else
cpu 386
%endif
db 0eah
dw main ;jmp 0x0050:main but reusing the segment as sentinel
org 0x7700
RSTACK_BASE equ 0x7700
STACK_BASE equ 2
TIB equ 0x0000
TIBP1 equ TIB+1
STATE equ 0x1000 ; must be 256-bytes aligned
CIN equ 0x1002
LATEST equ 0x1004
HERE equ 0x1006
FLAG_IMM equ 1<<7
LEN_MASK equ (1<<5)-1 ; have some extra flags, why not
%define link 0x50
%macro defword 2-3 0
word_%2:
dw link
%define link word_%2
%strlen %%len %1
db %3+%%len
db %1
%2:
%endmacro
defword "@",FETCH
pop di
push word [di]
jmp NEXT
defword "!",STORE
pop di
pop word [di]
jmp NEXT
defword "sp@",SPFETCH
push sp
jmp NEXT
defword "rp@",RPFETCH
push dx
jmp NEXT
defword "0#",ZEROEQ
pop ax
neg ax
sbb ax,ax
jmp pushax
defword "+",PLUS
pop di
pop ax
add ax,di
jmp pushax
defword "nand",NAND
pop di
pop ax
and ax,di
not ax
pushax:
push ax
jmp NEXT
defword "exit",EXIT
xchg sp,dx
pop si
jmp DOCOL.swapsp
defword "s@",STATEVAR
push bx
NEXT:
lodsw
jmp ax
defword ":",COLON
call tok
push si
mov si,di
mov ax,[bx+HERE-STATE]
mov di,ax
xchg [bx+LATEST-STATE],ax
stosw
mov al,cl
stosb
rep movsb
pop si
mov byte [bx],cl
mov ax,0x26ff
stosw
mov ax,DOCOL.addr
jmp sethere
DOCOL:
xchg sp,dx
push si
xchg ax,si
lodsw
lodsw
.swapsp:
xchg sp,dx
jmp NEXT
.addr:
dw DOCOL
defword "key",KEY
xchg cx,ax ; ah=0
int 0x16
jmp pushax
defword "emit",EMIT
pop ax
call putchar
jmp NEXT
defword ";",SEMICOLON,FLAG_IMM
mov byte [bx],cl
mov ax, EXIT
compile:
mov di,[bx+HERE-STATE]
sethere:
stosw
mov [bx+HERE-STATE],di
jmp NEXT
main:
push cs
push cs
push cs
pop ds
pop es
pop ss
mov bx,STATE
mov word [bx+LATEST-STATE],word_SEMICOLON
mov word [bx+HERE-STATE],here
error:
mov sp,STACK_BASE
mov al,13
call putchar
mov dx,RSTACK_BASE
xor si,si ;mov si,TIB
push si ;mov [TIB],si
inc si
mov [bx],si
find:
call tok
lea bp,[bx+LATEST-STATE]
.1: mov si,bp
lodsw
xchg bp,ax
test ah,ah
jz error
lodsb
push ax
and al,LEN_MASK
cmp al,cl
pop ax
jne .1
push cx
push di
repe cmpsb
pop di
pop cx
jne .1
and al,FLAG_IMM
or al,[bx]
xchg si,ax
mov si,_find
jz compile
jmp ax
storebyte:
stosb
db 0x3d ;mask xor di,di
getline:
xor di,di ;mov di,TIB
call getchar
and word [bx+CIN-STATE],di
cmp al,10
jne storebyte
mov ax, 0x0020
stosw
tok:
mov di,[bx+CIN-STATE]
mov al,32
.1: cmp byte [di],bl
je getline
scasb
je .1
xor cx,cx
.2: inc cx
scasb
jne .2
dec di
mov [bx+CIN-STATE],di
sub di,cx
ret
_find: dw find
%ifdef BACKSPACE
testdi: test di,di ; cmp di,TIB
je getchar
dec di
%endif
getchar:
xor ax,ax
int 0x16
putchar:
mov ah,0x0e
db 0x3d ;mask mov al,10
.1: mov al,10
int 0x10
cmp al,13
je .1
; The below lines are a "QOL" improvement: the delete key.
; Since sectorLISP does not handle the delete key, I have commented them out for a fair comparison of size.
; Even when re-added, this FORTH is still smaller, however.
%ifdef BACKSPACE
cmp al,8
je testdi
%endif
ret
%ifndef CHECKSIZE
times 510-($-$$) db 0
db 0x55, 0xaa
%endif
here: