-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf.asm
More file actions
383 lines (302 loc) · 6.13 KB
/
Copy pathf.asm
File metadata and controls
383 lines (302 loc) · 6.13 KB
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
%include "common.asm"
FUNCTION1 Print, print ;Print(buf)
global _start
%macro _pop_cell 1 ;_pop_cell underflow
?call pop_cell, %1
%endmacro
%macro _push_cell 2 ;_push_cell val, overflow
?call1 push_cell, %1, %2
%endmacro
%macro defword 3 ;defword name, fp, next
dd %3 ; next
dd %2 ; fp
%strlen __len %1
dd __len ; nl
db %1 ; name + padding
times (32 - __len) db 0
%endmacro
; TODO
global stdin
global stdout
; INTERPRETER/main
; =============================================================================
section .text
_start:
push 0
push stdin
call file_init
add esp, 8
push 1
push stdout
call file_init
add esp, 8
call main
mov eax, SYS_EXIT
mov ebx, 0
int 0x80
main:
_prologue
push esi
push edi
push ebx
sub esp, M_SYM_NAME ; _SLOT(4) = token
mov [p_cell], 0
.loop:
?call1 consume_whitespace, stdin, .error
lea eax, _SLOT(4)
ExpectToken(stdin, eax, .error)
lea ecx, _SLOT(4)
push eax ;length
push ecx ;buffer
call try_parse_number
pop edi ;edi has buffer
pop esi ;esi has token length
cmp eax, 0
jge .number ;edx has number
.operator:
?call2 find_word, edi, esi, .bad_token
mov eax, [edx + fword.fp]
call eax
cmp eax, 0
jl .runtime_error
jmp .reloop
.number: ;edx has number
_push_cell edx, .stack_overflow
.reloop:
jmp .loop
.runtime_error:
cmp eax, FE_OVER
je .stack_overflow
cmp eax, FE_UNDER
je .stack_underflow
jmp .error
.bad_token:
Print(s_invalidt)
?call1 consume_non_whitespace, stdin, .error
?call3 file_write, stdout, edi, esi, .error
Putc(stdout, 10, .error)
jmp .reloop
.stack_overflow:
Print(s_overflow)
jmp .error
.stack_underflow:
Print(s_underflow)
?call1 consume_non_whitespace, stdin, .error
jmp .reloop
.error:
Print(s_error)
.ret:
lea esp, _SLOT(3)
pop ebx
pop edi
pop esi
_epilogue
; FORTH
; =============================================================================
section .text
; Functions:
; push_cell(val)
; pop_cell()
; find_word(buf, length)
; Globals:
; p_cell
; cells
; dictionary
; load address of next word, jump
%macro NEXT 0
lodsw
jmp [eax]
%endmacro
%macro EXIT 0
pop esi
NEXT
%endmacro
; save esi
docol:
push esi
add eax, 4
mov esi, eax
NEXT
; IN: value to push
; OUT: eax = FE_OVER on overflow
push_cell: ;push_cell(val)
_prologue
mov edx, [p_cell]
cmp edx, CELL_COUNT
jge .bad
mov eax, _ARG(0)
mov [cells + edx * 4], eax
inc dword [p_cell]
mov eax, 0
jmp .ret
.bad:
mov eax, FE_OVER
.ret:
_epilogue
; IN: none
; OUT: eax = FE_UNDER on underflow, edx = value on success
pop_cell: ;pop_cell()
_prologue
dec dword [p_cell]
mov ecx, [p_cell]
cmp ecx, 0
jl .underflow
mov edx, [cells + ecx*4]
mov eax, 0
jmp .ret
.underflow:
mov eax, FE_UNDER
mov dword [p_cell], 0
.ret:
_epilogue
; IN: none
; OUT: eax = negative on error
print_stack: ;print_stack()
_prologue
push ebx
mov ebx, 0
.loop:
cmp ebx, [p_cell]
jge .finish
lea eax, [cells + 4 * ebx]
Wnum(stdout, [eax], .error)
Putc(stdout, ' ', .error)
inc ebx
jmp .loop
.finish:
Putc(stdout, 10, .error)
mov eax, 0
.error:
.ret:
lea esp, _SLOT(1)
pop ebx
_epilogue
; IN: buf, length
; OUT: eax = negative or 0, edx = fword* on success
; searches dictionary for word with given name
find_word: ;find_word(buf, length)
_prologue
push esi
mov esi, dictionary.root
.loop:
mov eax, [esi + fword.nl]
cmp eax, _ARG(1)
jnz .cont
push _ARG(1) ;same length
lea eax, [esi + fword.name]
push eax
push _ARG(0)
call strncmp
add esp, 12
test eax, eax
jnz .cont
mov eax, 0 ;same name = found
mov edx, esi
jmp .ret
.cont:
mov esi, [esi + fword.next]
test esi, esi
jz .error
jmp .loop
.error:
mov eax, -1
.ret:
lea esp, _SLOT(1)
pop esi
_epilogue
; helpers for generating binary intrinsics
; after sbintrins, edx has stack left, ecx has stack right
; runtime errors should jump to .ret
%macro sbintrins 0 ; sbintrins
_prologue
_pop_cell .ret
push edx
_pop_cell .ret
pop ecx
%endmacro
%macro ebintrins 0 ; ebintrins
mov eax, 0
.ret:
_epilogue
%endmacro
intrinsic_add: ;intrinsic_add (n1 n2 -- n1 + n2)
sbintrins
add edx, ecx
_push_cell edx, .ret
ebintrins
intrinsic_sub: ;intrinsic_sub (n1 n2 -- n1 - n2)
sbintrins
sub edx, ecx
_push_cell edx, .ret
ebintrins
intrinsic_mul:
sbintrins
mul edx, ecx
_push_cell edx, .ret
ebintrins
intrinsic_div:
sbintrins
mov eax, edx
mov edx, 0
idiv ecx ; eax := q, edx := r
_push_cell eax, .ret
ebintrins
intrinsic_beg_def:
_prologue
mov eax, FM_BEGDEF
_epilogue
intrinsic_end_def:
_prologue
mov eax, FM_ENDDEF
_epilogue
intrinsic_dot:
_prologue
_pop_cell .ret
Wnum(stdout, edx, .io_err)
Putc(stdout, 10, .io_err)
jmp .ret
.io_err:
mov eax, FE_IO
.ret:
_epilogue
intrinsic_print_stack:
_prologue
?call print_stack, .io_err
mov eax, 0
jmp .ret
.io_err:
mov eax, FE_IO
.ret:
_epilogue
; DATA
; =============================================================================
; BSS
section .bss
stdin: align 4
resb file_size
stdout: align 4
resb file_size
p_cell: align 4
resd 1
cells: align 4
resb 4*CELL_COUNT
; DATA
section .data
s_prompt: db "> ", 0
s_error: db "an error occured", 10, 0
s_rec: db "received new line", 10, 0
s_overflow db "stack overflow", 10, 0
s_underflow db "stack underflow", 10, 0
s_invalidt db "invalid token: ", 0
s_operator db "operator", 10, 0
s_number db "number", 10, 0
dictionary: align 32
.root:
.sub: defword "-", intrinsic_sub, .add
.add: defword "+", intrinsic_add, .mul
.mul: defword "*", intrinsic_mul, .div
.div: defword "/", intrinsic_div, .dot
.dot: defword ".", intrinsic_dot, .bword
.bword: defword ":", intrinsic_beg_def, .eword
.eword: defword ";", intrinsic_end_def, .dots
.dots: defword ".s", intrinsic_print_stack, 0