forked from Zaheer-A/brainfuck_interpreter_assembly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf3.s
208 lines (126 loc) · 2.9 KB
/
bf3.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
.data
ARRAY: .skip 30000
.global brainfuck
format_str: .asciz "We should be executing the following code:\n%s"
# Your brainfuck subroutine will receive one argument:
# a zero termianted string containing the code to execute.
brainfuck:
enter $0,$0
movq %rdi, %r10
movq %rdi, %rsi
movq $format_str, %rdi
call printf
movq $0, %rax
#initialize ARRAY
movq $0, %rcx
loop: movb $0, ARRAY(%rcx)
incq %rcx
cmpq $30000, %rcx
jl loop
# *********************************************INSERT CODE **************************************************************************
movq $0, %rdx #set datapointer to 0
movq $0, %r8 #set number of brackets to 0
movq $0, %rcx #set counter for loop/ iterate each char to 0
movb (%r10, %rcx, 1), %al #take first character of brainfuck code
main_loop: cmpb '+', %al
je plus
cmpb '-', %al
je minus
cmpb '>', %al
je pointer_up
cmpb '<', %al
je pointer_down
cmpb '[', %al
je loop_open
cmpb ']', %al
je loop_close
cmpb '.', %al
je print
cmpb ',', %al
je read
#*******************************
plus: incb ARRAY(%rdx)
jmp end_loop
#*************************************
minus: decb ARRAY(%rdx)
jmp end_loop
#*******************************
pointer_up: incq %rdx
jmp end_loop
#*******************************
pointer_down: decq %rdx
jmp end_loop
#********************
print: mov %al, %r9b
mov ARRAY(%rdx), %al
push %rax
call putchar
mov %r9b, %al
jmp end_loop
#************************
read: call getchar
mov %al, ARRAY(%rdx)
jmp end_loop
#*******************
loop_open: cmpb $0, ARRAY(%rdx)
je case1
jmp end_loop
case1: incq %rcx
movb (%r10, %rcx, 1), %al
while: cmpq $0, %r8
jg inside_loop
cmpb ']', %al
jne inside_loop
jmp end_loop
inside_loop: cmpb '[', %al
je ifcode
cmpb ']', %al
je elsecode
incq %rcx
movb (%r10, %rcx, 1), %al
jmp while
ifcode: incq %r8
incq %rcx
movb (%r10, %rcx, 1), %al
jmp while
elsecode: decq %r8
incq %rcx
movb (%r10, %rcx, 1), %al
jmp while
#*****************
loop_close: cmpb $0, ARRAY(%rdx)
jne case2
jmp end_loop
case2: decq %rcx
movb (%r10, %rcx, 1), %al
while2: cmpq $0, %r8
jg inside_loop2
cmpb '[', %al
jne inside_loop2
jmp end_loop_close
inside_loop2: cmpb ']', %al
je ifcode2
cmpb '[', %al
je elsecode2
decq %rcx
movb (%r10, %rcx, 1), %al
jmp while2
ifcode2: incq %r8
decq %rcx
movb (%r10, %rcx, 1), %al
jmp while2
elsecode2: decq %r8
decq %rcx
movb (%r10, %rcx, 1), %al
jmp while2
end_loop_close: decq %rcx
movb (%r10, %rcx, 1), %al
jmp end_loop
#*********************
end_loop: incq %rcx #increment counter
movb (%r10, %rcx, 1), %al # add %rcx to %rsi and multiply by one to get the next character from the string.
cmpb $0x00, %al
jne main_loop
jmp endsub
endsub: leave
ret