-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonasm.asm
386 lines (310 loc) · 5.07 KB
/
jsonasm.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
SECTION .data
format: db "%s", 0x0a, 0
expext: db "expected '%c' at index %i but found '%c'", 0x0a, 0
succes: db "JSON is valid!", 0x0a, 0
lit_true: db "true", 0
lit_false: db "false", 0
lit_null: db "null", 0
SECTION .bss
buffer resb 1024
tmpbuf resb 1024
exp_chr resb 4
buflen equ 1024
index resb 4
SECTION .text
global main
extern printf
readfile:
push ebp
mov ebp, esp
sub esp, 4
mov eax, 5 ; syscall for open
mov ebx, [ebp+8] ; first argument is filename
mov ecx, 0 ; read only
int 0x80
mov [ebp-4], eax ; move file descriptor into local-var
cmp eax, 0
jl error
mov eax, 3 ; syscall for read
mov ebx, [ebp-4] ; file descriptor
mov ecx, tmpbuf ; buffer to fill
mov edx, buflen ; buffer size
int 0x80
mov eax, 6 ; syscall for close
mov ebx, [ebp-4] ; file descriptor
int 0x80
mov esp, ebp
pop ebp
ret
validate:
push ebp
mov ebp, esp
mov byte[index], 0
call object
mov byte[exp_chr], 0
mov ebx, [index]
cmp byte[buffer+ebx], 0
jne error
push succes
push format
call printf
mov esp, ebp
pop ebp
ret
object:
push ebp
mov ebp, esp
mov ebx, [index]
mov byte [exp_chr], '{'
cmp byte [buffer+ebx], '{'
jne error
inc dword[index]
mov ebx, [index]
cmp byte[buffer+ebx], '"'
jne .end
.item:
call string
mov ebx, [index]
mov byte[exp_chr], ':'
cmp byte[buffer+ebx], ':'
jne error
inc dword[index]
call value
mov ebx, [index]
cmp byte[buffer+ebx], ','
jne .end
inc dword[index]
jmp .item
.end:
mov ebx, [index]
mov byte[exp_chr], '}'
cmp byte[buffer+ebx], '}'
jne error
inc dword[index]
mov esp, ebp
pop ebp
ret
string:
push ebp
mov ebp, esp
mov ebx, [index]
mov byte[exp_chr], '"'
cmp byte[buffer+ebx], '"'
jne error
.loop:
inc dword[index]
mov ebx, [index]
mov byte[exp_chr], '"'
cmp byte[buffer+ebx], 0
je error
cmp byte[buffer+ebx], '"'
jne .loop
inc dword[index]
mov esp, ebp
pop ebp
ret
cmp_str:
push ebp
mov ebp, esp
mov ebx, [index]
mov ecx, [ebp+8]
mov eax, 0
.loop:
mov dh, byte[ecx+eax]
cmp dh, 0
je .end
cmp byte[buffer+ebx], dh
jne .noteq
inc eax
inc ebx
jmp .loop
.noteq:
mov eax, 0
jmp .end
.end:
mov esp, ebp
pop ebp
ret
num:
push ebp
mov ebp, esp
mov ecx, -1
mov byte[exp_chr], 'n'
mov ebx, [index]
cmp byte[buffer+ebx], '-'
je .loop
cmp byte[buffer+ebx], '0'
jl error
cmp byte[buffer+ebx], '9'
jg error
jmp .loop
.dot:
cmp ecx, 0
jge error
not ecx
jmp .loop
.loop:
inc dword[index]
mov ebx, [index]
cmp byte[buffer+ebx], '.'
je .dot
cmp byte[buffer+ebx], '0'
jl .end
cmp byte[buffer+ebx], '9'
jg .end
jmp .loop
.end:
mov esp, ebp
pop ebp
ret
array:
push ebp
mov ebp, esp
mov ebx, [index]
mov byte [exp_chr], '['
cmp byte [buffer+ebx], '['
jne error
inc dword[index]
mov ebx, [index]
cmp byte [buffer+ebx], ']'
je .end
.item:
call value
mov ebx, [index]
cmp byte[buffer+ebx], ','
jne .end
inc dword[index]
jmp .item
.end:
mov ebx, [index]
mov byte[exp_chr], ']'
cmp byte[buffer+ebx], ']'
jne error
inc dword[index]
mov esp, ebp
pop ebp
ret
literal:
push ebp
mov ebp, esp
mov byte[exp_chr], 'l'
push lit_true
call cmp_str
add esp, 4
cmp eax, 0
jg .end
push lit_false
call cmp_str
add esp, 4
cmp eax, 0
jg .end
push lit_null
call cmp_str
add esp, 4
cmp eax, 0
jle error
.end:
add [index], eax
mov esp, ebp
pop ebp
ret
value:
push ebp
mov ebp, esp
mov ebx, [index]
mov dword[exp_chr], 'v'
cmp byte[buffer+ebx], '"'
je .string
cmp byte[buffer+ebx], '{'
je .object
cmp byte[buffer+ebx], '['
je .array
cmp byte[buffer+ebx], '-'
je .num
cmp byte[buffer+ebx], '0'
jl .literal
cmp byte[buffer+ebx], '9'
jg .literal
jmp .num
.string:
call string
jmp .end
.object:
call object
jmp .end
.array:
call array
jmp .end
.num:
call num
jmp .end
.literal:
call literal
jmp .end
.end:
mov esp, ebp
pop ebp
ret
strip:
push ebp
mov ebp, esp
mov ebx, 0
mov ecx, 0
mov esi, -1
.loop:
cmp byte[tmpbuf+ebx], '"'
je .qoute
cmp esi, 0 ; within string
jge .copy
cmp byte[tmpbuf+ebx], ' '
je .incr
cmp byte[tmpbuf+ebx], 10 ; newline
je .incr
cmp byte[tmpbuf+ebx], 9 ; tab
je .incr
cmp byte[tmpbuf+ebx], 0 ; eof
je .end
jmp .copy
.qoute:
not esi
.copy:
mov edx, [tmpbuf+ebx]
mov [buffer+ecx], edx
inc ebx
inc ecx
jmp .loop
.incr:
inc ebx
jmp .loop
.end:
mov byte[buffer+ecx], 0
mov esp, ebp
pop ebp
ret
main:
push ebp
mov ebp, esp
mov esi, dword[ebp+12] ; address of argv
add esi, 4 ; get second argument
push dword[esi] ; push first argv as filename
call readfile
call strip ; strip whitespace from buffer
push buffer ; print result
push format
call printf
call validate
mov ebx, 0
mov eax, 0x1
int 0x80
error:
mov ebx, [index]
mov ecx, [buffer+ebx]
push ecx ; push current char
push ebx ; push index
mov ebx, [exp_chr]
push ebx ; push expected character
push expext ; format for error message
call printf
mov ebx, ecx
mov eax, 0x1
int 0x80