-
Notifications
You must be signed in to change notification settings - Fork 1
/
Forth-STC-Prims.inc
413 lines (365 loc) · 13.1 KB
/
Forth-STC-Prims.inc
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
; -------------------------------------------------------------------------
; -------------------------------------------------------------------------
; -------------------------------------------------------------------------
xxprim_LITERAL: ; Implementation of LITERAL
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_FETCH: ; Implementation of FETCH
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_STORE: ; Implementation of STORE
m_pop edx
m_pop eax
mov [edx], eax
ret
prim_SWAP: ; Implementation of SWAP
m_pop eax ; NB ebx is the TOS, SWAP
m_getTOS ecx
m_setTOS eax
m_push ecx
ret
xxprim_DROP: ; Implementation of DROP
m_pop eax
ret
prim_DUP: ; Implementation of DUP
m_push ebx
ret
prim_SLITERAL: ; Implementation of SLITERAL
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_JMP: ; Implementation of JMP
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_JMPZ: ; Implementation of JMPZ
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_JMPNZ: ; Implementation of JMPNZ
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_CALL: ; Implementation of CALL
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_RET: ; Implementation of RET
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_OR: ; Implementation of OR
m_pop eax
or ebx, eax
ret
prim_XOR: ; Implementation of OR
m_pop eax
xor ebx, eax
ret
xxprim_CLITERAL: ; Implementation of CLITERAL
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_CFETCH: ; Implementation of CFETCH
xor eax, eax
mov al, [ebx]
m_setTOS eax
ret
prim_CSTORE: ; Implementation of CSTORE
m_pop edx
m_pop eax
mov [edx], al
ret
prim_ADD: ; Implementation of ADD
m_pop eax
add ebx, eax
ret
prim_SUB: ; Implementation of SUB
m_pop eax
sub ebx, eax
ret
prim_MUL: ; Implementation of MUL
m_pop ecx
m_getTOS eax
mul ecx
m_setTOS eax
ret
prim_SLASHMOD: ; Implementation of /MOD
m_pop ecx
m_pop eax
cmp ecx, 0
je smDivBy0
xor edx, edx
div ecx
m_push eax
m_push edx
ret
smDivBy0:
m_push '/'
call prim_EMIT
m_push 'm'
call prim_EMIT
m_push '?'
call prim_EMIT
m_push 0
m_push 0
ret;
prim_DIV: ; Implementation of DIV
m_pop ecx
m_pop eax
cmp ecx, 0
je divBy0
xor edx, edx
div ecx
m_push eax
ret
divBy0:
m_push 'v'
call prim_EMIT
m_push '?'
call prim_EMIT
m_setTOS 0
ret;
prim_LT: ; Implementation of LT
m_pop eax
cmp ebx, eax
jl LT_T
m_setTOS 0
ret
LT_T:
m_setTOS 1
ret
prim_EQ: ; Implementation of EQ
m_pop eax
cmp ebx, eax
je EQ_T
m_setTOS 0
ret
EQ_T: m_setTOS 1
ret
prim_GT: ; Implementation of GT
m_pop eax
cmp ebx, eax
jg GT_T
m_setTOS 0
ret
GT_T: m_setTOS 1
ret
xxprim_DICTP: ; Implementation of DICTP
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_EMIT: ; Implementation of EMIT
;
; BOOL WINAPI WriteConsole(
; _In_ HANDLE hConsoleOutput,
; _In_ const VOID *lpBuffer,
; _In_ DWORD nNumberOfCharsToWrite,
; _Out_opt_ LPDWORD lpNumberOfCharsWritten,
; _Reserved_ LPVOID lpReserved
m_pop eax
mov byte ptr tmp_buf, al
push 0
push 0
push 1
push tmp_buf
push DWORD [STDOUT]
call [WriteConsoleA]
ret
prim_OVER: ; Implementation of OVER
m_pop ecx
m_getTOS edx
m_push ecx
m_push edx
ret
prim_COMPARE: ; Implementation of COMPARE
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_COMPAREI: ; Implementation of COMPAREI
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_FOPEN: ; Implementation of FOPEN
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_FREAD: ; Implementation of FREAD
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_FREADLINE: ; Implementation of FREADLINE
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_FWRITE: ; Implementation of FWRITE
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_FCLOSE: ; Implementation of FCLOSE
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_DTOR: ; Implementation of DTOR
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
xxprim_RTOD: ; Implementation of RTOD
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_LOGLEVEL: ; Implementation of LOGLEVEL
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_AND: ; Implementation of AND
m_pop eax
and ebx, eax
ret
prim_PICK: ; Implementation of PICK
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_DEPTH: ; Implementation of DEPTH
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
GETCH_num_chars dd 0
prim_GETCH: ; Implementation of GETCH
;BOOL WINAPI ReadConsole(
; _In_ HANDLE hConsoleInput,
; _Out_ LPVOID lpBuffer,
; _In_ DWORD nNumberOfCharsToRead,
; _Out_ LPDWORD lpNumberOfCharsRead,
; _In_opt_ LPVOID pInputControl
;
push 0
push GETCH_num_chars
push 1
push tmp_buf
push DWORD [STDIN]
call [ReadConsoleA]
xor eax, eax
mov al, [tmp_buf]
m_push eax
ret
prim_USINIT: ; Implementation of USINIT
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_USPUSH: ; Implementation of USPUSH
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_USPOP: ; Implementation of USPOP
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_GETTICK:
; TODO
ret
prim_BREAK: ; Implementation of BREAK
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_RESET: ; Implementation of RESET
; TODO
m_push [HERE_ADDR]
m_pop [ecx]
m_getTOS edx
m_setTOS eax
ret
prim_BYE: ; Implementation of BYE
m_push ' '
call prim_EMIT
m_push 'B'
call prim_EMIT
m_push 'Y'
call prim_EMIT
m_push 'E'
call prim_EMIT
push 0
call [ExitProcess]
ret
; -------------------------------------------------------------------------
; -------------------------------------------------------------------------
; -------------------------------------------------------------------------