-
Notifications
You must be signed in to change notification settings - Fork 0
/
sscanf.asm
317 lines (295 loc) · 5.82 KB
/
sscanf.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
section .text
global _sscanf_asm
_sscanf_asm:
push esi
push ebx
push ecx
push edx
mov [ShiftResource], dword 0
mov [ShiftFormat], dword 0
mov [ShiftDestination], dword 28
mov [CompliteCount], dword 0
mov esi, [esp + 24] ;Format string
mov ebx, [esp + 20] ;Resource string
_find_first_format_char:
mov edx, [ShiftFormat]
mov al, [esi + edx] ;if Format[i] == ' ' -> inc i
cmp al, '%' ;else if Format[i] == '%' -> inc i
jz _FoundPercent ;read F second
cmp al, ' ' ;read F first
jz _FoundSpace
cmp al, 9h ;horiz tab \r
jz _FoundSpace
cmp al, Ah ; \n
jz _FoundSpace
cmp al, Dh ; \r
jz _FoundSpace
jmp _ReturnCompliteCount
_FoundSpace:
add [ShiftFormat], dword 1
jmp _find_first_format_char
_FoundPercent:
add [ShiftFormat], dword 1
jmp _read_second_format_char
_read_second_format_char:
mov edx, [ShiftFormat]
mov al, [esi + edx] ;if Format[i] == 's' -> inc i
add [ShiftFormat], dword 1 ;if Format[i] == ' ' -> read address next container from stack
cmp al, 's'
jz _s_func
cmp al, 'S'
jz _s_func
cmp al, 'i'
jz _i_func
cmp al, 'I'
jz _i_func
cmp al, 'd'
jz _i_func
cmp al, 'D'
jz _i_func
cmp al, 'X'
jz _x_func
cmp al, 'x'
jz _x_func
jmp _ReturnCompliteCount ;else GOTO _ReturnCompliteCount
_s_func:
mov edx, [ShiftDestination]
mov eax, [esp + edx]
mov [eax], dword 0
mov [ShiftStringR], dword 0
_find_first_char:
mov edx, [ShiftResource]
mov cl, [ebx + edx]
cmp cl, ' '
jz _pre_find_first_char
cmp cl, 9h
jz _pre_find_first_char
cmp cl, 0Ah
jz _pre_find_first_char
cmp cl, 0Dh
jz _pre_find_first_char
cmp cl, 0
jz _ReturnCompliteCount
jmp _read_next_chr
_pre_find_first_char:
add [ShiftResource], dword 1
jmp _find_first_char
_read_next_chr:
mov edx, [ShiftStringR]
mov [eax + edx], cl
add [ShiftStringR], dword 1
add [ShiftResource], dword 1
mov edx, [ShiftResource]
mov cl, [ebx + edx]
cmp cl, ' '
jz _ReadStrComplite
cmp cl, 9h
jz _ReadStrComplite
cmp cl, 0Ah
jz _ReadStrComplite
cmp cl, 0Dh
jz _ReadStrComplite
cmp cl, 0
jz _ReadStrComplite_end
jmp _read_next_chr
_ReadStrComplite:
mov edx, [ShiftStringR]
mov [eax + edx], byte 0
add [CompliteCount], dword 1
add [ShiftDestination], dword 4
jmp _find_first_format_char
_ReadStrComplite_end:
mov edx, [ShiftStringR]
mov [eax + edx], byte 0
add [CompliteCount], dword 1
jmp _ReturnCompliteCount
_i_func:
mov [NegVal], byte 0
mov eax, [ShiftDestination]
mov edx, [esp + eax]
mov [edx], dword 0
mov [ShiftStringR], dword 0
_find_first_int:
mov eax, [ShiftResource]
xor ecx, ecx
mov cl, byte [ebx + eax]
cmp cl, ' '
jz _pre_find_first_int
cmp cl, 9h
jz _pre_find_first_int
cmp cl, 0Ah
jz _pre_find_first_int
cmp cl, 0Dh
jz _pre_find_first_int
cmp cl, 0
jz _ReturnCompliteCount
cmp cl, '-'
jz _findNeg
jmp _read_next_int
_pre_find_first_int:
add [ShiftResource], dword 1
jmp _find_first_int
_findNeg:
test byte [NegVal], 0Fh
jnz _ReturnCompliteCount
mov byte [NegVal], 1
jmp _pre_find_first_int
_read_next_int:
jmp _save_number
_read_next_int_resume:
add [ShiftStringR], dword 1
add [ShiftResource], dword 1
mov eax, [ShiftResource]
xor ecx, ecx
mov cl, [ebx + eax]
jmp _read_next_int
_save_number:
cmp cl, 48
js _NAN_
cmp cl, 58
jns _NAN_
push edx
push ebx
xor ebx, ebx
mov ebx, dword [edx]
xor edx, edx
xor eax, eax
mov eax, 10
mul ebx
sub ecx, 48
add eax, ecx
pop ebx
pop edx
mov dword [edx], eax
jmp _read_next_int_resume
_NAN_:
test byte [NegVal], 0Fh
jnz _NegInt
test dword [ShiftStringR], 0FFFFFFFFh
jnz _ReadIntComplite
jmp _ReturnCompliteCount
_NegInt:
mov eax, dword [edx]
neg eax
mov dword [edx], eax
test dword [ShiftStringR], 0FFFFFFFFh
jnz _ReadIntComplite
jmp _ReturnCompliteCount
_ReadIntComplite:
add [CompliteCount], dword 1
add [ShiftDestination], dword 4
jmp _find_first_format_char
_x_func:
mov [NegVal], byte 0
mov eax, [ShiftDestination]
mov edx, [esp + eax]
mov [edx], dword 0
mov [ShiftStringR], dword 0
_find_first_hex:
mov eax, [ShiftResource]
xor ecx, ecx
mov cl, byte [ebx + eax]
cmp cl, ' '
jz _pre_find_first_hex
cmp cl, 9h
jz _pre_find_first_hex
cmp cl, 0Ah
jz _pre_find_first_hex
cmp cl, 0Dh
jz _pre_find_first_hex
cmp cl, 0
jz _ReturnCompliteCount
cmp cl, '-'
jz _findNegHex
jmp _read_next_hex
_pre_find_first_hex:
add [ShiftResource], dword 1
jmp _find_first_hex
_findNegHex:
test byte [NegVal], 0Fh
jnz _ReturnCompliteCount
mov byte [NegVal], 1
jmp _pre_find_first_hex
_read_next_hex:
jmp _test_number_hex
_read_next_hex_resume:
add [ShiftStringR], dword 1
add [ShiftResource], dword 1
mov eax, [ShiftResource]
xor ecx, ecx
mov cl, [ebx + eax]
jmp _read_next_hex
_test_number_hex:
cmp cl, 103
jns _NAN_hex
cmp cl, 48
js _NAN_hex
cmp cl, 58
jns _test2_hex
sub cl, '0'
jmp _save_number_hex
_test2_hex:
cmp cl, 65
js _NAN_hex
cmp cl, 71
jns _test3_hex
js _hex_upper_to_int
_test3_hex:
cmp cl, 97
js _NAN_hex
jns _hex_lower_to_int
_hex_upper_to_int:
sub cl, 'A'
add cl, 10
jmp _save_number_hex
_hex_lower_to_int:
sub cl, 'a'
add cl, 10
jmp _save_number_hex
_save_number_hex:
push edx
push ebx
xor ebx, ebx
mov ebx, dword [edx]
xor edx, edx
xor eax, eax
mov eax, 16
mul ebx
;sub ecx, 48
add eax, ecx
pop ebx
pop edx
mov dword [edx], eax
jmp _read_next_hex_resume
_NAN_hex:
test byte [NegVal], 0Fh
jnz _NegHex
test dword [ShiftStringR], 0FFFFFFFFh
jnz _ReadHexComplite
jmp _ReturnCompliteCount
_NegHex:
mov eax, dword [edx]
neg eax
mov dword [edx], eax
test dword [ShiftStringR], 0FFFFFFFFh
jnz _ReadHexComplite
jmp _ReturnCompliteCount
_ReadHexComplite:
add [CompliteCount], dword 1
add [ShiftDestination], dword 4
jmp _find_first_format_char
_ReturnCompliteCount:
pop edx
pop ecx
pop ebx
pop esi
mov eax, [CompliteCount];
ret
end
section .bss
ShiftResource resb 4
ShiftFormat resb 4
ShiftDestination resb 4
CompliteCount resb 4
ShiftStringR resb 4
NegVal resb 1