-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.asm
279 lines (265 loc) · 6.09 KB
/
interp.asm
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
;The aim of this is to create a brainfuck intepreter
;status - trying for speed increases
extern printf
extern malloc
extern putchar
;due to different calling convention, the registers that we use depend on the OS
%ifdef WIN64
%define vd rcx
%define main WinMain ;on windows main is not called main
%endif
%ifdef LIN64
%define vd rdi
%endif
global main
;x64 calling convention
;ints / pointers - RCX RDX R8 R9
; - rdi is first on linux
; initialized data is put in the .data segment - it may be true that these are not modifiable
segment .data
;loopless hello world
;program db "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++++++++++++++++.+++.------.--------.-------------------------------------------------------------------.",0
;hello world w/ loops
;program db "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.",0
;factorial
program db ">++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]<[>+<-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]<<<<[<[>+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<[<[>+>+<<-]>.<<<<<]>.>>>>]", 0
debug db "dd",0
intf db "%i" ,10,0
charf db "%c",0
hellostr db "loop", 10 ,0
incdp db ">"
decdp db "<"
incbyte db "+"
decbyte db "-"
output db "."
input db ","
whilestart db "["
whileend db "]"
array dq 0
jtab dq 0 ;stored in r9
plength dq 0
segment .text
main:
push rbp
mov rbp,rsp
%ifdef WIN64 ;not sure precisely what this does, got it by doing an objdump of a c exe - consider it magic
sub rsp,0x20
%elifdef LIN64
sub rsp,0x10
%endif
;allocate memory for memory
mov vd, 0x5000000
call malloc
mov [array], rax
jmp maparr
maparr:
mov rax, program
dec rax
.ib:
mov byte [rax], 0
jmp .loop
.db:
mov byte [rax], 1
jmp .loop
.out:
mov byte [rax], 6
jmp .loop
.ddp:
mov byte [rax], 3
jmp .loop
.idp:
mov byte [rax], 2
jmp .loop
.inp:
mov byte [rax], 7
jmp .loop
.ws:
mov byte [rax], 4
jmp .loop
.we:
mov byte [rax], 5
jmp .loop
.loop:
inc rax
mov bl, [rax]
cmp bl, [incbyte]
je .ib
cmp bl, [decbyte]
je .db
cmp bl, [output]
je .out
cmp bl, [decdp]
je .ddp
cmp bl, [incdp]
je .idp
cmp bl, [input]
je .inp
cmp bl, [whilestart]
je .ws
cmp bl, [whileend]
je .we
cmp bl, 0
jne .loop
mov byte [rax], 8
jmp Zeromem
Zeromem:
mov rcx, 0 ;0 value to store in memory
mov rax,40008 ;currently 5k memory cells
add rax, [array] ;rax is 'end' of memory
mov rbx, 0
add rbx, [array];rbx is 'start' of memory
.loop_start:
sub rax,8
mov [rax], rcx
cmp rax, rbx
je Allocjmptab
jmp .loop_start
Allocjmptab:
;first calculate prog length
mov rax, program
dec rax
xor rcx,rcx ;use rcx to hold prog length
.loop_start:
inc rax
inc rcx
cmp byte [rax],8
je .loop_done
.loop_done:
mov rcx, [plength]
;now allocate memory
mov vd, rcx
sal vd,3 ;jtab has to store ints, so is 8 times bigger than the program, which is bytes
call malloc
mov[jtab], rax
jmp Buildjmptab
Buildjmptab:
;Variables I need to store
;I use the actual stack as the stack here - why not + frees up registers
;rcx - position through program
mov rcx, program
dec rcx ;for loop to be easier
jmp .loop_start
.whilestart:
push rcx
jmp .loop_start
.whileend:
pop rax ;rax is v
push rax ; use this again later
sub rax, program ;rax is now count through program of original start tax
;now I need to calculate the position to put the address
mov r9, [jtab]
mov [r9 + rax*8], rcx
;explaination -
;rax = position through program
;*8 - jump table is ints, program is bytes
;jtab - need to add in base address
;now do program.[curpointer] <- end(v)
mov rax, rcx ;rax is now cur loc
sub rax, program; rax now dist to current instruction
pop rdx
mov [r9 + rax*8],rdx
jmp .loop_start
.loop_start:
inc rcx
mov al, [rcx]
mov dl, 4 ;whilestart
cmp dl,al
je .whilestart
mov dl, 5 ;whileend
cmp dl,al
je .whileend
mov dl, 8 ;move 8 - EOF
cmp dl,al
jne .loop_start ;if not end, keep going
jmp initptrs ;now go to main loop
initptrs:
mov rdx, program
mov al, [rdx]
mov rbx, [array]
mov vd, [rbx]
jmp decode
;routines for different opcodes
incdpC:
;decode at start should be faster - more time to get next opcode - but didn't actually make a difference
inc rdx
mov al, [rdx]
mov [rbx], vd
add rbx, 8
mov vd, [rbx]
jmp decode
decdpC:
inc rdx
mov al, [rdx]
mov [rbx], vd
sub rbx, 8
mov vd, [rbx]
jmp decode
decbyteC:
inc rdx
mov al, [rdx]
dec vd
jmp decode
incbyteC:
inc rdx
mov al, [rdx]
inc vd
jmp decode
outputC:
;works correctly in both oses - not sure if extra stack space necersarry, but keep it anyway
push rbx
push vd
push r8
push r9
push rdx
call putchar
pop rdx
pop r9
pop r8
pop vd
pop rbx
inc rdx
mov al, [rdx]
jmp decode
whilestartC:
;check if memory.[pointer] = 0
cmp vd, 0
jne .nojmp
;now we need to get the new value for instrpointer
sub rdx, program
mov rdx, [rdx*8+r9]
.nojmp: ;fall through
inc rdx
mov al, [rdx]
jmp decode
whileendC:
cmp vd, 0 ;essentially same as above, but jump test reversed
je .nojmp
sub rdx, program
mov rdx, [rdx*8+r9]
.nojmp:
inc rdx
mov al, [rdx]
jmp decode
;Instruction decode loop
decode:
and al,al
je incbyteC
dec al
je decbyteC
dec al
je incdpC
dec al
je decdpC
dec al
je whilestartC
dec al
je whileendC
dec al
je outputC
sub al, 2
je exit
;EXIT HERE
exit:
xor rax,rax ;return 0
leave
ret