-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforth.asm
123 lines (110 loc) · 1.96 KB
/
forth.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
%include "../lab1/lib.inc"
%include "lib.asm"
section .bss
user_mem: resq 65536
com_stack: resq 65536
section .data
word_ptr: dq 0;
xt_exit: dq exit
ifbranch: db 1
srsp: dq 0
program_stub: dq 0
xt_interpreter: dq .interpreter
.interpreter: dq execution
last_word: dq link
section .text
docol:
sub rstack, 8
mov [rstack], pc
add w, 8
mov pc, w
jmp next
exit:
mov pc, [rstack]
add rstack, 8
jmp next
next:
mov w, pc
add pc, 8
mov w, [w]
jmp [w]
find_word:
mov rcx, [last_word]
.loop:
lea rsi, [rcx+8]
push rcx
call string_equals
pop rcx
test rax, rax
jz .next
mov rax, rcx
ret
.next:
mov rcx, [rcx]
test rcx, rcx
jnz .loop
xor rax, rax
ret
cfa:
lea rax, [rdi+8]
.loop:
inc rax
cmp byte[rax], 0
jne .loop
add rax, 2
ret
execution:
call read_word
test rdx, rdx
jz execution
mov [word_ptr], rax
mov rdi, rax;
call find_word
test rax, rax
jz .number
mov rdi, rax
call cfa
cmp byte [state], 0
jz .interpreter_loop
.compiler_loop:
mov dil, [rax-1]
mov [ifbranch], dil
test dil, dil
jnz .notimmediate
.interpreter_loop:
mov qword[program_stub], rax
mov pc, program_stub
jmp next
.notimmediate:
mov rdi, [here]
mov [rdi], rax
add qword [here], 8
jmp execution
.number:
mov rdi, [word_ptr]
call parse_int
test rdx, rdx
jz execution
cmp byte [state], 0
jnz .compiler_number
.interpreter_number:
push rax
jmp execution
.compiler_number:
cmp byte [ifbranch], 2
je .branch
mov rdi, [here]
mov qword [rdi], xt_lit
add qword [here], 8
.branch:
mov rdi, [here]
mov qword [rdi], rax
add qword [here], 8
mov byte [ifbranch], 0
jmp execution
global _start
_start:
mov [srsp], rsp
lea rstack, [com_stack+65536]
mov pc, xt_interpreter
jmp next