-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathquick-sort.asm
162 lines (113 loc) · 3.34 KB
/
quick-sort.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
.386
.model flat, c
.code
;Code by Miguel Casillas.
;This code can be used and reproduced, please give credit
;void QuickSort(void *pArray, int nItems);
QuickSort PROC
;These registers must be restored at the end
push EBP
mov EBP, ESP
push EBX
push ESI
push EDI
;EBP + 8 is the array
;EBP + 12 is the number of items in the array
mov ESI, [EBP+8] ;ESI is the array
;setting ECX to the number of items
;we multiply by 4 (size of the element) in order to put ECX
;at the last address of the array
mov EAX, [EBP+12]
mov ECX, 4
mul ECX
mov ECX, EAX
;EAX will be our 'low index', we initially set it to 0
xor EAX, EAX
;EBX will be our 'high index', we initially set it to
;the last element of the array (currently stored in ECX)
mov EBX, ECX
;We now call our recursive function that will sort the array
call QuickRecursive
;Restoring the registers
pop EDI
pop ESI
pop EBX
pop EBP
RET
QuickSort ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Recursive QuickSort function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
QuickRecursive:
;if lowIndex >= highIndex, we exit the function
cmp EAX, EBX
jge PostIf
push EAX ;saving our low index, now EAX is 'i'
push EBX ;saving our high index, now EBX is 'j'
add EBX, 4 ;j = high + 1
;EDI is our pivot
;pivot = array[lowIndex];
mov EDI, [ESI+EAX]
MainLoop:
iIncreaseLoop:
;i++
add EAX, 4
;If i >= j, exit this loop
cmp EAX, EBX
jge End_iIncreaseLoop
;If array[i] >= pivot, exit this loop
cmp [ESI+EAX], EDI
jge End_iIncreaseLoop
;Go back to the top of this loop
jmp iIncreaseLoop
End_iIncreaseLoop:
jDecreaseLoop:
;j--
sub EBX, 4
;If array[j] <= pivot, exit this loop
cmp [ESI+EBX], EDI
jle End_jDecreaseLoop
;Go back to the top of this loop
jmp jDecreaseLoop
End_jDecreaseLoop:
;If i >= j, then don't swap and end the main loop
cmp EAX, EBX
jge EndMainLoop
;Else, swap array[i] with array [j]
push [ESI+EAX]
push [ESI+EBX]
pop [ESI+EAX]
pop [ESI+EBX]
;Go back to the top of the main loop
jmp MainLoop
EndMainLoop:
;Restore the high index into EDI
pop EDI
;Restore the low index into ECX
pop ECX
;If low index == j, don't swap
cmp ECX, EBX
je EndSwap
;Else, swap array[low index] with array[j]
push [ESI+ECX]
push [ESI+EBX]
pop [ESI+ECX]
pop [ESI+EBX]
EndSwap:
;Setting EAX back to the low index
mov EAX, ECX
push EDI ;Saving the high Index
push EBX ;Saving j
sub EBX, 4 ;Setting EBX to j-1
;QuickSort(array, low index, j-1)
call QuickRecursive
;Restore 'j' into EAX
pop EAX
add EAX, 4 ;setting EAX to j+1
;Restore the high index into EBX
pop EBX
;QuickSort(array, j+1, high index)
call QuickRecursive
PostIf:
RET
END