-
Notifications
You must be signed in to change notification settings - Fork 1
/
Forth-STC.ASM
287 lines (221 loc) · 6.65 KB
/
Forth-STC.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
; A simple STC Forth implementation
format pe console
; you can simply switch between win32ax, win32wx, win64ax and win64wx here
include 'win32ax.inc'
include "./Forth-STC-Macros.inc"
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
section '.data' data readable writeable executable
MEMORY_SIZE = 1024*2
init_esp dd 0
STDIN dd 0
STDOUT dd 0
tmp_buf db 256 dup (0)
; The DATA stack
dstk_ptr dd 0
dstk_data dd 64 dup (0)
; our "MEMORY" starts here
the_memory db MEMORY_SIZE dup (0)
; Leave room for 8 DWORDS
HERE_ADDR = the_memory + 0x0020
LAST_ADDR = HERE_ADDR + 4
BASE_ADDR = LAST_ADDR + 4
STATE_ADDR = BASE_ADDR + 4
; Code starts 8 DWORDS after the pointers
LAST = the_memory + 0x0040
HERE = LAST + 4
Hello db 5, 'Hello', 0
OK db 5, ' ok', 13, 10, 0
; ------------------------------------------------------------------------------
section '.text' code readable writable executable
; ------------------------------------------------------------------------------
start:
call sys_init
; Save the initial value of the stack pointer, esp
mov [init_esp], esp
m_push Hello
call L0000037A ; COUNT
call L00000386 ; TYPE
;jmp L0000096B ; main
jmp mainLoop
call prim_BYE
; ------------------------------------------------------------------------------
sys_init:
mov [dstk_ptr], dstk_ptr
mov ebp, dstk_ptr
mov DWORD [HERE_ADDR], HERE
mov DWORD [LAST_ADDR], LAST
mov DWORD [BASE_ADDR], 10
mov DWORD [LAST], 0
push STD_INPUT_HANDLE
call [GetStdHandle]
mov [STDIN], eax
push STD_OUTPUT_HANDLE
call [GetStdHandle]
mov [STDOUT], eax
ret
; ------------------------------------------------------------------------------
mainLoop:
m_push OK
call L0000037A ; COUNT
call L00000386 ; TYPE
mov esp, [init_esp]
call CCCtest
jmp prim_BYE
; jmp mainLoop
ret
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
include "./Forth-STC-Prims.inc"
; ------------------------------------------------------------------------------
doCopy:
; esi = start
; edi = dest
; ecx = number of bytes to move
push esi
push edi
push ecx
cld ; CLD => auto-increment, STD => auto-decrement
c1: rep movsb
pop ecx
pop edi
pop esi
ret
CopyStart:
CCCtest2:
m_push 'X'
call prim_EMIT
ret
CopyEnd:
CCCtest:
; es=ds? yes
; cs=ds? no
; ss=ds? yes (what is this for? - don't use)
; fs=ds? no (what is this for? - don't use)
; gs=ds? yes (what is this for? - don't use)
m_push 20
m_push 1
call L00000BAA ; countTo
m_push 16
m_push BASE_ADDR ; (BASE)
call prim_STORE
;m_push HERE_ADDR
;call L00000B06 ; .
m_push 10
m_push BASE_ADDR ; (BASE)
call prim_STORE
call L00000BCB ; doCount
m_push OK
call L0000037A ; COUNT
call L00000386 ; TYPE
m_push [STDIN] ;
call L00000B06 ; .
m_push [STDOUT] ;
call L00000B06 ; .
m_push OK
call L0000037A ; COUNT
call L00000386 ; TYPE
m_push the_memory ; start of memory buf
call L00000B06 ; .
m_push HERE_ADDR ; (HERE)
call L00000B06 ; .
m_push LAST_ADDR ; (LAST)
call L00000B06 ; .
m_push HERE_ADDR ; HERE
mov ebx, [ebx] ; @
call L00000B06 ; .
m_push LAST_ADDR ; LAST
mov ebx, [ebx] ; @
call L00000B06 ; .
kk: m_push OK
call L0000037A ; COUNT
call L00000386 ; TYPE
call L00000C2F
call L00000C2F
call L00000C2F
call L00000C2F
call L00000C2F
call L00000C2F
call L00000C2F
jmp prim_BYE
ret
ml: call prim_GETCH
m_push ebx ; DUP 13 = IF <eol> THEN
m_push 13
call prim_EQ
m_pop eax
cmp eax, 1
jne ml1
eol: m_drop
jmp kk
ret
ml1: m_push ebx ; DUP ' ' < IF DROP <continue> THEN
m_push 32
call prim_LT
m_pop eax
cmp eax, 0
je bbq
m_drop
jmp ml
bbq: m_push ebx ; DUP 'x' < IF BYE THEN
m_push 'x'
call prim_EQ
m_pop eax
cmp eax, 1
je prim_BYE
call prim_EMIT
jmp ml
;push es
;pop eax
;push ds
;pop edx
;cmp eax, edx
mov esi, CopyStart
mov edi, HERE
mov ecx, CopyEnd
sub ecx, esi
call doCopy
mov al, [esi]
mov ah, [edi]
cmp al, 0
je nn
cmp al, ah
je yy
nn:
m_push '.'
m_push 'n'
jmp zz
yy:
m_push '.'
m_push 'y'
mov edx, CCCtest2
; mov edx, HERE
call edx
zz:
call prim_EMIT
call prim_EMIT
;mov edx, FIRST
;add edx, 4
;call RWORD [edx]
ret
; The RETURN stack
;rstk_ptr dd 0
;rstk_data dd 64 dup (0)
; our "MEMORY" ends here
; ------------------------------------------------------------------------------
getWord:
; This gets one word from the input stream.
; Note, the word is counted and null-terminated.
; It also puts a pointer to that word and the word-length on the stack.
; If the length = 0, that implies that we have hit <EOF> on the input stream.
; For STDIN, the length should never be 0.
call L00000C2F
ret
; ------------------------------------------------------------------------------
include "./Forth-STC-Words.ASM"
; ------------------------------------------------------------------------------
xxx123:
; ------------------------------------------------------------------------------
.end start