-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick.asm
400 lines (347 loc) · 6.42 KB
/
quick.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
; asmsyntax=nasm
;***********************************************************************
;* An implementation of quicksort in nasm *
;***********************************************************************
section .data
global sort
global _sort
global partition
global less
global exch
global uniform
global shuffle
extern time
extern printf
extern srand
extern rand
section .bss
section .rodata
flagline:
db "flag in in asm", 10, 0
printnum:
db "%d", 10, 0
invalidn:
db "please give a positive value n as argument for uniformi(int n)", 10, 0
section .text
;************************************************************************
;* void sort(void **a, int len, int (*cmp)(void *, void *)) *
;************************************************************************
%define a [ebp+8]
%define len [ebp+12]
%define comp [ebp+16]
sort:
push ebp
mov ebp, esp
mov eax, len
push eax
mov eax, a
push eax
call shuffle
add esp, 8
mov eax, comp
push eax
mov eax, len
lea ebx, [eax-1]
push ebx
mov eax, 0
push eax
mov eax, a
push eax
call _sort
mov esp, ebp
pop ebp
ret
;************************************************************************
; void _sort(void **a, int lo, int hi, int(*cmp)(void *, void *)) *
;************************************************************************
%define a [ebp+8]
%define lo [ebp+12]
%define hi [ebp+16]
%define comp [ebp+20]
%define j [ebp-4]
_sort:
push ebp
mov ebp, esp
sub esp, 4
mov eax, hi
mov ebx, lo
cmp eax, ebx
jle .end
mov eax, comp
push eax
mov eax, hi
push eax
mov eax, lo
push eax
mov eax, a
push eax
call partition
mov j, eax
add esp, 16
mov eax, comp
push eax
mov eax, j
dec eax
push eax
mov eax, lo
push eax
mov eax, a
push eax
call _sort
add esp, 16
mov eax, comp
push eax
mov eax, hi
push eax
mov eax, j
inc eax
push eax
mov eax, a
push eax
call _sort
add esp, 16
.end:
mov esp, ebp
pop ebp
ret
;************************************************************************
; int partition(void **a, int lo, int hi, int (*cmp)(void *, void *));
;************************************************************************
%define a [ebp+8]
%define lo [ebp+12]
%define hi [ebp+16]
%define comp [ebp+20]
%define i [ebp-4]
%define j [ebp-8]
%define v [ebp-12]
partition:
push ebp
mov ebp, esp
sub esp, 12
mov eax, lo
mov i, eax
mov eax, hi
inc eax
mov j, eax
mov eax, lo
mov ebx, 4
imul ebx
mov ebx, eax
mov eax, a
add eax, ebx
mov eax, [eax]
mov v, eax
.everwhile:
jmp .eval_ai_l_v
.while_ai_l_v:
mov eax, i
mov ebx, hi
cmp eax, ebx
je .end_ai_l_v
.eval_ai_l_v:
mov eax, comp
push eax
mov eax, v
push eax
mov eax, i
inc eax
mov i, eax
mov ebx, 4
imul ebx
mov ebx, a
add ebx, eax
mov eax, [ebx]
push eax
call less
add esp, 12
cmp eax, 0
jne .while_ai_l_v
.end_ai_l_v:
.while_v_l_aj:
mov eax, j
mov ebx, lo
cmp eax, ebx
je .end_v_l_aj
.eval_v_l_aj:
mov eax, comp
push eax
mov eax, j
dec eax
mov j, eax
mov ebx, 4
imul ebx
mov ebx, a
add ebx, eax
mov eax, [ebx]
push eax
mov eax, v
push eax
call less
add esp, 12
cmp eax, 0
jne .while_v_l_aj
.end_v_l_aj:
mov eax, i
mov ebx, j
cmp eax, ebx
jge .endeverwhile
mov eax, j
push eax
mov eax, i
push eax
mov eax, a
push eax
call exch
add esp, 12
jmp .everwhile
.endeverwhile:
mov eax, j
push eax
mov eax, lo
push eax
mov eax, a
push eax
call exch
add esp, 12
mov eax, j
mov esp, ebp
pop ebp
ret
;************************************************************************
; int less(void *v, void *w, int (*cmp)(void *, void *));
;************************************************************************
%define v [ebp+8]
%define w [ebp+12]
%define comp [ebp+16]
less:
push ebp
mov ebp, esp
mov eax, w
push eax
mov eax, v
push eax
mov eax, comp
call eax
cmp eax, 0
jl .true
jmp .false
.true:
mov eax, 1
jmp .end
.false:
mov eax, 0
.end:
mov esp, ebp
pop ebp
ret
;************************************************************************
; void exch(void **a, int i, int j);
;************************************************************************
%define a [ebp+8]
%define i [ebp+12]
%define j [ebp+16]
exch:
push ebp
mov ebp, esp
mov eax, a
mov ebx, i
mov ecx, j
imul ebx, 4
imul ecx, 4
add ebx, eax
add ecx, eax
mov eax, [ebx]
mov edx, [ecx]
mov [ebx], edx
mov [ecx], eax
mov esp, ebp
pop ebp
ret
;************************************************************************
; int uniform(int n);
;************************************************************************
%define n [ebp+8]
uniform:
push ebp
mov ebp, esp
mov eax, n
cmp eax, 0
jg .validn
push invalidn
call printf
add esp, 4
jmp .end
.validn:
xor eax, eax
push eax
call time
push eax
call srand
add esp, 8
call rand
xor edx, edx
mov ebx, n
idiv ebx
mov eax, edx
.end:
mov esp, ebp
pop ebp
ret
;************************************************************************
; void shuffle(void **a, int len);
;************************************************************************
%define a [ebp+8]
%define len [ebp+12]
%define N [ebp-4]
%define i [ebp-8]
%define r [ebp-12]
%define temp [ebp-16]
shuffle:
push ebp
mov ebp, esp
sub esp, 16
mov eax, a
cmp eax, 0
jne .valida
jmp .end
.valida:
mov eax, len
mov N, eax
xor eax, eax
mov i, eax
jmp .evalfor
.for:
mov eax, i
mov ebx, N
sub ebx, eax
push ebx
call uniform
add esp, 4
mov ebx, i ;ebx = i
add eax, ebx
mov r, eax ;eax = r
imul ebx, 4
imul eax, 4
mov ecx, a
add ebx, a
add eax, a
mov ecx, [ebx]
mov edx, [eax]
mov [ebx], edx
mov [eax], ecx
mov eax, i
inc eax
mov i, eax
.evalfor:
mov eax, i
mov ebx, N
cmp eax, ebx
jl .for
.endfor:
push flagline
call printf
add esp, 4
.end:
mov esp, ebp
pop ebp
ret