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
|
; sanctuary
; macros {{{
; TODO: error handling (once i add that)
%macro pspush 1
lea r15, [r15-8]
mov qword [r15], %1
%endmacro
%macro pspop 1
mov %1, qword [r15]
lea r15, [r15+8]
%endmacro
%define s_latest 0
%macro defdict 3 ; name label flags
%strlen slen %1
global lfa_%2
lfa_%2: dq s_latest
%define s_latest lfa_%2
ffa_%2: db %3
nfa_%2: db slen
db %1
%endmacro
%macro defcode 3
defdict %1, %2, %3
%2:
%endmacro
; this is just taken from jewelforth, and does not correspond
; to how user variables are planned to work in sanctuary
; so todo make better later? i don't know if it really matters
; because it will only apply to builtin variables.
%macro defvar 4
%2: dq %4
defdict %1, %2, %3
pspush qword %2
%endmacro
; }}}
%assign INTERPRET 0x0
%assign COMPILING (~0x0)
%assign __NR_write 1
%assign __NR_mprotect 10
%assign __NR_brk 12
%assign __NR_exit 60
section .bss
resq 4091
wstk: resq 1
section .text
global _start
_start:
lea r15, [wstk]
call brk@
pspop r11
mov qword [dp], r11
mov qword [dp0], r11
mov r11, 0x9c400
pspush r11
call grow
call parse_name
call type
call bye
teststr: db "TESTING"
defcode "brk@", brk@, 0
xor rdi, rdi
mov rax, __NR_brk
syscall
pspush rax
ret
defcode "grow", grow, 0
call brk@
pspop rdi
pspop r13
add rdi, r13
mov rax, __NR_brk
syscall
mov qword [dp$], rax
ret
defcode "executable", executable, 0
mov rdx, 0x7 ; PROT_{READ,WRITE,EXEC}
pspop rdi ; addr
pspop rsi
mov rax, __NR_mprotect
syscall
ret
defcode "here", here, 0
mov r11, qword [dp]
pspush r11
ret
defcode "bye", bye, 0
mov rdi, 0
mov rax, __NR_exit
syscall
ret
; input parsing {{{
; r11: string character count
; rsi: input buffer address
; al: char being parsed
; r10: end of input buffer
defcode "parse-name", parse_name, 0
mov rsi, qword [to_in]
mov r10, qword [tib]
add rsi, r10
add r10, qword [n_tib]
xor rax, rax
.wsloop:
cmp rsi, r10
jge .empty
lodsb
cmp al, 0x20
je .wsloop
cmp al, 0x09
je .wsloop
cmp al, 0x0a
je .wsloop
cmp rsi, r10
jge .empty
mov r11, 1
dec rsi ; bring down by one to point to the start
push rsi ; will become `a`
inc rsi
.wordloop:
cmp al, 0x20
je .wordloop_e
cmp al, 0x09
je .wordloop_e
cmp al, 0x0a
je .wordloop_e
cmp rsi, r10
jge .wordloop_e
inc r11
lodsb
jmp .wordloop
.wordloop_e:
dec r11
sub rsi, qword [tib]
mov qword [to_in], rsi
pop rsi
pspush rsi
pspush r11
ret
.empty:
pspush 0
pspush 0
ret
; r11: string character count
; rsi: input buffer address
; al: char being parsed
; r10: end of input buffer
defcode "parse", parse, 0
pspop rbx
mov rsi, qword [to_in]
mov r10, qword [tib]
add rsi, r10
add r10, qword [n_tib]
xor rax, rax
.wsloop:
cmp rsi, r10
jge .empty
lodsb
cmp al, bl
je .wsloop
cmp al, 0x0a
je .wsloop
cmp rsi, r10
jge .empty
mov r11, 1
dec rsi ; bring down by one to point to the start
push rsi ; will become `a`
inc rsi
.wordloop:
cmp al, bl
je .wordloop_e
cmp al, 0x0a
je .wordloop_e
cmp rsi, r10
jge .wordloop_e
inc r11
lodsb
jmp .wordloop
.wordloop_e:
dec r11
sub rsi, qword [tib]
mov qword [to_in], rsi
pop rsi
pspush rsi
pspush r11
ret
.empty:
pspush 0
pspush 0
ret
; }}}
defcode "type", type, 0
pspop rdx
pspop rsi
mov rdi, 1
mov rax, __NR_write
syscall
ret
; .s {{{
defcode ".s", dots, 0
push r11
push r12
mov r12, r15
.loop:
cmp r12, wstk
jge .done
mov [.space], r12
mov rdx, 8 ; qword
mov rsi, .space
mov rdi, 1
mov rax, __NR_write
syscall
mov rdx, 8 ; qword
mov rsi, r12
mov rdi, 1
mov rax, __NR_write
syscall
lea r12, [r12+8]
jmp .loop
.done:
mov rdx, 16 ; 2 qword
mov rsi, .dmsg
mov rdi, 1
mov rax, __NR_write
syscall
pop r12
pop r11
ret
.space: resq 1
.dmsg: db "DONEDONEYIPPEEEE"
; }}}
defvar "state", state, 0, INTERPRET
defvar "dp", dp, 0, 0
defvar "dp0", dp0, 0, 0
defvar "dp$", dp$, 0, 0
defvar "tib", tib, 0, initfile
defvar "#tib", n_tib, 0, initlen
defvar ">in", to_in, 0, 0
defvar "latest", latest, 0, lfa_latest
initfile: incbin "sanctuary.fs"
initlen equ $ - initfile
|