-
Notifications
You must be signed in to change notification settings - Fork 1
/
bf.wsasm
357 lines (304 loc) · 5.88 KB
/
bf.wsasm
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
; A brainfuck interpreter in whitespace.
; Reads the program from STDIN. A `!` character marks
; the end of the program and the start of input.
; Heap layout:
; [0] = input character
; [1] = length of program (L)
; [2 .. 2 + 2*L] = program and associated information
; [2*L + 2 ..] = tape
; Step 1: Parse the program, using the stack to store
; the location of matching opening brackets.
; Specifically, we say that the *match* of a bracket
; is a number one greater than the index of its closing
; bracket.
; stack = []
; L = 0
parse:
; stack = [...]
; read a character and push it to the stack
push 0
dup
ichr
get
; stack = [..., c]; heap[0] = c
; if c == '!', stop parsing
push 33
sub
jz end_parse
; stack = [...]
; L += 1
push 1
dup
get
push 1
add
set
; stack = [...]
; get two more copies of the character
push 0
get
dup
; stack = [..., c, c]
; get the appropriate heap location and assign the character there
push 1
get
push 2
mul
swap
set
; stack = [..., c]; heap[2 * L] = c
; if c == '[': goto parse_open_bracket
dup
push 91
sub
jz parse_open_bracket
; stack = [..., c]
; elif c == ']': goto parse_close_bracket
push 93
sub
jz parse_close_bracket
; else: continue
jmp parse
parse_open_bracket:
; stack = [..., c]
; push L
pop
push 1
get
jmp parse
parse_close_bracket:
; stack = [..., match_loc]
; heap[match_loc * 2 + 1] = L
dup
push 2
mul
push 1
add
push 1
get
set
; stack = [..., match_loc]
; heap[L * 2 + 1] = match_loc
push 1
get
push 2
mul
push 1
add
swap
set
jmp parse
; Parsing is finished.
; We can now use heap cell `0` to store the data pointer.
; We can also use the stack to store the instruction pointer.
end_parse:
; stack = []
; heap[0] = push(2*L + 2)
push 0
push 1
dup
get
add
push 2
mul
set
; stack = []
; push ip
push 0
; Step 2: Execute the program.
run:
; stack = [ip]
; if ip == L: goto end_run
dup
push 1
get
sub
jz end_run
; stack = [ip]
; Increment the instruction pointer.
dup
push 1
add
swap
; stack = [ip+1, ip]
; Fetch the current character and its match.
push 2
mul
push 3
add
dup
get
swap
push 1
sub
get
; stack = [ip+1, m, c]
; if c == '[': goto run_open_bracket
dup
push 91
sub
jz run_open_bracket
; stack = [ip+1, m, c]
; if c == ']': goto run_close_bracket
dup
push 93
sub
jz run_close_bracket
; stack = [ip+1, m, c]
; no other instruction depends on `m`
swap
pop
; stack = [ip+1, c]
; if c == '+': goto run_plus
dup
push 43
sub
jz run_plus
; stack = [ip+1, c]
; if c == '-': goto run_minus
dup
push 45
sub
jz run_minus
; stack = [ip+1, c]
; if c == '<': goto run_left
dup
push 60
sub
jz run_left
; stack = [ip+1, c]
; if c == '>': goto run_right
dup
push 62
sub
jz run_right
; stack = [ip+1, c]
; if c == ',': goto run_input
dup
push 44
sub
jz run_input
; stack = [ip+1, c]
; if c == '.': goto run_output
dup
push 46
sub
jz run_output
; stack = [ip+1, c]
; do nothing with `c`
jmp pop_and_jmp_run
run_open_bracket:
pop
; stack = [ip+1, m]
; if -heap_dp < 0: goto pop_and_jmp_run
push 0
get
get
push -1
mul
jn pop_and_jmp_run
; stack = [ip+1, m]
; set `m` as the new ip
swap
jmp pop_and_jmp_run
run_close_bracket:
pop
; stack = [ip+1, m]
; if heap[dp] == 0: goto run_close_bracket_nojmp
push 0
get
get
jz pop_and_jmp_run
; stack = [ip+1, m]
; set `m` as the new ip
swap
jmp pop_and_jmp_run
run_plus:
; stack = [ip+1, c]
; heap[dp] = (heap[dp] + 1) % 256
push 0
get
dup
get
push 1
add
push 256
mod
set
; stack = [ip+1, c]
; do nothing with `c`
jmp pop_and_jmp_run
run_minus:
; stack = [ip+1, c]
; heap[dp] = (heap[dp] + 255) % 256
push 0
get
dup
get
push 255
add
push 256
mod
set
; stack = [ip+1, c]
; do nothing with `c`
jmp pop_and_jmp_run
run_left:
; stack = [ip+1, c]
; if dp < 2*L + 3: goto pop_and_jmp_run
push 0
get
push 1
get
push 2
mul
push 3
add
sub
jn pop_and_jmp_run
; stack = [ip+1, c]
; dp = dp - 1
push 0
dup
get
push 1
sub
set
jmp pop_and_jmp_run
run_right:
; stack = [ip+1, c]
; dp = dp + 1
push 0
dup
get
push 1
add
set
; stack = [ip+1, c]
; do nothing with `c`
jmp pop_and_jmp_run
run_input:
; stack = [ip+1, c]
; read a character and write it to dp
push 0
get
ichr
; stack = [ip+1, c]
; do nothing with `c`
jmp pop_and_jmp_run
run_output:
; stack = [ip+1, c]
; write heap[dp] as a character
push 0
get
get
pchr
; stack = [ip+1, c]
; do nothing with `c`
pop_and_jmp_run:
pop
jmp run
end_run:
; stack = [ip]
; clean up the stack.
pop