-
Notifications
You must be signed in to change notification settings - Fork 1
/
forth1.asm
561 lines (506 loc) · 16.1 KB
/
forth1.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
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
; Test to see if CS and DS are equal
format pe console
include 'win32ax.inc' ; you can simply switch between win32ax, win32wx, win64ax and win64wx here
section '.text' code readable writeable executable
STDIN dd 0
STDOUT dd 0
; -----------------------------------------------------------------------------
macro m_PUSH val
{
add ebp, 4
mov [ebp], ebx
mov ebx, val
}
macro m_POP tgt
{
mov tgt, ebx
m_DROP
}
macro m_DROP
{
mov ebx, [ebp]
sub ebp, 4
}
; -----------------------------------------------------------------------------
macro m_RPUSH val
{
push val
}
macro m_RPOP tgt
{
pop tgt
}
; -----------------------------------------------------------------------------
cs_PUSH:
cmp ebp, DSTACKEND
jl cspsOK
invoke MessageBox,HWND_DESKTOP,"Stack overflow", "xxx",MB_OK
cspsOK: ret
; -----------------------------------------------------------------------------
cs_POP:
cmp ebp, DSTACK
jg csppOK
invoke MessageBox,HWND_DESKTOP,"Stack underflow", "xxx",MB_OK
csppOK: ret
; -----------------------------------------------------------------------------
startup:
push STD_INPUT_HANDLE
call [GetStdHandle]
mov [STDIN], eax
push STD_OUTPUT_HANDLE
call [GetStdHandle]
mov [STDOUT], eax
mov ebp, DSTACK
mov ebx, 0
m_PUSH 'c'
m_PUSH 'b'
m_PUSH 'a'
call fEMIT_XT
call fEMIT_XT
call fEMIT_XT
ret
; -----------------------------------------------------------------------------
mainLoop:
call fWORDS_XT
ret
; -----------------------------------------------------------------------------
buf0 db 16 dup 0
HERE_ADDR dd HERE_INIT
LAST_ADDR dd DICT_START
BASE_ADDR dd 10
STATE_ADDR dd 0
INPUTFP_ADDR dd 0
HERE_INIT dd 24
THE_MEMORY db (8*1024) dup 0
; -----------------------------------------------------------------------------
do_exec:
push ecx
add ecx, 4
mov ecx, [ecx]
call ecx
pop ecx
ret
; -----------------------------------------------------------------------------
macro dict_ENTRY dent, nxt, fl, ct, nm {
dent dd nxt
dd dent#_XT
db fl
db ct, nm, 0
dent#_XT:
}
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
DICT_START:
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
dict_ENTRY fDUP, fOVER, 1, 3, "DUP"
m_PUSH ebx
ret
; -----------------------------------------------------------------------------
dict_ENTRY fOVER, fDROP, 1, 4, "OVER"
mov eax, [ebp]
m_PUSH eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fDROP, fSWAP, 1, 4, "DROP"
m_DROP
ret
; -----------------------------------------------------------------------------
dict_ENTRY fSWAP, fSLMOD, 1, 4, "SWAP"
mov eax, [ebp]
mov [ebp], ebx
mov ebx, eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fSLMOD, fthMUL, 0, 4, "/MOD"
m_POP ecx
m_POP eax
cmp ecx, 0
je smDivBy0
xor edx, edx
div ecx
m_PUSH eax ; quotient
m_PUSH edx ; remainder
ret
smDivBy0:
m_PUSH 0
m_PUSH 0
ret
; -----------------------------------------------------------------------------
dict_ENTRY fthMUL, fthDIV, 0, 1, "*"
m_POP ecx
mov eax, ebx
mul ecx
mov ebx, eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fthDIV, fthADD, 0, 1, "/"
call fSLMOD_XT
m_DROP
ret
; -----------------------------------------------------------------------------
dict_ENTRY fthADD, fthSUB, 0, 1, "+"
m_POP eax
add ebx, eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fthSUB, fGT, 0, 1, "-"
m_POP eax
sub ebx, eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fGT, fLT, 0, 1, ">"
m_POP eax
cmp ebx, eax
jg cmp_TT
jmp cmp_FF
; -----------------------------------------------------------------------------
dict_ENTRY fLT, fEQ, 0, 1, "<"
m_POP eax
cmp ebx, eax
jl cmp_TT
jmp cmp_FF
; -----------------------------------------------------------------------------
dict_ENTRY fEQ, fTRUE, 0, 1, "="
m_POP eax
cmp ebx, eax
je cmp_TT
; ... fall through to FALSE
; -----------------------------------------------------------------------------
cmp_FF: mov ebx, 0
ret
cmp_TT: mov ebx, 1
ret
; -----------------------------------------------------------------------------
dict_ENTRY fTRUE, fFALSE, 0, 4, "TRUE"
m_PUSH 1
ret
; -----------------------------------------------------------------------------
dict_ENTRY fFALSE, fDOT2, 0, 5, "FALSE"
m_PUSH 0
ret
; -----------------------------------------------------------------------------
dict_ENTRY fDOT2, fDOT1, 0, 1, "."
call fDOT1_XT
m_PUSH ' '
call fEMIT_XT
ret
; -----------------------------------------------------------------------------
dict_ENTRY fDOT1, fCOUNT_TO, 0, 3, "(.)"
m_PUSH ebx ; DUP
m_PUSH 0 ; CLITERAL (0000)
call fEQ_XT
m_POP eax
cmp eax, 0
je L00000A9B ; JMPZ
m_DROP ; DROP
m_PUSH '0' ; CLITERAL (0030)
call fEMIT_XT
ret
L00000A9B: m_PUSH ebx ; DUP
m_PUSH 0 ; CLITERAL (0000)
call fLT_XT
m_POP eax
cmp eax, 0
je L00000AAE ; JMPZ
m_PUSH 0 ; CLITERAL (0000)
call fthSUB_XT
m_PUSH 1 ; CLITERAL (0001)
jmp L00000AB0 ; JMP
L00000AAE: m_PUSH 0 ; CLITERAL (0000)
L00000AB0:
m_POP eax ; DTOR (>R)
m_RPUSH eax
m_PUSH 0 ; CLITERAL (0000)
call fSWAP_XT
L00000AB4: m_PUSH ebx ; DUP
m_POP eax
cmp eax, 0
je L00000ADA ; JMPZ
call fBASE_XT
call fFETCH_XT
call fSLMOD_XT ; CALL 0A79, /MOD
m_PUSH '0' ; '0' +
call fthADD_XT
m_PUSH ebx ; DUP '9' > IF 7 + THEN
m_PUSH '9' ; CLITERAL (0009)
call fGT_XT
m_POP eax
cmp eax, 0
je L00000AD3 ; JMPZ
m_PUSH 7 ; CLITERAL (0007)
call fthADD_XT
L00000AD3:
call fSWAP_XT
jmp L00000AF7 ; JMP
L00000ADA: m_DROP ; DROP
m_RPOP eax ; RTOD (R>)
m_PUSH eax
m_POP eax
cmp eax, 0
je L00000AE4 ; JMPZ
m_PUSH 45 ; CLITERAL (002D)
call fEMIT_XT
L00000AE4: m_PUSH ebx ; DUP
m_POP eax
cmp eax, 0
je L00000AF0 ; JMPZ
call fEMIT_XT
jmp L00000AF2 ; JMP
L00000AF0: m_POP eax ; DROP
ret
L00000AF2: jmp L00000AE4 ; JMP
L00000AF7: jmp L00000AB4 ; JMP
ret
; -----------------------------------------------------------------------------
dict_ENTRY fCOUNT_TO, fCOUNT, 0, 8, "COUNT_TO"
;
; : COUNT_TO DO 1- DUP 0 > WHILE DROP ;
;
ct: ; DO
dec ebx ; 1-
m_PUSH ebx ; DUP
m_PUSH 0 ; 0
call fGT_XT ; >
m_POP eax ; WHILE
cmp eax, 0
jne ct
m_DROP
ret
; -----------------------------------------------------------------------------
dict_ENTRY fCOUNT, fTYPE, 0, 5, "COUNT"
xor eax, eax
mov al, [ebx]
inc ebx
m_PUSH eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fTYPE, fFETCH, 0, 4, "TYPE"
m_POP ecx
ty:
cmp ecx, 0
jg tyc
m_POP eax
ret
tyc:
m_PUSH ebx
call fCFETCH_XT
push ecx
call fEMIT_XT
pop ecx
inc ebx
dec ecx
jmp ty
; -----------------------------------------------------------------------------
dict_ENTRY fFETCH, fCFETCH, 1, 2, "C@"
mov ebx, [ebx]
ret
; -----------------------------------------------------------------------------
dict_ENTRY fCFETCH, fCSTORE, 1, 1, "@"
xor eax, eax
mov al, [ebx]
mov ebx, eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fCSTORE, fSTORE, 1, 2, "C!"
m_POP edx
m_POP eax
mov [edx], al
ret
; -----------------------------------------------------------------------------
dict_ENTRY fSTORE, fEMIT, 1, 1, "!"
m_POP edx
m_POP eax
mov [edx], eax
ret
; -----------------------------------------------------------------------------
dict_ENTRY fEMIT, fpWORD, 0, 4, "EMIT"
;
; BOOL WINAPI WriteConsole(
; _In_ HANDLE hConsoleOutput,
; _In_ const VOID *lpBuffer,
; _In_ DWORD nNumberOfCharsToWrite,
; _Out_opt_ LPDWORD lpNumberOfCharsWritten,
; _Reserved_ LPVOID lpReserved
m_POP eax
push ebx
mov byte ptr tmp_buf, al
push 0
push 0
push 1
push tmp_buf
push DWORD [STDOUT]
call [WriteConsoleA]
pop ebx
ret
; -----------------------------------------------------------------------------
dict_ENTRY fpWORD, fWORDS, 0, 5, "WORDS"
add ebx, 9
call fCOUNT_XT
jmp fTYPE_XT
; -----------------------------------------------------------------------------
dict_ENTRY fWORDS, fSPACE, 0, 5, ".WORD"
mov ecx, [LAST_ADDR]
wl: cmp ecx, 0
je wlx
push ecx
m_PUSH ecx
call fSPACE_XT
call fpWORD_XT
pop ecx
mov ecx, [ecx]
jmp wl
wlx:
ret
; -----------------------------------------------------------------------------
dict_ENTRY fSPACE, fBYE, 0, 5, "SPACE"
m_PUSH ' '
call fEMIT_XT
ret
; -----------------------------------------------------------------------------
dict_ENTRY fBYE, fComma, 0, 3, "BYE"
m_PUSH ' '
call fEMIT_XT
push 0
push 0
push 3
push fBYE+10
push DWORD [STDOUT]
call [WriteConsoleA]
invoke ExitProcess,0
ret
; -----------------------------------------------------------------------------
dict_ENTRY fComma, fCComma, 0, 1, ","
mov eax, [HERE_ADDR]
m_POP edx
mov [eax], edx
add eax, 4
mov [HERE_ADDR], eax
; invoke MessageBox,HWND_DESKTOP,"Comma called", "xxx",MB_OK
ret
; -----------------------------------------------------------------------------
dict_ENTRY fCComma, fBASE, 0, 2, "C,"
mov eax, [HERE_ADDR]
m_POP edx
mov [eax], dl
inc eax
mov [HERE_ADDR], eax
; invoke MessageBox,HWND_DESKTOP,"CComma called", "xxx",MB_OK
ret
; -----------------------------------------------------------------------------
dict_ENTRY fBASE, fNewDot, 0, 4, "BASE"
m_PUSH BASE_ADDR
ret
; -----------------------------------------------------------------------------
dict_ENTRY fNewDot, fPoundLt, 0, 2, "n."
call fPoundLt_XT
call fPoundS_XT
call fPoundGt_XT
call fTYPE_XT
call fSPACE_XT
ret
; -----------------------------------------------------------------------------
dict_ENTRY fPoundLt, fPound, 0, 2, "<#"
;
; : <# ( u1 -- 0|1 u2 ) DUP 0 > IF 0 - 1 ELSE 0 THEN SWAP ;
;
ret
; -----------------------------------------------------------------------------
dict_ENTRY fPound, fPoundS, 0, 1, "#"
;
; : # ( u1 -- u2 ) BASE @ /MOD '0' + DUP '9' > IF 7 + THEN SWAP ;
;
call fBASE_XT
mov ebx, [ebx]
call fSLMOD_XT
m_PUSH '0'
call fthADD_XT
m_PUSH ebx
m_PUSH '9'
call fGT_XT
m_POP eax
cmp eax, 0
je lbr
m_PUSH 7
call fthADD_XT
lbr:
call fSWAP_XT
ret
; -----------------------------------------------------------------------------
dict_ENTRY fPoundS, fPoundGt, 0, 2, "#S"
;
; : #S ( u1 -- u2 ) DO # DUP WHILE ;
;
lbs1: call fPound_XT
cmp ebx, 0
jne lbs1
ret
; -----------------------------------------------------------------------------
dict_ENTRY fPoundGt, 0, 0, 2, "#>"
ret
; -----------------------------------------------------------------------------
dict_ENTRY gth2, gth, 0, 4, "gth2"
; : gth2 dup . ;
m_PUSH ebx
call fDOT2_XT
ret
; -----------------------------------------------------------------------------
dict_ENTRY gth, 0, 0, 5, "(gth)" ; got-to-here
push ebx
push 0
push 0
push 13
push gth+10
push DWORD [STDOUT]
call [WriteConsoleA]
pop ebx
ret
; -----------------------------------------------------------------------------
DSTACK dd 64 dup 0
DSTACKEND dd 0
tmp_buf db 256 dup 0
; -----------------------------------------------------------------------------
start:
call startup
call mainLoop
call fSPACE_XT
m_PUSH 'z'
m_PUSH 'a'
call fEMIT_XT
m_PUSH 1000000000
call fCOUNT_TO_XT
call fEMIT_XT
m_PUSH 10
call fBASE_XT
call fSTORE_XT
m_PUSH HERE_ADDR
call fDOT2_XT
m_PUSH HERE_ADDR
call fFETCH_XT
call fDOT2_XT
m_PUSH 13
call fCComma_XT
m_PUSH 37
call fPoundS_XT
m_DROP
call fEMIT_XT
call fEMIT_XT
;m_PUSH 37
;m_PUSH 10
;call fSLMOD_XT
;call fDOT2_XT
;call fDOT2_XT
m_PUSH HERE_ADDR
call fFETCH_XT
call fDOT2_XT
m_PUSH 1234
call fComma_XT
m_PUSH HERE_ADDR
call fFETCH_XT
call fDOT2_XT
call fBYE_XT
done: invoke ExitProcess,0
; -----------------------------------------------------------------------------
.end start