-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.asm
More file actions
581 lines (474 loc) · 11.6 KB
/
main.asm
File metadata and controls
581 lines (474 loc) · 11.6 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
INCLUDE Irvine32.inc
.DATA
Welcome_msg byte "**********************************************", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* WELCOME TO THE *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* COMPREHENSIVE SORTING ALGORITHMS *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* FRAMEWORK *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "**********************************************", 0Dh, 0Ah, 0
Thank_you_msg byte "**********************************************", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* THANK YOU *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* FOR USING OUR *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* COMPREHENSIVE SORTING ALGORITHMS *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "* FRAMEWORK *", 0Dh, 0Ah
byte "* *", 0Dh, 0Ah
byte "**********************************************", 0Dh, 0Ah, 0
Menu byte "**********************************************", 0Dh, 0Ah
byte "* Choose a Sorting Algorithm: *", 0Dh, 0Ah
byte "**********************************************", 0Dh, 0Ah
byte "1) Bubble Sort", 0Dh, 0Ah
byte "2) Selection Sort", 0Dh, 0Ah
byte "3) Insertion Sort", 0Dh, 0Ah
byte "4) Merge Sort", 0Dh, 0Ah
byte "5) Quick Sort", 0Dh, 0Ah
byte "**********************************************", 0Dh, 0Ah
byte "Enter your choice: ", 0
Menu2 byte "**********************************************", 0Dh, 0Ah
byte "* Choose the order: *", 0Dh, 0Ah
byte "**********************************************", 0Dh, 0Ah
byte "1) Ascending", 0Dh, 0Ah
byte "2) Descending", 0Dh, 0Ah
byte "**********************************************", 0Dh, 0Ah
byte "Enter your choice: ", 0
size_input byte "Enter the size of the array to sort: ", 0
input_taker byte "Enter element #", 0
input_taker2 byte ": ", 0
array_size DWORD ?
myArray DWORD 9999 DUP(?)
spacer byte " ", 0
presser byte "Press ENTER to continue...", 0
Output_message byte "Sorted Array: ", 0
.CODE
quickSort PROTO,
pArray:PTR DWORD,
loww:DWORD,
highh:DWORD
Welcome PROC
call clrscr
mov edx, OFFSET Welcome_msg
call writestring
call waitForInput
ret
Welcome ENDP
Thank_you PROC
call clrscr
mov edx, OFFSET Thank_you_msg
call writestring
ret
Thank_you ENDP
waitForInput PROC
mov edx, OFFSET presser
call crlf
call crlf
call crlf
call crlf
call crlf
call writestring
call readdec
ret
waitForInput ENDP
main PROC
call Welcome
call clrscr
call Take_input
call clrscr
mov edx, OFFSET Menu
call writestring
call readdec
cmp eax, 1
je call_bubble
cmp eax, 2
je call_selection
cmp eax, 3
je call_insertion
cmp eax, 4
je call_merge
cmp eax, 5
je call_quick
call_bubble:
call bubble_sort
jmp call_printer
call_selection:
call selection_sort
jmp call_printer
call_insertion:
call insertion_sort
call clrscr
mov edx, OFFSET Menu2
call writestring
call readDec
cmp eax, 2
jne la1
call ReverseSort
jmp exitter
la1:
call array_printer
jmp exitter
call_merge:
mov eax, 4
mov ebx, 0
mov edx, array_size
dec edx
mov esi, OFFSET myArray
call MergeSort
jmp call_printer
call_quick:
mov ebx, array_size
dec ebx
invoke quickSort, ADDR myArray, 0, ebx
jmp call_printer
call_printer:
call clrscr
mov edx, OFFSET Menu2
call writestring
call readDec
cmp eax, 2
je callDescending
call printer
jmp exitter
callDescending:
call Descending
exitter:
call waitForInput
call Thank_you
exit
main ENDP
bubble_sort PROC
mov ecx, array_size
dec ecx
mov esi, 0
L1:
mov edx, ecx
mov edi, 0
mov ecx, array_size
dec ecx
sub ecx, esi
L2:
mov ebx, edi
add ebx, 1
mov eax, myArray[ebx* Type myArray]
CMP myArray[edi* Type myArray], eax
JG swapper
jmp skipper
swapper:
xchg myArray[edi* Type myArray], eax
mov myArray[ebx* Type myArray], eax
skipper:
inc edi
loop L2
mov ecx, edx
inc esi
loop L1
ret
bubble_sort endp
selection_sort PROC
LOCAL minIndex : DWORD
mov ecx, array_size
dec ecx ; size - 1
mov esi, 0 ; i = 0
L1:
mov minIndex, esi
mov edi, esi
inc edi ; j = i+1
L2:
mov ebx, minIndex
mov eax, myArray[edi* Type myArray]
CMP eax, myArray[ebx* Type myArray]
JGE skipper
mov minIndex, edi
skipper:
inc edi
CMP edi, array_size
JL L2
CMP minIndex, esi
JE skipp
mov ebx, minIndex
mov eax, myArray[ebx* Type myArray]
xchg myArray[esi* Type myArray], eax
mov myArray[ebx* Type myArray], eax
skipp:
inc esi
loop L1
ret
selection_sort endp
insertion_sort PROC
mov ecx, array_size ; = size
mov esi, 1 ; i = 1
L1:
mov edx, myArray[esi* Type myArray] ; edx(temp) = arr[i]
mov edi, esi
dec edi ; j = i-1
L2:
CMP edi, 0
JL skipp
CMP myArray[edi* Type myArray], edx
JLE skipp
mov ebx, myArray[edi* Type myArray] ; ebx = arr[j]
inc edi
mov myArray[edi* Type myArray], ebx ; arr[j+1] = arr[j]
dec edi
dec edi ; j--
JMP L2
skipp:
inc edi
mov myArray[edi* Type myArray], edx ; arr[j+1] = edx(temp)
dec edi
inc esi
loop L1
ret
insertion_sort endp
MergeSort PROC
LOCAL m:DWORD, l:DWORD, h:DWORD, t:DWORD
mov t, eax
mov l, ebx
mov h, edx
cmp ebx, edx
jge endMS
mov eax, l
add eax, h
shr eax, 1
mov m, eax
mov ebx, l
mov edx, m
mov eax, t
call MergeSort
mov ebx, m
inc ebx
mov edx, h
mov eax, t
call MergeSort
mov ebx, l
mov edx, h
mov eax, t
mov eax, m
call Merge
endMS:
ret
MergeSort ENDP
Merge PROC
LOCAL i:DWORD, j:DWORD, k:DWORD, l:DWORD, m:DWORD, h:DWORD, t[100]:DWORD
mov i, ebx
mov k, ebx
mov j, eax
inc j
mov m, eax
mov h, edx
mov l, ebx
mergeLoop:
mov edx, m
cmp i, edx
jg outMerge
mov edx, h
cmp j, edx
jg outMerge
mov ebx, i
mov eax, [esi + ebx * 4]
mov ebx, j
cmp eax, [esi + ebx * 4]
jl copyL
jmp copyR
copyL:
mov ebx, i
mov edx, k
mov eax, [esi + ebx * 4]
mov [t + edx * 4], eax
inc k
inc i
jmp nextMerge
copyR:
mov ebx, j
mov edx, k
mov eax, [esi + ebx * 4]
mov [t + edx * 4], eax
inc k
inc j
nextMerge:
jmp mergeLoop
outMerge:
secondLoop:
mov eax, m
cmp i, eax
jg thirdLoop
mov ebx, i
mov edx, k
mov eax, [esi + ebx * 4]
mov [t + edx * 4], eax
inc k
inc i
jmp secondLoop
thirdLoop:
mov eax, h
cmp j, eax
jg finalCopy
mov ebx, j
mov edx, k
mov eax, [esi + ebx * 4]
mov [t + edx * 4], eax
inc k
inc j
jmp thirdLoop
finalCopy:
mov eax, l
mov i, eax
finalLoop:
mov eax, k
cmp i, eax
jge endDM
mov ebx, i
mov eax, [t + ebx * 4]
mov [esi + ebx * 4], eax
inc i
jmp finalLoop
endDM:
ret
Merge ENDP
Partition PROC,
pArray:PTR DWORD,
loww:DWORD,
highh:DWORD
LOCAL pivot:DWORD, i:DWORD, j:DWORD
mov esi, pArray
mov edx, highh
mov eax, [esi + edx * 4]
mov pivot, eax
mov eax, loww
mov j, eax
dec eax
mov i, eax
forLoop:
mov eax, j
cmp eax, highh
jnl partitionDone
mov ecx, j
mov eax, [esi + ecx * 4]
cmp eax, pivot
jg skipSwap
inc i
mov edx, i
mov eax, [esi + edx * 4]
mov edx, j
mov ebx, [esi + edx * 4]
mov edx, i
mov [esi + edx * 4], ebx
mov edx, j
mov [esi + edx * 4], eax
skipSwap:
inc j
jmp forLoop
partitionDone:
inc i
mov edx, i
mov eax, [esi + edx * 4]
mov edx, highh
mov ebx, [esi + edx * 4]
mov [esi + edx * 4], eax
mov edx, i
mov [esi + edx * 4], ebx
mov eax, i
ret
Partition ENDP
quickSort PROC,
pArray:PTR DWORD,
loww:DWORD,
highh:DWORD
LOCAL pi:DWORD
mov eax, loww
cmp eax, highh
jge endQuickSort
invoke Partition, pArray, loww, highh
mov pi, eax
dec pi
invoke quickSort, pArray, loww, pi
add pi, 2
invoke quickSort, pArray, pi, highh
endQuickSort:
ret
quickSort ENDP
array_printer PROC USES ecx edx esi
mov esi, 1
mov ecx, array_size
mov edx, OFFSET Output_message
call writeString
mov edx, OFFSET spacer
Printer_loop:
mov eax, myArray[esi * TYPE myArray]
call writedec
call writeString
inc esi
loop Printer_loop
ret
array_printer ENDP
ReverseSort PROC USES ecx edx esi
mov esi, array_size
mov ecx, array_size
mov edx, OFFSET Output_message
call writeString
mov edx, OFFSET spacer
Printer_loop:
mov eax, myArray[esi * TYPE myArray]
call writedec
call writeString
dec esi
loop Printer_loop
ret
ReverseSort ENDP
Take_input PROC
mov edx, OFFSET size_input
call writestring
call readdec
mov array_size, eax
mov ecx, array_size
mov eax, 1
mov esi , 0
L1:
mov edx, OFFSET input_taker
call writestring
call writedec
mov edx, OFFSET input_taker2
call writestring
push eax
call readint
mov myArray[esi * type myArray], eax
inc esi
pop eax
inc eax
loop L1
ret
Take_input endp
Descending PROC USES ecx edx esi
mov esi, array_size
dec esi
mov ecx, array_size
mov edx, OFFSET Output_message
call writeString
mov edx, OFFSET spacer
Printer_loop:
mov eax, myArray[esi * TYPE myArray]
call writedec
call writeString
dec esi
loop Printer_loop
ret
Descending ENDP
printer PROC USES ecx edx esi
mov esi, 0
mov ecx, array_size
mov edx, OFFSET Output_message
call writeString
mov edx, OFFSET spacer
Printer_loop:
mov eax, myArray[esi * TYPE myArray]
call writedec
call writeString
inc esi
loop Printer_loop
ret
printer ENDP
END main