-
Notifications
You must be signed in to change notification settings - Fork 2
/
asmbf-rv64.S
257 lines (231 loc) · 4.16 KB
/
asmbf-rv64.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
/*
* asmbf-rv64
* The rv64 port of asmbf:
* a brainfuck interpreter written in pure rv64 assembly.
* Date: 2023.09.27
* By MIT License.
* Copyright (c) 2023 Yao Zi.
*/
#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
#define FILENO_STDIN 0
#define FILENO_STDOUT 1
#define AT_FDCWD -100
#define sys_exit 93
#define sys_read 63
#define sys_write 64
#define sys_close 57
#define sys_openat 56
#define sys_lseek 62
#define sys_mmap 222
#define sys_munmap 215
.global _start
.text
_start:
/*
* Open the source file and load it into memory
*/
ld a0, 0(sp)
addi a0, a0, -1
bnez a0, loadFile
/* argc == 1 */
la a0, noEnoughArg
call print
li a0, -1
j exitFinal
loadFile:
li a0, AT_FDCWD
ld a1, 16(sp)
li a2, O_RDONLY
mv a3, zero
li a7, sys_openat
ecall
blt a0, zero, errorLoadFile
mv s6, a0 // a0, s6: fd
/* Get size */
mv a1, zero
li a2, SEEK_END
li a7, sys_lseek
ecall
blt a0, zero, errorLoadFile
mv s5, a0 // s5, a0 hold size
mv a0, s6
mv a1, zero
li a2, SEEK_SET
li a7, sys_lseek
ecall // Rewind
blt a0, zero, errorLoadFile
/* Do mapping */
mv a0, zero
li a1, BRAINFUCK_MEM
add a1, a1, s5
addi a1, a1, 1
li a2, (PROT_READ | PROT_WRITE)
li a3, (MAP_ANONYMOUS | MAP_SHARED)
mv a4, zero
li s0, -1
mv a7, s0
li a7, sys_mmap
ecall
beq a0, s0, errorLoadFile
mv s0, a0
/* Load the file into memory */
mv a0, s6
mv a1, s0
mv a2, s5
li a7, sys_read
ecall
bne a0, s5, errorLoadFile
add t0, s0, s5
sb zero, 1(t0)
mv a0, s6
li a7, sys_close
ecall
j initInterpreter
errorLoadFile:
la a0, cannotLoadFile
call print
li a0, -1
j exitFinal
initInterpreter:
/* Clear the data section */
add t0, s0, s5
mv s2, t0
li t1, BRAINFUCK_MEM
.clearDataSection:
sb zero, 1(t0)
addi t1, t1, -1
bnez t1, .clearDataSection
/* s1: pc s2: data s3: lookup table */
mv s1, s0
addi s2, s2, 1
la s3, instTable
.interpreterLoop:
mv t0, zero
lb t0, 0(s1)
slli t0, t0, 3
add t0, t0, s3
ld t0, 0(t0)
addi s1, s1, 1
jalr zero, t0
instPrev:
addi s2, s2, -1
j .interpreterLoop
instNext:
addi s2, s2, 1
j .interpreterLoop
instWrite:
li a0, FILENO_STDOUT
mv a1, s2
li a2, 1
li a7, sys_write
ecall
j .interpreterLoop
instRead:
li a0, FILENO_STDIN
mv a1, s2
li a2, 1
li a7, sys_read
ecall
j .interpreterLoop
instInc:
lb t0, 0(s2)
addi t0, t0, 1
sb t0, 0(s2)
j .interpreterLoop
instDec:
lb t0, 0(s2)
addi t0, t0, -1
sb t0, 0(s2)
j .interpreterLoop
instLoopBegin:
lb t0, 0(s2)
bnez t0, .saveContext
li t1, 1 // t1 is a counter
.findNext:
lb t0, 0(s1)
li t2, '['
beq t0, t2, .increaseNest
li t2, '];
beq t0, t2, .decreaseNest
j .nextLoop
.increaseNest:
addi t1, t1, 1
j .nextLoop
.decreaseNest:
addi t1, t1, -1
j .nextLoop
.nextLoop:
addi s1, s1, 1
bnez t1, .findNext
j .interpreterLoop
.saveContext:
addi sp, sp, -8
sd s1, 0(sp)
j .interpreterLoop
instLoopEnd:
lb t0, 0(s2)
beqz t0, .jumpOutOfLoop
ld s1, 0(sp)
j .interpreterLoop
.jumpOutOfLoop:
addi sp, sp, 8
j .interpreterLoop
instEnd:
exit:
mv a0, s0
mv a1, s5
li a7, sys_munmap
ecall
exitFinal:
li a7, sys_exit
ecall
print:
mv a2, zero
mv t2, a0
.repeatLen:
lb t0, (t2)
beqz t0, .doWrite
addi t2, t2, 1
addi a2, a2, 1
j .repeatLen
/* a0 holds the address of the string, and t2 holds the its length */
.doWrite:
mv a1, a0
li a0, FILENO_STDOUT
li a7, sys_write
ecall
ret
.data
noEnoughArg: .asciz "need file path\n"
cannotLoadFile: .asciz "cannot load 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