-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-functions.asm
553 lines (472 loc) · 8.16 KB
/
core-functions.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
%include "util.inc"
%include "macro.inc"
native drop, "drop"
pop rax
jmp next
native swap, "swap"
pop rax
pop rdx
push rax
push rdx
jmp next
native rot, "rot"
pop rax
pop rdx
pop rcx
push rax
push rcx
push rdx
jmp next
native dup, "dup"
push qword[rsp]
jmp next
native not, "not"
pop rax
test rax, rax
jz .zero
xor rax, rax
push rax
jmp next
.zero:
mov rax, 1
push rax
jmp next
native and, "and"
pop rax
pop rdx
and rax, rdx
push rax
jmp next
native or, "or"
pop rax
pop rdx
or rax, rdx
push rax
jmp next
native land, "land"
pop rax
pop rdx
test rax, rax
jz .no
push rdx
jmp next
.no:
push rax
jmp next
native lor, "lor"
pop rax
pop rdx
test rax, rax
jnz .yes
push rdx
jmp next
.yes:
push rax
jmp next
native show_stack, ".S"
mov rcx, rsp
.loop:
cmp rcx, [stack_start]
je next
mov rdi, [rcx]
push rcx
call print_int
call print_newline
pop rcx
add rcx, 8
jmp .loop
native to_ret, ">r"
pop rax
sub rstack, 8
mov qword [rstack], rax
jmp next
native from_ret, "r>"
mov rax, qword[rstack]
add rstack, 8
push rax
jmp next
native ret_fetch, "r@"
push qword [rstack]
jmp next
native colon, ":"
mov r8, [here] ; put previous address firstly
mov r9, [last_word] ;
mov qword[r8], r9 ;
mov qword[last_word], r8 ; set last word to current
add qword[here], 8 ; update here
mov rdi, input_buf ; read new word's name
mov rsi, 1024 ;
call read_word ;
mov rdi, rax ;
mov rsi, [here] ;
mov rdx, 1024 ;
push rsi ;
call string_copy ; put it into word defitinion
pop rsi ;
mov rdi, rsi ;
call string_length ;
add qword[here], rax ;
add qword[here], 2 ; update here
mov r8, [here] ;
mov qword[r8], docol_impl ; put docol here
add qword[here], 8 ; update here
mov qword[state], 1 ; change state to compilation
jmp next ;
native semicolon, ";", 1
mov r8, [here] ;
mov qword[r8], xt_exit ; put xt_exit at the end
add qword[here], 8 ; update here
mov qword[state], 0 ; change state back
jmp next ;
native branch, "branch"
mov pc, [pc]
jmp next
native branch0, "branch0"
pop rax
test rax, rax
jnz .skip
mov pc, [pc]
jmp next
.skip:
add pc, 8
jmp next
native emit, "emit"
pop rdi
call print_char
jmp next
native word, "word"
mov rdi, input_buf
mov rsi, 1024
call read_word
mov rdi, rax
pop rsi
mov rdx, 1024
call string_copy
push rdx
jmp next
native exit, "exit"
mov pc, [rstack]
add rstack, 8
jmp next
native docol, "docol"
sub rstack, 8
mov [rstack], pc
add w, 8
mov pc, w
jmp next
native lit, "lit"
push qword [pc]
add pc, 8
jmp next
native fetch, "@"
pop rax
mov r10, [rax]
push r10
jmp next
native write, "!"
pop rax
pop r10
mov [r10], rax
jmp next
native write_char, "c!"
pop rax
pop r10
mov byte[r10], al
jmp next
native plus, "+"
pop r10
pop rax
add rax, r10
push rax
jmp next
native minus, "-"
pop r10
pop rax
sub rax, r10
push rax
jmp next
native multiply, "*"
pop r10
pop rax
imul r10
push rax
jmp next
native divide, "/"
pop r10
pop rax
xor rdx, rdx
idiv r10
push rax
jmp next
native mod, "%"
pop r10
pop rax
xor rdx, rdx
idiv r10
push rdx
jmp next
native equals, "="
pop rsi
pop rdi
cmp rdi, rsi
je .equals
push 0
jmp next
.equals:
push 1
jmp next
native lt, "<"
pop rsi
pop rdi
cmp rsi, rdi
jg .greather
push 0
jmp next
.greather:
push 1
jmp next
native gt, ">"
pop rsi
pop rdi
cmp rdi, rsi
jg .greather
push 0
jmp next
.greather:
push 1
jmp next
native dot, "."
pop rdi
call print_int
call print_newline
jmp next
native find_word, "find_word"
pop rsi
mov rdi, last_word
.loop:
push rdi
lea rdi, [rdi + 8]
push rsi
call string_equals
pop rsi
pop rdi
cmp rax, 0
je .find
mov rdi, [rdi]
cmp rdi, 0
je .fail
jmp .loop
.find:
mov rax, rdi
jmp .return
.fail:
mov rax, 0
.return:
push rax
jmp next
native cfa, "cfa"
pop rdi
lea rdi, [rdi + 8]
push rdi
call string_length
pop rdi
lea rax, [rdi + rax + 2]
push rax
jmp next
native bye, "bye"
call exit
native inbuf, "inbuf"
mov rdi, input_buf
mov rsi, 1024
call read_word
push rax
jmp next
native exec, "exec"
pop rax
mov w, rax
jmp [rax]
native count, "count"
pop rdi
call string_length
push rax
jmp next
native number, "number"
pop rdi
call parse_int
push rdx
push rax
jmp next
native prints, "prints"
pop rdi
call print_string
jmp next
native put_state, "put_state"
push qword[state]
jmp next
native add_word, "add_word"
pop rax
mov r9, [here] ; if not immediate, put it xt here
mov [r9], rax ;
add qword[here], 8 ;
jmp next ;
native immediate, "immediate"
pop rdi
lea rdi, [rdi + 8]
call string_length
lea rax, [rdi + rax + 1]
mov rdi, rax
xor rax, rax
mov al, byte[rdi]
push rax
jmp next
native check_branch, "check_branch"
sub qword[here], 8 ; check if prev word is branch or
mov r8, [here] ; branch0
cmp qword[r8], xt_branch ;
je .branch ;
cmp qword[r8], xt_branch0 ;
je .branch ;
mov rax, 0
jmp .return
.branch:
mov rax, 1
.return:
push rax
add qword[here], 8
jmp next
native here, "here"
push qword[here]
jmp next
native last_word, "last_word"
push qword[last_word]
jmp next
native IMMEDIATE, "IMMEDIATE"
mov rdi, [last_word]
lea rdi, [rdi + 8]
push rdi
call string_length
pop rdi
lea rax, [rdi + rax + 1]
mov byte[rax], 1
jmp next
colon selector, "selector"
.loop:
dq xt_put_state
dq xt_branch0
dq .interpret
dq xt_compiler
dq xt_branch
dq .loop
.interpret:
dq xt_interpret
dq xt_branch
dq .loop
colon compiler, "compiler"
dq xt_inbuf
dq xt_dup
dq xt_find_word
dq xt_dup
dq xt_branch0
dq .not_word
.word:
dq xt_swap
dq xt_drop
dq xt_dup
dq xt_immediate
dq xt_branch0
dq .add_word
dq xt_cfa
dq xt_exec
dq xt_exit
.add_word:
dq xt_cfa
dq xt_add_word
dq xt_exit
.not_word:
dq xt_drop
dq xt_dup
dq xt_count
dq xt_dup
dq xt_branch0
dq .empty_line
dq xt_swap
dq xt_dup
dq xt_number
dq xt_to_ret
dq xt_rot
dq xt_rot
dq xt_equals
dq xt_branch0
dq .not_found
dq xt_drop
dq xt_from_ret
dq xt_check_branch
dq xt_branch0
dq .no_branch
.branch:
dq xt_add_word
dq xt_exit
.no_branch:
dq xt_lit
dq xt_lit
dq xt_add_word
dq xt_branch
dq .branch
.not_found:
dq xt_from_ret
dq xt_drop
dq xt_lit, not_found
dq xt_prints
dq xt_prints
dq xt_lit, 10
dq xt_emit
dq xt_exit
.empty_line:
dq xt_drop
dq xt_drop
dq xt_exit
colon interpret, "interpret"
dq xt_inbuf
dq xt_dup
dq xt_find_word
dq xt_dup
dq xt_branch0
dq .not_word
.word:
dq xt_swap
dq xt_drop
dq xt_cfa
dq xt_exec
dq xt_exit
.not_word:
dq xt_drop
dq xt_dup
dq xt_count
dq xt_dup
dq xt_branch0
dq .empty_line
dq xt_swap
dq xt_dup
dq xt_number
dq xt_to_ret
dq xt_rot
dq xt_rot
dq xt_equals
dq xt_branch0
dq .not_found
dq xt_drop
dq xt_from_ret
dq xt_exit
.not_found:
dq xt_from_ret
dq xt_drop
dq xt_lit, not_found
dq xt_prints
dq xt_prints
dq xt_lit, 10
dq xt_emit
dq xt_exit
.empty_line:
dq xt_drop
dq xt_drop
dq xt_exit