forked from Firemoon777/forth-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.asm
328 lines (291 loc) · 4.66 KB
/
dict.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
section .text
; Ищет слово в словаре
; rdi -- ссылка на слово
; rax -- адрес начала слова
find_word:
;push rdi
xor rax, rax
mov rsi, [last_word]
.loop:
push rsi
add rsi, 8
call string_equals
pop rsi
test rax, rax
jnz .finish
mov rsi, [rsi]
test rsi, rsi
jnz .loop
xor rax, rax
ret
.finish:
mov rax, rsi
;pop rdi
ret
; Возвращает адрес xt от слова, на который указывает rdi
cfa:
xor rax, rax
add rdi, word_size
.loop:
mov al, [rdi]
test al, 0xFF
jz .finish
inc rdi
jmp .loop
.finish:
add rdi, 2
mov rax, rdi
ret
; compiler mode
; native block
native '+', plus
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
add [rsp], rax
jmp next
native '-', minus
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
sub [rsp], rax
jmp next
native '*', multiple
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
pop rcx
imul rcx
push rax
jmp next
native '/', division
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rcx
pop rax
cqo
idiv rcx
push rax
jmp next
native '=', equals
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
pop rcx
cmp rax, rcx
sete al
movzx rax, al
push rax
jmp next
native '<', less
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
pop rcx
cmp rcx, rax
setl al
movzx rax, al
push rax
jmp next
native 'and', log_and
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
pop rcx
and rax, rcx
cmp rax, 0
setne al
movzx rax, al
push rax
jmp next
native 'not', negation
cmp rsp, [stackHead]
jge error_underflow
pop rax
test rax, rax
setne al
movzx rax, al
push rax
jmp next
native 'rot', rot
mov rax, rsp
add rax, 2*word_size
cmp rax, [stackHead]
jge error_underflow
pop rcx ; 3
pop rdx ; 2
pop rax ; 1
push rdx ; 2
push rcx ; 3
push rax ; 1
jmp next
native 'swap', swap_stack
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
pop rcx
push rax
push rcx
jmp next
native 'dup', dup
cmp rsp, [stackHead]
jge error_underflow
pop rax
push rax
push rax
jmp next
native 'drop', drop
cmp rsp, [stackHead]
jge error_underflow
pop rax
jmp next
native '.', dot
cmp rsp, [stackHead]
jge error_underflow
pop rdi
call print_int
jmp next
native 'key', key
call read_char
push rdi
jmp next
native 'emit', emit
cmp rsp, [stackHead]
jge error_underflow
pop rdi
call print_char
jmp next
native 'number', number
call read_word
mov rax, rdi
call parse_int
push rax
jmp next
native 'mem', mem
push qword[ustackHead]
jmp next
native '@', data_read
cmp rsp, [stackHead]
jge error_underflow
pop rax
mov rax, [rax]
push rax
jmp next
native '!', data_write
mov rax, rsp
add rax, 1*word_size
cmp rax, [stackHead]
jge error_underflow
pop rax
pop rdx
mov [rax], rdx
jmp next
native '.S', show_stack
mov rax, rsp
.loop:
cmp rax, [stackHead]
jge next
mov rdi, [rax]
push rax
call print_int
call print_newline
pop rax
add rax, 8
jmp .loop
native 'exit', close_int
jmp close
; colon block
colon '>', greater
dq xt_swap_stack
dq xt_less
dq xt_exit
colon 'or', log_or
dq xt_negation
dq xt_swap_stack
dq xt_negation
dq xt_log_and
dq xt_negation
dq xt_exit
; compiler mode
native ':', col, 1
mov byte[state], 1
mov rdi, [last_word]
mov [here], rdi
mov qword[last_word], here
add here, word_size
call read_word
mov rdi, rax
mov rsi, here
call string_copy
mov here, rsi
inc here
mov qword[here], docol
add here, word_size
jmp next
native ';', semicolon, 1
mov byte[state], 0
mov qword[here], xt_exit
add here, word_size
jmp next
native 'lit', lit
push qword[pc]
add pc, word_size
jmp next
native 'branch', branch, 2
mov rax, qword[pc]
inc rax
mov rcx, word_size
mul rcx
js .back
add pc, rax
jmp next
.back:
neg rax
sub pc, rax
jmp next
native 'branch0', branch0, 2
pop rcx
test rcx, rcx
jnz .finish
jmp branch_impl
.finish:
add pc, word_size
jmp next
; data block
section .data
last_word: dq link
xt_docol: dq docol
xt_exit: dq exit
; error handling block
error_underflow:
mov rdi, underflow
call print_string
jmp next
; Начало всех colon-слов
docol:
sub rstack, 8
mov [rstack], pc
add w, 8
mov pc, w
jmp next
; конец всех colon-слов
exit:
mov pc, [rstack]
add rstack, 8
jmp next