-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingVis.asm
3501 lines (2809 loc) · 74.2 KB
/
SortingVis.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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
INCLUDE Irvine32.inc
; AUTHOR: Shuval de Villiers
; CS2290
; LAB 9
;------------------------
; PURPOSE:
; Visualize these sorting algorithms:
; - bubble sort
; - cocktail shaker sort
; - comb sort
; - selection sort
; - insertion sort
; - shell sort
; - quick sort
; - merge sort
; - heap sort
; - radix sort
; - pigeon hole sort
; - BOGO sort
; Sorting algorithms start at line ~1815
;------------------------
CONSOLE_WIDTH = 119 ; default = 119 minimum = 65 (odd number pls)
CONSOLE_HEIGHT = (CONSOLE_WIDTH + 1) / 2
BOX_WIDTH = CONSOLE_WIDTH - (CONSOLE_WIDTH / 6)
BOX_HEIGHT = BOX_WIDTH / 2
ARRAY_SIZE = BOX_HEIGHT ; 50 elements for CONSOLE_WIDTH of 119
BOX_TOP_MARGIN = (CONSOLE_WIDTH / 36) + 2
BOX_LEFT_MARGIN = (CONSOLE_WIDTH - BOX_WIDTH) / 2
BAR_CHAR = 219 ; 178 looks good too
SPACE = 32
; These are stored in AH after calling ReadKey
UP_ARROW = 048h
DOWN_ARROW = 050h
LEFT_ARROW = 04Bh
RIGHT_ARROW = 04Dh
SPACE_BAR = 039h
ENTER_KEY = 01Ch
ESCAPE_KEY = 01h
; these delays are used when RUN ALL is selected
CHECK_DELAY = 10
DEFAULT_DELAY = 15
SLOW_DELAY = 20
SLOWER_DELAY = 30
SLOWEST_DELAY = 500
SHUFFLE_DELAY = 8
PAUSE_DELAY = 1000
BACK_COLOR = gray
FORE_COLOR = black
CHECK_COLOR = lightGreen ; color for checking order
CURRENT_COLOR = lightRed ; elements are being compared or swapped
PIVOT_COLOR = lightBlue ; used in merge/quick/selection sorts
SELECT_COLOR = lightCyan ; used in menu
PADDING_COLOR = lightGray
MSG_COLOR = white
SORT_MSG_LOC = 0101h ; where the algorithm type is printed
CMP_LOC = 0121h ; where # of comparisons is printed
ACCESSES_LOC = 0137h ; where # of accesses is printed
OPTIONS_LOC = 0500h ; offsets where menu items are displayed
.data
; goto DX = 03704h for debug_msg
debug_msg BYTE " 0 1 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 201 2 3 4 5 6 7 8 9",
" 301 2 3 4 5 6 7 8 9 401 2 3 4 5 6 7 8 9",0
;*** MENU ITEMS ***
menu_header_msg BYTE 39 DUP (SPACE), 13,10,
" SHUVAL'S SUPER SORTING SIMULATOR 2019 ",13,10,
39 DUP (SPACE),0
; choose sort
run_all_opt BYTE " RUN ALL ",0
insertion_opt BYTE " Insertion Sort ",0
bubble_opt BYTE " Bubble Sort ",0
comb_opt BYTE " Comb Sort ",0
shell_opt BYTE " Shell Sort ",0
cocktail_opt BYTE " Cocktail Sort ",0
selection_opt BYTE " Selection Sort ",0
quick_opt BYTE " Quick Sort ",0
merge_opt BYTE " Merge Sort ",0
heap_opt BYTE " Heap Sort ",0
radix_opt BYTE " Radix Sort ",0
pigeon_opt BYTE " Pigeonhole Sort ",0
bogo_opt BYTE " BogoSort ",0
run_all_info BYTE " ",0
INFO_SIZE = ($ - run_all_info)
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
INFO_ROWS = ($ - run_all_info) / INFO_SIZE
bubble_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Bubble sort is a simple sorting algorithm",0
BYTE "that works by repeatedly swapping ",0
BYTE "adjacent elements if they are in the ",0
BYTE "wrong order. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Easy to conceptualize and implement",0
BYTE " - Stable and in-place ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Much slower than other O(n^2) ",0
BYTE " sorting algorithms ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
shaker_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Cocktail shaker sort is a variation on ",0
BYTE "bubble sort where the list is traversed ",0
BYTE "in both directions alternatively. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Easy to conceptualize and implement",0
BYTE " - Stable and in-place ",0
BYTE " - Slightly faster than bubble sort ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Still much slower than other O(n^2)",0
BYTE " sorting algorithms ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
comb_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Comb sort is a generalization of bubble ",0
BYTE "sort. Instead of swapping adjacent ",0
BYTE "elements it swaps elements at an ",0
BYTE "interval that decreases by a factor of ",0
BYTE "1.3 on each pass. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - In-place ",0
BYTE " - Improves on bubble sort ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Still slower than other O(n^2) ",0
BYTE " sorting algorithms ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
selection_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Selection sort repeatedly finds the ",0
BYTE "maximum element in the unsorted portion ",0
BYTE "of the array then moves it to the ",0
BYTE "beginning of the sorted portion until the",0
BYTE "whole array is sorted. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Easy to conceptualize and implement",0
BYTE " - In-place ",0
BYTE " - Stable depending on implementation ",0
BYTE " - At most performs O(n) swaps ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Usually usurped by insertion sort ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
insertion_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Insertion sort sorts an array the same ",0
BYTE "way that you would sort playing cards in ",0
BYTE "your hands. At each iteration, insertion ",0
BYTE "sort removes one element from the ",0
BYTE "unsorted portion of the array, then finds",0
BYTE "the location it belongs within the sorted",0
BYTE "portion, and inserts it there. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Usually the O(n^2) sorting algoritm",0
BYTE " of choice ",0
BYTE " - Stable and in place ",0
BYTE " - Online (can sort a list as it ",0
BYTE " receives it) ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Usually only useful with small data",0
BYTE " sets ",0
BYTE " ",0
BYTE " ",0
shell_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Shell sort is a generalizatin of ",0
BYTE "insertion sort. It uses insertion sort to",0
BYTE "sort elements on an interval that ",0
BYTE "decreases with each pass. The interval is",0
BYTE "divided in half for this implementation. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Twice as fast as insertion sort ",0
BYTE " - In-place ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Not stable ",0
BYTE " - Usually usurped by quicksort unless",0
BYTE " you cannot use the call stack ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
quick_info BYTE "Worst case performance: O(n^2) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Quick sort uses a divide and conquer ",0
BYTE "strategy. It selects a pivot, then puts ",0
BYTE "all elements smaller than the pivot on ",0
BYTE "left of the pivot and all elements ",0
BYTE "greater on the right. My implementation ",0
BYTE "uses the rightmost element as the pivot. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Takes O(nlogn) time on average ",0
BYTE " - Generally performs 2-3 times faster",0
BYTE " than heap sort and merge sort ",0
BYTE " - In-place ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Difficult to make stable while ",0
BYTE " maintaining its speed advantage ",0
BYTE " - Relies on the call stack unless ",0
BYTE " implemented iteratively ",0
BYTE " - Outperformed by merge sort when ",0
BYTE " sorting linked lists ",0
merge_info BYTE "Worst case performance: O(nlogn) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Merge sort is a divide and conquer ",0
BYTE "algorithm. It divides the array into two ",0
BYTE "sub-arrays until each half contains one ",0
BYTE "element. It then repeatedly merges the ",0
BYTE "two sorted halves until all sub-arrays ",0
BYTE "are merged. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Stable ",0
BYTE " - Very effective for linked lists ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Uses O(n) auxiliary space with ",0
BYTE " RAM-based data ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
heap_info BYTE "Worst case performance: O(nlogn) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Heap sort is similar to selection sort ",0
BYTE "but organizes the data into a max heap ",0
BYTE "data structure to extract the maximums. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - In-place ",0
BYTE " - Looks cool ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Outperformed by quicksort and merge",0
BYTE " sort for most applications ",0
BYTE " - Typically not stable ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
radix_info BYTE "Worst case performance: O(n*(key size)) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Radix sort is a non-comparative sorting ",0
BYTE "algorithm. It works by distributing ",0
BYTE "elements into buckets according to their ",0
BYTE "radix. The array is re-bucketed for every",0
BYTE "digit in the largest element while ",0
BYTE "preserving the ordering of the previous ",0
BYTE "pass. My version works in base 10. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Faster than comparative sorts for a",0
BYTE " wide range of input numbers ",0
BYTE " - Stable ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Not in-place ",0
BYTE " - Constrained to lexicographic data ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
pigeon_info BYTE "Worst case performance: O(n + range) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Pigeonhole sort works by creating an ",0
BYTE 'auxiliary array of empty "pigeonholes", ',0
BYTE "one for each possible element in the ",0
BYTE "range of the orginial array. It then does",0
BYTE "one scan of the original array, placing ",0
BYTE "each element in its corresponding ",0
BYTE "pigeonhole. Then it iterates over the ",0
BYTE "pigeonhole array, placing the elements ",0
BYTE "back into the original array in the ",0
BYTE "correct order. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - Faster than comparative sorts when ",0
BYTE " the number of elements is about the",0
BYTE " same as the range ",0
BYTE " - Stable ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Not in-place ",0
BYTE " - Usually usurped by radix sort ",0
bogo_info BYTE "Average case performance: O(n*n!) ",0
BYTE (INFO_SIZE - 1) DUP ('~'),0
BYTE "Bogosort works by repeatedly generating a",0
BYTE "random permutation of its input and ",0
BYTE "checking if it's sorted. ",0
BYTE " ",0
BYTE " PROS: ",0
BYTE " - May be useful for educational ",0
BYTE " purposes ",0
BYTE " - In-place ",0
BYTE " - Best-case performance is O(n) ",0
BYTE " ",0
BYTE " CONS: ",0
BYTE " - Ridiculous ",0
BYTE " - No upper bound for worst case ",0
BYTE " - Not stable ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
BYTE " ",0
; choose speed
choose_speed_msg BYTE " SELECT A SPEED ",0
speed_padding BYTE (LENGTHOF choose_speed_msg)-1 DUP (SPACE),0
vfast_opt BYTE " 0 ms (very fast) ",0
fast_opt BYTE " 15 ms (fast) ",0
normal_opt BYTE " 30 ms (normal) ",0
slow_opt BYTE " 60 ms (slow) ",0
vslow_opt BYTE " 350 ms (very slow) ",0
; Available speeds
speedArray WORD 0, 15, 30, 60, 350
; this switch is used in menu for printing the sorting algos
caseTable BYTE -1
DWORD printRunAll
entrySize = ($ - caseTable)
BYTE 0
DWORD printBubble
BYTE 1
DWORD printShaker
BYTE 2
DWORD printComb
BYTE 3
DWORD printSelection
BYTE 4
DWORD printInsertion
BYTE 5
DWORD printShell
BYTE 6
DWORD printQuick
BYTE 7
DWORD printMerge
BYTE 8
DWORD printHeap
BYTE 9
DWORD printRadix
BYTE 10
DWORD printPigeon
BYTE 11
DWORD printBogo
tableSize = ($ - caseTable) / entrySize
; this switch is used in menu for printing the speed options
speedTable BYTE 0
DWORD printVFast
speedEntry = ($ - speedTable)
BYTE 1
DWORD printFast
BYTE 2
DWORD printNormal
BYTE 3
DWORD printSlow
BYTE 4
DWORD printVSlow
speedSize = ($ - speedTable) / speedEntry
; Controls
back_msg BYTE "[Esc] - Go back ",0
quit_msg BYTE "[Esc] - Quit ",0
navig_msg BYTE '[',24,25,27,26, "] - Navigation",0
insertion_msg BYTE "Insertion Sort - comparisons: array accesses:",0
bubble_msg BYTE "Bubble Sort - comparisons: array accesses:",0
comb_msg BYTE "Comb Sort - comparisons: array accesses:",0
shell_msg BYTE "Shell Sort - comparisons: array accesses:",0
cocktail_msg BYTE "Cocktail Sort - comparisons: array accesses:",0
selection_msg BYTE "Selection Sort - comparisons: array accesses:",0
quick_msg BYTE "Quick Sort - comparisons: array accesses:",0
merge_msg BYTE "Merge Sort - comparisons: array accesses:",0
heap_msg BYTE "Heap Sort - comparisons: array accesses:",0
radix_msg BYTE "Radix Sort - comparisons: array accesses:",0
pigeon_msg BYTE "Pigeonhole Sort - comparisons: array accesses:",0
bogo_msg BYTE "Bogosort - comparisons: array accesses:",0
shuffle_msg BYTE "Shuffling...",0
clear_msg BYTE 60 DUP (SPACE),0 ; clears previous msg
array BYTE ARRAY_SIZE DUP (?) ; stores base array values
garbage BYTE ARRAY_SIZE ; checkSort goes one byte out of bounds
.data?
speed_loc WORD ? ; where speed option get printed
tmpArray BYTE ARRAY_SIZE DUP (?) ; used for merge and radix sort
pigeonArray BYTE ARRAY_SIZE DUP (?) ; used for pigeon sort
countArray BYTE 10 DUP (?) ; used for radix sort
mid DWORD ? ; used in merge sort
; indices for drawSwap()
index2 BYTE ?
index1 BYTE ?
comparisons DWORD ? ; tracks # of comparisons
accesses DWORD ? ; tracks # of array accesses
sort_delay DWORD ? ; delay during sorts
; lazy way to make sure things work
saveEBX DWORD ?
saveStack DWORD ?
print_bool BYTE ?
.code
main PROC
;;;;;;;;; Driver function
call Randomize
; start at 'RUN ALL' option
mov EBX, -1
mov saveEBX, EBX
; with MENU_LOOP being global, stack slowly grows if program is used
; for a while. So I reset it when a jump to the menu is made
mov saveStack, ESP
; call getOutOfJailFreeCard
MENU_LOOP::
mov ESP, saveStack
; clear buffer incase Libero was pressing too many buttons
call readKey
; must be reset at start of menu
mov speed_loc, OPTIONS_LOC + 01Bh
mov EBX, saveEBX
call chooseSort ; returns EBX = -1 to +11
mov saveEBX, EBX
; all sorts take EDX and EAX as parameters
mov EDX, OFFSET array
mov EAX, ARRAY_SIZE
; initialize array with values 1 to n
call initArray
; if RUN ALL
cmp EBX, -1
jg NOT_ALL
call runAll
jmp CONT
NOT_ALL:
; else...
call chooseSpeed
call shuffle
call pauseDelay
; if...
cmp EBX, 0
jg NO_BUBBLE
call bubbleSort
jmp CONT
NO_BUBBLE:
cmp EBX, 1
jg NO_SHAKER
call cocktailSort
jmp CONT
NO_SHAKER:
cmp EBX, 2
jg NO_COMB
call combSort
jmp CONT
NO_COMB:
cmp EBX, 3
jg NO_SELECTION
call selectionSort
jmp CONT
NO_SELECTION:
cmp EBX, 4
jg NO_INSERTION
call insertionSort
jmp CONT
NO_INSERTION:
cmp EBX, 5
jg NO_SHELL
call shellSort
jmp CONT
NO_SHELL:
cmp EBX, 6
jg NO_QUICK
call quickSort
jmp CONT
NO_QUICK:
cmp EBX, 7
jg NO_MERGE
call mergeSort
jmp CONT
NO_MERGE:
cmp EBX, 8
jg NO_HEAP
call heapSort
jmp CONT
NO_HEAP:
cmp EBX, 9
jg NO_RADIX
call radixSort
jmp CONT
NO_RADIX:
cmp EBX, 10
jg NO_PIGEON
call pigeonholeSort
jmp CONT
NO_PIGEON:
cmp EBX, 11
jg NO_BOGO
call bogoSort
jmp CONT
NO_BOGO:
; ENDIF
CONT:
call checkSort
; wait for [Esc] to return to menu
WAIT_LOOP:
mov EAX, 50
call delay
call readKey
cmp AH, ESCAPE_KEY
je RETURN
cmp AH, LEFT_ARROW
je RETURN
jmp WAIT_LOOP
RETURN:
jmp MENU_LOOP
exit
main ENDP
;------------------------
runAll PROC USES EAX ECX EDX
; runs through all sorts
; PARAMETERS:
; EDX --> offset of first element in byte array
; EAX --> size of array
;------------------------
call clrscr
call shuffle
call pauseDelay
mov sort_delay, DEFAULT_DELAY
call bubbleSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, DEFAULT_DELAY
call cocktailSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, DEFAULT_DELAY
call combSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, DEFAULT_DELAY
call selectionSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, DEFAULT_DELAY
call insertionSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOW_DELAY
call shellSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOWER_DELAY
call quickSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOW_DELAY
call mergeSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOWER_DELAY
call heapSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOWER_DELAY
call radixSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOW_DELAY
call pigeonholeSort
call checkSort
call pauseDelay
call shuffle
call pauseDelay
mov sort_delay, SLOWEST_DELAY
; bogo usually doesn't end so it returns when the user hits [esc]
call bogoSort
ret
runAll ENDP
;------------------------
chooseSort PROC USES EAX ECX EDX
; menu to choose the sorting algorithm
; RETURNS:
; EBX --> number representing which sorting algo to run
;------------------------
; reset color
mov EAX, PADDING_COLOR + (BACK_COLOR * 16)
call SetTextColor
call clrscr
; print controls...
mov DH, CONSOLE_HEIGHT - 2
mov DL, 1
call GotoXY
mov EDX, OFFSET quit_msg
call WriteString
mov EDX, OFFSET navig_msg
call WriteString
mov EAX, MSG_COLOR + (FORE_COLOR * 16)
call setTextColor
; printing header
mov DX, OPTIONS_LOC - 0400h
call GotoXY
mov EDX, OFFSET menu_header_msg
call writeString
mov EAX, MSG_COLOR + (BACK_COLOR * 16)
call setTextColor
; printing menu options
mov print_bool, 0
push EBX
mov ECX, tableSize
PRINTING:
mov EBX, ECX
sub EBX, 2
call switch
LOOP PRINTING
pop EBX
mov EAX, FORE_COLOR + (SELECT_COLOR * 16)
call setTextColor
; print highlighted option
mov print_bool, 1
call switch
; allows user to navigate menu
WAIT_LOOP:
mov EAX, 50
call delay
push EBX
call readKey
pop EBX
; if [Esc] is hit, quit program
cmp AH, ESCAPE_KEY
jne GOOD
exit
GOOD:
; user makes selection
cmp AH, SPACE_BAR
je RETURN
cmp AH, ENTER_KEY
je RETURN
cmp AH, RIGHT_ARROW
je RETURN
jmp NO_RET
RETURN:
; erases info
push EBX
mov EBX, OFFSET run_all_info
call printInfo
pop EBX
mov EAX, FORE_COLOR + (BACK_COLOR * 16)
call setTextColor
; determine where to draw speed options
mov ECX, EDI
shl ECX, 8
add speed_loc, CX
ret
NO_RET:
; moving up or down
cmp AH, UP_ARROW
jne NOT_UP
call moveUp
jmp NOT_DOWN
NOT_UP:
cmp AH, DOWN_ARROW
jne NOT_DOWN
call moveDown
NOT_DOWN:
jmp WAIT_LOOP
ret
chooseSort ENDP
;------------------------
moveUp PROC
; moves cursor up
; RETURNS:
; EBX --> new selection
; PARAMETERS:
; EBX --> current selection
;------------------------
; don't go to high!
cmp BL, -1
je RETURN
; erase prev selection
mov EAX, MSG_COLOR + (BACK_COLOR * 16)
call setTextColor
call switch
dec BL
; print new selection
mov EAX, FORE_COLOR + (SELECT_COLOR * 16)
call setTextColor
call switch
RETURN:
ret
moveUp ENDP
;------------------------
moveDown PROC
; moves cursor down
; RETURNS:
; EBX --> new selection
; PARAMETERS:
; EBX --> current selection
;------------------------
; don't go to low!
cmp BL, 11
je RETURN
; erase prev selection
mov EAX, MSG_COLOR + (BACK_COLOR * 16)
call setTextColor
call switch
inc BL
; print new selection
mov EAX, FORE_COLOR + (SELECT_COLOR * 16)
call setTextColor
call switch
RETURN:
ret
moveDown ENDP
;------------------------
switch PROC USES ECX ESI
; Prints the appropriate algo option
; PARAMETERS:
; EBX --> option to print (-1 to 11)
;------------------------
mov ESI, OFFSET caseTable
mov ECX, tableSize
SWITCH_LOOP:
cmp BL, [ESI]
jne CONT
; prints the appropriate option
call NEAR PTR [ESI + 1]
jmp BREAK
CONT:
add ESI, entrySize
LOOP SWITCH_LOOP
BREAK:
ret
switch ENDP
;------------------------
printInfo PROC USES EAX EBX ECX EDX
; Prints the appropriate info box
; PARAMETERS:
; EBX --> offset of the info box to print
;------------------------
; only prints if print_bool is true
cmp print_bool, 0
je NO_INFO
mov EAX, MSG_COLOR + (BACK_COLOR * 16)
call setTextColor
; EAX stores the location
mov EAX, OPTIONS_LOC
add EAX, 0217h
; prime loop
mov ECX, INFO_ROWS
L1:
mov EDX, EAX
call GotoXY
mov EDX, EBX
call writeString
; move to next line
add EBX, INFO_SIZE
add EAX, 0100h
LOOP L1
NO_INFO:
ret
printInfo ENDP
; ****** SORT OPTIONS ******
; these print an option then adjust EBX and EDI
; EDI is used to determine where the speed menu gets printed
printRunAll PROC USES EDX
mov DX, OPTIONS_LOC
call GotoXY
mov EDX, OFFSET run_all_opt
call writeString
mov EBX, OFFSET run_all_info
call printInfo
mov EBX, -1
ret
printRunAll ENDP
printBubble PROC USES EDX
mov DX, OPTIONS_LOC + 0200h
call GotoXY
mov EDX, OFFSET bubble_opt
call writeString
mov EBX, OFFSET bubble_info
call printInfo
mov EBX, 0
mov EDI, 0
ret
printBubble ENDP