-
Notifications
You must be signed in to change notification settings - Fork 0
/
FORTH.ASM
368 lines (254 loc) · 4.63 KB
/
FORTH.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
;; FORTH.ASM -- Forth system for Microsoft (R) DOS
BITS 16
;; DOS loads .COM executables here
ORG 100h
%INCLUDE "DOS.ASM"
;;; MACROS ;;;
;; Step indirect threaded code to next word. Call this in a raw
;; word to effectively return. In an interpreted word SI first
;; needs to be reset to the calling value.
%MACRO NEXT 0
LODSW
MOV BX, AX ; [ax] is invalid in 16 bits
JMP [BX]
%ENDMACRO
;; Push register operand to return stack
%MACRO RSPUSH 1
SUB BP, WORDSZ
MOV [BP], %1
%ENDMACRO
;; Pop from return stack to register operand
%MACRO RSPOP 1
MOV %1, [BP]
ADD BP, WORDSZ
%ENDMACRO
;; Used for the compile-time dictionary linked list. At runtime
;; LATEST is used instead
%DEFINE LINK 0
IMMEDIATE_BIT EQU 1 << 6
HIDDEN_BIT EQU 1 << 5
LENGTH_MASK EQU 0b11111
;; Define a threaded word. The arguments should be the symbol for
;; the word, followed by the string version. e.g.:
;;
;; DEFWORD DUP, 'DUP', IMMEDIATE_BIT
%MACRO DEFWORD 3
ALIGN 2
WORD_%1:
DW LINK
%DEFINE LINK WORD_%1
DB WORDLEN_%1 | %3 ; Length | Flags
NAME_%1:
DB %2,
WORDLEN_%1 EQU $ - NAME_%1
ALIGN 2
%1:
%ENDMACRO
%MACRO DEFWORD_THREADED 2
DEFWORD %1, %2, 0
DW DOCOL
%ENDMACRO
%MACRO DEFWORD_THREADED_IMMED 2
DEFWORD %1, %2, IMMEDIATE_BIT
DW DOCOL
%ENDMACRO
;; Same as DEFWORD_THREADED but this time with raw code
%MACRO DEFWORD_RAW 2
DEFWORD %1, %2, 0
DW INTRAW ; Raw interpreter codeword
;; Callable from assembly
CODE_%1:
%ENDMACRO
%MACRO DEFWORD_RAW_IMMEDIATE 2
DEFWORD %1, %2, IMMEDIATE_BIT
DW INTRAW
;; Callable from assembly
CODE_%1:
%ENDMACRO
;; DEFVAR name, 'name'
;; dw 0
%MACRO DEFVAR 2
DEFWORD_RAW %1, %2
PUSH VAR_%1
NEXT
VAR_%1:
%ENDMACRO
%MACRO DEFCONST 2
DEFWORD_RAW %1, %2
PUSH WORD [CONST_%1]
NEXT
CONST_%1:
%ENDMACRO
%MACRO INCLUDE_STRING 1
DW LITSTRING
DW %%STRINGLEN
%%BEFORE_STRING:
DB %1
%%STRINGLEN EQU $ - %%BEFORE_STRING
ALIGN WORDSZ
%ENDMACRO
%MACRO RELATIVE_ADDRESS 1
DW (%1 - $ - 2)
%ENDMACRO
%MACRO DOS_STRING 1
DB %1, 0Dh, 0Ah, '$'
%ENDMACRO
;; Move the address of the PAD into %1
;; Pad starts at 256 bytes above LATEST
%MACRO GET_PAD 1
MOV %1, [VAR_LATEST]
ADD %1, 256
%ENDMACRO
;;; PROGRAM CODE ;;;
_START:
;; Progran begins
MOV BP, SP
SUB BP, 1024
MOV WORD [CONST_SP_INITIAL], SP
MOV SI, INDIRECT_START
NEXT
ALIGN 2
;; DO COLon definition -- Codeword for indirect threaded code
;; ax: indirect execution address
DOCOL:
RSPUSH SI
ADD AX, WORDSZ ; Point to the first word address
MOV SI, AX ; Enter the function body (set si)
NEXT
;; Interpret raw code (plain machine code)
INTRAW:
ADD AX, WORDSZ
JMP AX
INDIRECT_START:
DW SETUP
DW QUIT
SETUP:
DW INTRAW
MOV DX, MSG
WRITESOUT
NEXT
LITERAL:
DW INTRAW
LODSW ; Load the next word
PUSH AX
NEXT
EXIT:
DW INTRAW
RSPOP SI
NEXT
DEFWORD_RAW BYE, 'BYE'
MOV DX, .BYE_MSG
WRITESOUT
QUIT_PROC
.BYE_MSG DOS_STRING 'Bye'
DEFWORD_RAW LIT, 'LIT'
LODSW ; Read next word from input to AX
PUSH AX
NEXT
DEFWORD_RAW DROP, 'DROP'
ADD SP, WORDSZ
NEXT
DEFWORD_RAW SWAP, 'SWAP'
POP AX
POP BX
PUSH AX
PUSH BX
NEXT
;; a b -- a b a
DEFWORD_THREADED OVER, 'OVER'
DW TO_RET, DUP, FROM_RET, SWAP, EXIT
DEFWORD_RAW DUP, 'DUP'
;; This is stupid, [SP] is invalid
POP AX
PUSH AX
PUSH AX
NEXT
DEFWORD_RAW TO_RET, '>R'
POP AX
RSPUSH AX
NEXT
DEFWORD_RAW FROM_RET, 'R>'
RSPOP AX
PUSH AX
NEXT
;; ( a b c -- b c a )
DEFWORD_THREADED ROT, 'ROT'
DW TO_RET, SWAP, FROM_RET, SWAP, EXIT
DEFWORD_RAW PLUS, '+'
POP AX
POP BX
ADD AX, BX
PUSH AX
NEXT
DEFWORD_RAW MINUS, '-' ; ( DX AX -- DX-AX )
POP AX
POP DX
SUB DX, AX
PUSH DX
NEXT
DEFWORD_RAW SLASHMOD, '/MOD'
POP AX
POP DX
IDIV DX
PUSH DX ; Remainder
PUSH AX ; Quotient
NEXT
DEFWORD_RAW _TIMES, '*'
POP AX
POP DX
IMUL DX
PUSH AX
NEXT
DEFWORD_RAW AND, 'AND'
POP AX
POP DX
AND AX, DX
PUSH AX
NEXT
DEFWORD_RAW XOR, 'XOR'
POP DX
POP AX
XOR AX, DX
PUSH AX
NEXT
DEFWORD_RAW NOT, 'NOT'
POP AX
NOT AX
PUSH AX
NEXT
DEFWORD_RAW ADD1, '1+'
POP AX
ADD AX, 1
PUSH AX
NEXT
DEFWORD_RAW ADD2, '2+'
POP AX
ADD AX, 2
PUSH AX
NEXT
;; This kind of sucks
DEFWORD_RAW _2DUP, '2DUP' ; ( a b -- a b a b )
POP AX
POP BX
PUSH BX
PUSH AX
PUSH BX
PUSH AX
NEXT
%INCLUDE "IOWORDS.ASM"
%INCLUDE "DICTNRY.ASM"
;;; LATE-INIT VARIABLES ;;;
DEFVAR STATE, 'STATE'
DW 0 ; Interpret; ( cfa )
DEFVAR HERE, 'HERE'
DW HERE_START
DEFCONST SP_INITIAL, 'S0'
DW 0
;; LATEST must be the last word defined in FORTH.ASM!
DEFVAR LATEST, 'LATEST'
DW LINK
;;; PROGRAM DATA ;;;
MSG DB 'DOS FORTH', 0Dh, 0Ah, '$'
;;; FREE DATA ;;;
ALIGN 4
HERE_START: