aboutsummaryrefslogtreecommitdiffstats
path: root/src/kernel/forth.asm
blob: 5ac04965a1749a3bab87988f3a4b4ad4900cc668 (plain)
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
;; forth.asm

%ifndef FORTH_ASM
%define FORTH_ASM

%macro defword 2-3 0
        word_%2:    
        dd link
%define link word%2
%strlen %%len %1
        db %3+%%len
        db %1
        align 4
        dd %2
%endmacro

;; Collection of useful macros to built on later
%macro NEXT 0
        lodsl
        jmp [eax]
%endmacro

%macro DATA_PUSH 1
        lea ebp, [ebp - 4]
        mov ebp, %1
%endmacro

%macro DATA_POP 1
        mov %1, [ebp]
        lea ebp, [ebp + 4]
%endmacro

%macro RET_PUSH 1
        mov %1, [edi]
        lea edi, [edi + 4]
%endmacro

%macro RET_POP 1
        lea edi, [edi - 4]
        mov [edi], %1
%endmacro

%macro DOCOL 0
        RET_PUSH esi
        lea esi, [ebx + 4]
        NEXT
%endmacro

;; Words related to the stack

        defword "DROP", DROP
        DATA_POP eax
        NEXT

        defword "SWAP", SWAP
        DATA_POP eax
        DATA_POP ebx
        DATA_PUSH eax
        DATA_PUSH ebx
        NEXT

        defword "DUP", DUP
        DATA_POP eax
        DATA_PUSH eax
        DATA_PUSH eax
        NEXT

        defword "OVER", OVER
        DATA_POP eax
        DATA_POP ebx
        DATA_PUSH ebx
        DATA_PUSH eax
        DATA_PUSH ebx
        NEXT

        defword "ROT", ROT
        DATA_POP eax
        DATA_POP ebx
        DATA_POP ecx
        DATA_PUSH ebx
        DATA_PUSH eax
        DATA_PUSH ecx
        NEXT

        defword "-ROT", NROT
        DATA_POP eax
        DATA_POP ebx
        DATA_POP ecx
        DATA_PUSH eax
        DATA_PUSH ecx
        DATA_PUSH ebx
        NEXT
        
        defword "2DROP", TWODROP
        DATA_POP eax
        DATA_POP eax
        NEXT
        
        defword "2DUP", TWODUP
        DATA_POP eax
        DATA_POP ebx
        DATA_PUSH ebx
        DATA_PUSH eax
        DATA_PUSH ebx
        DATA_PUSH eax
        NEXT

        defword "?DUP", QDUP
        DATA_POP eax
        test eax, eax
        jz .done
        DATA_PUSH eax
        DATA_PUSH eax
.done:
        NEXT

;; Words related to memory

        defword "@", FETCH
        DATA_POP ebx
        mov eax, [ebx]
        DATA_PUSH eax
        NEXT
        
        defword "!", STORE
        DATA_POP ebx
        DATA_POP eax
        mov [ebx], eax
        NEXT

        defword "C@", CFETCH
        DATA_POP ebx
        movzx eax, byte [ebx]
        DATA_PUSH eax
        NEXT

        defword "C!", CSTORE
        DATA_POP ebx
        DATA_POP eax
        mov [ebx], al

;; Words related to the interpreter

        defword "EXIT", EXIT_WORD
        RET_POP esi
        NEXT

        defword "LIT", LITERAL
        lodsl
        DATA_PUSH eax
        NEXT

        defword "BRANCH", branch
        lodsl
        add esi, eax
        NEXT
        
        defword "0BRANCH", ZERO_BRANCH
        DATA_POP eax
        test eax, eax
        jnz .skip
        lodsl
        add esi, eax
        NEXT
.skip:
        add esi, 4
        NEXT

        defword "HERE", HERE
        DATA_PUSH dword [here]
        NEXT

        defword "ALLOT", ALLOT
        DATA_POP eax
        add [here], eax
        NEXT

        defword ",", COMMA
        DATA_POP eax
        mov ebx, [here]
        mov [ebx], eax
        add dword [here], 4
        
%endif