-
Notifications
You must be signed in to change notification settings - Fork 2
/
asmbf.S
300 lines (274 loc) · 4.81 KB
/
asmbf.S
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
/*
* asmbf
* A brainfuck interpreter written in pure x86-64 assembly (AT&T style)
* Date:2022.12.16
* By MIT License.
* Copyright (c) 2022 Ziyao.
*/
#define BRAINFUCK_MEM (1024 * 1024 )
#define O_RDONLY 00
#define SEEK_SET 0
#define SEEK_END 2
#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define MAP_SHARED 0x01
#define MAP_ANONYMOUS 0x20
.global _start
.text
_start:
/*
* Open the source file and load it into memory
*/
movq (%rsp), %rax
decq %rax
jnz loadFile
/* argc == 1 */
movq $noEnoughArg, %rsi
callq print
movq $-1, %rdi // exit
jmp exitFinal
loadFile:
/* Open */
xorq %r12, %r12 // %r12 is 0
movq 16(%rsp), %rdi
movq $O_RDONLY, %rsi
xorq %rdx, %rdx
movq $2, %rax // sys_open
syscall
cmpq %r12, %rax
jl errorLoadFile
movq %rax, %r13 // %r13 holding fd
/* Get size */
movq %rax, %rdi
xorq %rsi, %rsi
movq $SEEK_END, %rdx // SEEK_END
movq $8, %rax // sys_lseek
syscall
cmpq %r12, %rax
jl errorLoadFile
movq %rax, %r14 // %r14 holding size
movq %rax, %rcx
movq $8, %rax
movq $SEEK_SET, %rdx // SEEK_SET
syscall
/* Do mapping */
xorq %rdi, %rdi
movq %r14, %rsi
addq $BRAINFUCK_MEM, %rsi // Allocate only once
incq %rsi // The '\0'
pushq %rsi
movq $(PROT_READ | PROT_WRITE),%rdx
movq $(MAP_ANONYMOUS | MAP_SHARED),%r10
xorq %r9, %r9
movq %r9, %r8
decq %r8
movq $9, %rax // sys_mmap
syscall
decq %r12 // %r12 is -1 now
cmpq %r12, %rax
je errorLoadFile
movq %rax, %r15
/* Load into memory */
movq %r13, %rdi
movq %rax, %rsi
movq %r14, %rdx
movq $0, %rax
syscall
cmpq %rax, %r14
jne errorLoadFile
movb %al, 1(%r15,%r14) // %rax is 0
/* Close file */
movq $3, %rax
syscall
jmp initInterpreter
errorLoadFile:
movq $cannotLoadFile,%rsi
callq print
movq $-1, %rdi
jmp exitFinal
/*
* %rbx Base address of the look-up table for instructions
* %r12 PC
* %r13 Data Pointer
* Loop nesting is recorded in the stack
*/
initInterpreter:
/* Clear the data section */
leaq 1(%r15,%r14), %rdi
movq %rdi, %r13
xorq %rax, %rax
movq $BRAINFUCK_MEM, %rcx
repnz stosb
/* Set registers */
movq %r15, %r12 // PC
movq $instTable, %rbx
.interpreterLoop:
xorq %rax, %rax
movb (%r12), %al
incq %r12
jmp *(%rbx,%rax,8)
instPrev:
decq %r13
jmp .interpreterLoop
instNext:
incq %r13
jmp .interpreterLoop
instWrite:
movq %r13, %rsi
movq $1, %rax
movq %rax, %rdi
movq %rax, %rdx
syscall
jmp .interpreterLoop
instRead:
movq %r13, %rsi
movq $0, %rax
movq %rax, %rdi
movq $1, %rdx
syscall
jmp .interpreterLoop
instInc:
movb (%r13), %al
incb %al
movb %al, (%r13)
jmp .interpreterLoop
instDec:
movb (%r13), %al
decb %al
movb %al, (%r13)
jmp .interpreterLoop
instLoopBegin:
xorq %rax, %rax
cmpb (%r13), %al
jne .saveContext
/* Get to the end */
movq $1, %rcx
movq %rax, %rdx
.findNext:
movb (%r12), %al
cmpb $'[', %al
je .increaseNest
cmpb $']', %al
je .decreaseNest
jmp .nextLoop
.increaseNest:
incq %rcx
jmp .nextLoop
.decreaseNest:
decq %rcx
jmp .nextLoop
.nextLoop:
incq %r12
xorq %rax, %rax
cmpq %rcx, %rdx
jne .findNext
jmp .interpreterLoop
.saveContext:
pushq %r12
jmp .interpreterLoop
instLoopEnd:
xorq %rax, %rax
cmpb (%r13), %al
je .jumpOutOfLoop
movq (%rsp), %r12
jmp .interpreterLoop
.jumpOutOfLoop:
addq $8, %rsp
jmp .interpreterLoop
instEnd:
/*
* Exit the program
*/
exit:
/* Exit gently */
movq %r15, %rdi
popq %rsi
movq $11, %rax
syscall
exitFinal:
xorq %rdi, %rdi
movq $60, %rax
syscall
/*
* Print to the stdout (assuming its fd is 1)
* Input:
* %rsi Address of the message
*/
print:
pushq %rcx
pushq %rdx
callq strlen
movq $1, %rdx
callq write
popq %rdx
popq %rcx
ret
/*
* Write data to a file
* Input:
* %rsi Address of the buffer
* %rcx Size
* %rdx File descriptor
* Output:
* %rcx Remaining size
*/
write:
pushq %rax
pushq %rdi
movq %rdx, %rdi
movq %rcx, %rdx
movq $1, %rax
syscall
sub %rax, %rcx
popq %rdi
popq %rax
ret
/*
* Get the length of a string
* Input:
* %rsi Address of the string
* output:
* %rcx Length
* Kept:
* %rsi
*/
strlen:
pushq %rax
pushq %rdi
movq %rsi, %rdi
xor %rax, %rax
xor %rcx, %rcx
not %rcx
repnz scasb
neg %rcx
popq %rdi
popq %rax
dec %rcx
ret
.data
noEnoughArg: .asciz "Need source file path\n"
cannotLoadFile: .asciz "Cannot load source file\n"
instTable:
.quad instEnd
.rept 42
.quad .interpreterLoop
.endr
.quad instInc
.quad instRead
.quad instDec
.quad instWrite
.rept 13
.quad .interpreterLoop
.endr
.quad instPrev
.quad .interpreterLoop
.quad instNext
.rept 28
.quad .interpreterLoop
.endr
.quad instLoopBegin
.quad .interpreterLoop
.quad instLoopEnd
.rept 162
.quad .interpreterLoop
.endr