-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter.asm
473 lines (394 loc) · 5.29 KB
/
iter.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
section .data
@formatin: db "%d", 0
@formatout: db "%d", 10, 0
section .text
global main
extern printf
extern scanf
extern malloc
extern free
extern memset
extern memcpy
extern memcmp
; avx2
%macro @avx2_add 0
pop eax
vmovdqu ymm1, [eax]
vpaddd ymm0, ymm0, ymm1
%endmacro
%macro @avx2_sub 0
pop eax
vmovdqu ymm1, [eax]
vpsubd ymm0, ymm0, ymm1
%endmacro
%macro @avx2_mul 0
pop eax
vmovdqu ymm1, [eax]
vpmulld ymm0, ymm0, ymm1
%endmacro
%macro @avx2_xor 0
pop eax
vmovdqu ymm1, [eax]
vpxor ymm0, ymm0, ymm1
%endmacro
%macro @avx2_and 0
pop eax
vmovdqu ymm1, [eax]
vpand ymm0, ymm0, ymm1
%endmacro
%macro @avx2_or 0
pop eax
vmovdqu ymm1, [eax]
vpor ymm0, ymm0, ymm1
%endmacro
%macro @avx2_min 0
pop eax
vmovdqu ymm1, [eax]
vpminsd ymm0, ymm0, ymm1
%endmacro
%macro @avx2_max 0
pop eax
vmovdqu ymm1, [eax]
vpmaxsd ymm0, ymm0, ymm1
%endmacro
%macro @avx2_cmpeq 0
pop eax
vmovdqu ymm1, [eax]
vpcmpeqd ymm0, ymm0, ymm1
%endmacro
%macro @avx2_cmpgt 0
pop eax
vmovdqu ymm1, [eax]
vpcmpgtd ymm0, ymm0, ymm1
%endmacro
%macro @avx2_load 0
pop eax
vmovdqu ymm0, [eax]
%endmacro
%macro @avx2_store 0
pop eax
vmovdqu [eax], ymm0
%endmacro
%macro @avx2_set1 0
pop eax
mov dword [@AVX2], eax
mov dword [@AVX2 + 4], eax
mov dword [@AVX2 + 8], eax
mov dword [@AVX2 + 12], eax
mov dword [@AVX2 + 16], eax
mov dword [@AVX2 + 20], eax
mov dword [@AVX2 + 24], eax
mov dword [@AVX2 + 28], eax
vmovdqu ymm0, [@AVX2]
%endmacro
; system
%macro @start 0
mov ebp, esp
mov esi, @MEM40
%endmacro
%macro @exit 0
mov esp, ebp
xor eax, eax
ret
%endmacro
; get
%macro @num 1
push %1
%endmacro
%macro @getvar 1
push dword [%1]
%endmacro
%macro @getaddr 1
push %1
%endmacro
%macro @getconst 1
push %1
%endmacro
%macro @getptr 0
pop eax
push dword [eax]
%endmacro
%macro @getarr 1
pop eax
mov ecx, dword [%1]
push dword [ecx + eax * 4]
%endmacro
%macro @read 0
sub esp, 4
mov [esp], esp
push dword @formatin
call scanf
add esp, 4
%endmacro
; set
%macro @setvar 1
pop dword [%1]
%endmacro
%macro @setlvar 1
pop dword [%1]
%endmacro
%macro @lsetptr 0
pop ebx
pop eax
mov dword [eax], ebx
%endmacro
%macro @rsetptr 0
pop ebx
pop eax
mov dword [ebx], eax
%endmacro
%macro @lsetarr 1
pop ebx
pop eax
mov ecx, dword [%1]
mov [ecx + eax * 4], ebx
%endmacro
%macro @rsetarr 1
pop ebx
pop eax
mov ecx, dword [%1]
mov [ecx + ebx * 4], eax
%endmacro
%macro @swapptr 0
pop ebx
pop eax
mov ecx, dword [ebx]
mov edx, dword [eax]
mov dword [ebx], edx
mov dword [eax], ecx
%endmacro
%macro @heap 0
shl dword [esp], 2
call malloc
add esp, 4
push eax
%endmacro
%macro @heaparr 1
shl dword [esp], 2
call malloc
add esp, 4
mov dword [%1], eax
%endmacro
%macro @free 0
call free
add esp, 4
%endmacro
%macro @mset 0
shl dword [esp + 8], 2
call memset
add esp, 12
%endmacro
%macro @mcpy 0
shl dword [esp + 8], 2
call memcpy
add esp, 12
%endmacro
%macro @mcmp 0
shl dword [esp + 8], 2
call memcmp
add esp, 12
push eax
%endmacro
; (a)
%macro @inc 0
inc dword [esp]
%endmacro
%macro @dec 0
dec dword [esp]
%endmacro
%macro @not 0
not dword [esp]
%endmacro
%macro @mul2 0
shl dword [esp], 1
%endmacro
%macro @div2 0
sar dword [esp], 1
%endmacro
%macro @incptr 0
add dword [esp], 4
%endmacro
%macro @decptr 0
sub dword [esp], 4
%endmacro
%macro @bcnt 0
pop eax
popcnt ebx, eax
push ebx
%endmacro
%macro @bclz 0
pop ebx
bsr eax, ebx
xor eax, 31
push eax
%endmacro
%macro @bctz 0
pop ebx
bsf eax, ebx
push eax
%endmacro
; (a, b)
%macro @add 0
pop ebx
add [esp], ebx
%endmacro
%macro @sub 0
pop ebx
sub [esp], ebx
%endmacro
%macro @mul 0
pop ebx
pop eax
imul ebx
push eax
%endmacro
; not working for negative number
%macro @div 0
xor edx, edx
pop ebx
pop eax
idiv ebx
push eax
%endmacro
%macro @mod 0
xor edx, edx
pop ebx
pop eax
idiv ebx
push edx
%endmacro
%macro @xor 0
pop ebx
xor [esp], ebx
%endmacro
%macro @and 0
pop ebx
and [esp], ebx
%endmacro
%macro @or 0
pop ebx
or [esp], ebx
%endmacro
%macro @shl 0
pop ecx
shl dword [esp], cl
%endmacro
%macro @shr 0
pop ecx
shr dword [esp], cl
%endmacro
%macro @rarrow 0
pop eax
shl eax, 2
add dword [esp], eax
%endmacro
%macro @larrow 0
pop eax
shl eax, 2
sub dword [esp], eax
%endmacro
; (a, b, c)
%macro @muldiv 0
pop ecx
pop ebx
pop eax
imul ebx
idiv ecx
push eax
%endmacro
%macro @mulmod 0
pop ecx
pop ebx
pop eax
imul ebx
idiv ecx
push edx
%endmacro
; stack
%macro @copy 0
push dword [esp]
%endmacro
%macro @prev 0
push dword [esp + 4]
%endmacro
%macro @prew 0
push dword[esp + 8]
%endmacro
%macro @swap 0
pop ebx
pop eax
push ebx
push eax
%endmacro
%macro @push 0
push edi
%endmacro
%macro @save 0
mov edi, dword [esp]
%endmacro
%macro @pick 0
pop edi
%endmacro
%macro @drop 0
add esp, 4
%endmacro
%macro @kill 0
pop eax
mov dword [esp], eax
%endmacro
%macro @post 0
push dword @formatout
call printf
add esp, 8
%endmacro
%macro @dump 0
pop eax
pop ecx
shl ecx, 2
mov ebx, esp
push ecx
push ebx
push eax
call memcpy
add esp, 8
pop ecx
add esp, ecx
%endmacro
%macro @minc 0
pop eax
inc dword [eax]
%endmacro
%macro @mdec 0
pop eax
dec dword [eax]
%endmacro
; conditions
%macro @cmp 0
pop ebx
pop eax
cmp eax, ebx
%endmacro
%macro @cmp0 0
pop eax
cmp eax, 0
%endmacro
; mem stack
%macro @meminit 0
pop dword [esi]
add esi, 4
%endmacro
%macro @memgetvar 1
mov eax, dword [%1]
mov [esi], eax
add esi, 4
%endmacro
%macro @memsetvar 1
sub esi, 4
mov eax, dword [esi]
mov dword [%1], eax
%endmacro
%macro @memexit 0
sub esi, 4
push dword [esi]
ret
%endmacro