-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm_Q4
More file actions
655 lines (524 loc) · 12.7 KB
/
Copy pathasm_Q4
File metadata and controls
655 lines (524 loc) · 12.7 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
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
INCLUDE Irvine32.inc
INCLUDE asm3_Q4_data.inc
;Submit by: Omer Idel, ID: 203347794
;This program make QuickSort
;and make Collatz QuickSort (CollQuickSort)
;Arr array sorted by CollQuickSort function
;DiffArr shows the differnce beetween the regualr QuickSort
;and the Collatz QuickSort
;if there is 0 at the i'th place - it's the same value
;if there is 1 at the i'th place - it's not the same value
.data
FirstLeft=0
MyName BYTE "Omer Idel, ID: 203347794", 10, 13, 0
DupliArr word NN dup(?)
count word 0 ; for Collatz
newpivotindex dword ?
space byte " ",13,10,0
.code
my_main PROC
call randomize
mov edx, offset MyName
call WriteString
push offset Arr
push offset DupliArr
push lengthof NN
call dupArr
push offset DupliArr
push FirstLeft
push dword ptr (NN-1)
call QuickSort
push offset Arr
push FirstLeft
push dword ptr (NN-1)
call CollQuickSortSecond
push offset DupliArr
push offset Arr
push offset DiffArr
push dword ptr NN
call FindTheDiff
mov edx, offset DiffArr
call exitProcess
my_main ENDP
;------------------------------------------------------------
;This function find the differnce between the regular quick sort
;and the coll quick sort
;input: 3 Arrays - DupliArr, Arr, DiffArr and the Size (16 Bytes)
;output: Nothing
;------------------------------------------------------------
FindTheDiff PROC
push ebp
mov ebp, esp
push eax
push ebx
push ecx
push edx
mov eax, [ebp+20] ; offset of DupliArr
mov ebx, [ebp+16] ; offset of Arr
mov edx, [ebp+12] ; offset of DiffArr
mov ecx, [ebp+8] ; Size of Arrays
FindDiffLoop:
shl ecx, 1
mov di, word ptr[eax+ecx-2]
cmp di, word ptr[ebx+ecx-2]
je FillDiffArray
mov word ptr[edx+ecx-2], 1
shr ecx, 1
loop FindDiffLoop
FillDiffArray:
mov word ptr[edx+ecx-2], 0
shr ecx, 1
loop FindDiffLoop
pop ecx
pop edx
pop ebx
pop eax
mov esp, ebp
pop ebp
ret 16
FindTheDiff ENDP
;------------------------------------------------------------
;This function make copy of arr
;input: 2 Arrays - DupliArr, Arr and Size (12 Bytes)
;output: Nothing
;------------------------------------------------------------
dupArr PROC
push ebp
mov ebp, esp
push eax
push ebx
push ecx
push edx ; helper
mov eax, [ebp+16] ; offset of Arr
mov ebx, [ebp+12] ; offset of DupliArr
mov ecx, [ebp+8] ; Size of Arrays
duplicateArr:
shl ecx, 1
mov dx, word ptr [eax+ecx-2]
mov word ptr [ebx+ecx-2], dx
shr ecx, 1
loop duplicateArr
pop edx
pop ecx
pop ebx
pop eax
mov esp, ebp
pop ebp
ret 12
dupArr ENDP
;------------------------------------------------------------
;This is the collatz function
;input: number (2 Bytes)
;output: ax - the collatz value
;------------------------------------------------------------
Collatz PROC
push ebp
mov ebp, esp
push ecx ; helper for mul
push edx ; helper for div
xor eax, eax
mov ax, word ptr [ebp + 8] ; the input
shr ax, 1 ;check if even or not
jnc EvenNum ;if even
jmp OddNum ;else
jmp EndCollatz
OddNum:
cmp word ptr [ebp + 8], 1 ;checking the stop conditions
je EndCollatz
mov ax, word ptr [ebp + 8]
mov ecx, 3
mul ecx
inc ax
inc Count
push ax
call Collatz
jmp EndCollatz
EvenNum:
cmp word ptr [ebp + 8], 1 ; checking the stop conditions
je EndCollatz
mov ax, word ptr [ebp + 8]
mov ecx, 2
xor edx, edx
div ecx
inc Count
push ax
call Collatz
EndCollatz:
mov ax, count
pop edx
pop ecx
mov esp, ebp
pop ebp
ret 2
Collatz ENDP
;------------------------------------------------------------
;This function get left and right and retrun a random index beetwen those numbers
;input: left, right (8 Bytes)
;output: ax - the random index
;------------------------------------------------------------
ChooseIndex Proc
push ebp
mov ebp, esp
push edx
mov eax, [ebp+12] ; para right
mov edx, [ebp+8] ; para left
sub eax, edx
sub eax, 1
cmp eax, 0
je donechooswindex
call RandomRange
donechooswindex:
pop edx
mov esp, ebp
pop ebp
ret 8
ChooseIndex ENDP
;------------------------------------------------------------
;This is the regular partion
;This partion return the index of the new location of pivot
;input: Array, Left, Right, Pivot (16 Bytes)
;output: ax - the new index of pivot
;------------------------------------------------------------
Partition PROC
push ebp
mov ebp, esp
sub esp, 8 ; ebp-4 for index_i, ebp-8 for index_j
push ecx
push edx
push ebx
mov edx, [ebp+20] ;address of Arr
mov ebx, [ebp+16] ;index Left
mov ecx, [ebp+12] ;index Right
mov eax, [ebp+8] ;pivot
;for relative location in arr
shl ebx, 1
add edx, ebx
shr ebx, 1
;for looping
sub ecx, ebx
inc ecx
mov dword ptr [ebp-4], 0 ; index_i
mov dword ptr [ebp-8], 0 ; index_j
sub dword ptr [ebp-4], 2
;moving to the right place in the arr and taking the arr[i]
shl eax, 1 ; shifing left becuase the arr is word arr
mov ax, word ptr [edx+eax]
Ploop:
mov esi, edx
add esi, dword ptr [ebp-8]
mov edi, 0 ; reset the var
mov di, word ptr [esi]
cmp di, ax
jae continueL
add dword ptr [ebp-4], 2
;push eax and ecx for saving them before the swap
push eax
push ecx
xor eax, eax
xor ecx, ecx
;beacuse lea dosent work with vars - save ebx and use him to reach the right index
push ebx
xor ebx, ebx
mov ebx, dword ptr [ebp-4]
lea eax, [edx + [ebx]]
push eax
xor ebx, ebx
mov ebx, dword ptr [ebp-8]
lea ecx, [edx + [ebx]]
push ecx
call Swap
pop ebx
pop ecx
pop eax
add dword ptr [ebp-8], 2
loop Ploop
Jmp DonePartition
continueL:
add dword ptr [ebp-8], 2
loop Ploop
DonePartition:
add dword ptr [ebp-4], 2
xor ecx, ecx
xor ebx, ebx
mov ebx, dword ptr [ebp-4]
lea ecx, [edx + [ebx]]
push ecx
xor ecx, ecx
xor ebx, ebx
mov ebx, [ebp+16] ;index Left
mov ecx, [ebp+12] ;index Right
sub ecx, ebx
inc ecx
findPivotIndex:
shl ecx, 1
cmp ax, [edx+ecx]
je contine
shr ecx, 1
loop findPivotIndex
contine:
lea ecx, [edx + [ecx]]
push ecx
call Swap
mov edx, [ebp+20]
mov ecx, lengthof arr
findPivotIndexLast:
shl ecx, 1
cmp ax, [edx+ecx]
je DoneDone
shr ecx, 1
loop findPivotIndexLast
DoneDone:
shr ecx, 1
mov eax, ecx ; return the pivot index
pop ebx
pop edx
pop ecx
mov esp, ebp
pop ebp
ret 16
Partition ENDP
;------------------------------------------------------------
;This is the QuickSort
;input: Array, Left, Right (12 Bytes)
;output: Nothing
;------------------------------------------------------------
QuickSort PROC
push ebp
mov ebp ,esp
sub esp ,8 ; first 4 bytes for choose index, second 4 bytes for newpivotindex
push ecx
push edx
push ebx
mov ebx, [ebp + 16] ; Arr offset
mov edx, [ebp + 12] ; Left
mov ecx, [ebp +8] ;Right
cmp dx, cx
jge DoneQsort
push ecx ; push right
push edx ; push Left
call ChooseIndex ; the return call is in ax
mov [ebp-4], eax
push ebx ; the address of the arr
push edx ; push left
push ecx ; push right
push [ebp-4] ; push pivot index that returned from Chooseindex
call partition
mov [ebp-8], eax
push ebx ; the address of the arr
push edx ; push left
push ecx
mov ecx, [ebp-8]
mov newpivotindex, ecx
sub newpivotindex ,1
pop ecx
push newpivotindex ; push new pivot index
call QuickSort
push ebx ; the address of the arr
push ecx
mov ecx,[ebp-8]
mov newpivotindex, ecx
add newpivotindex ,1
pop ecx
push newpivotindex ; push new pivot index
push ecx ; push right
call QuickSort
DoneQsort:
pop ebx
pop edx
pop ecx
mov esp, ebp
pop ebp
ret 12
QuickSort ENDP
;------------------------------------------------------------
;This is the partion for collQuickSort
;This partion return the index of the new location of pivot
;input: Array, Left, Right, Pivot (16 Bytes)
;output: ax - the new index of pivot
;------------------------------------------------------------
PartitionColl PROC
push ebp
mov ebp, esp
sub esp, 8 ; ebp-4 for index_i, ebp-8 for index_j
push ecx
push edx
push ebx
mov edx, [ebp+20] ;address of Arr
mov ebx, [ebp+16] ;index Left
mov ecx, [ebp+12] ;index Right
mov eax, [ebp+8] ;pivot
;for relative location in arr
shl ebx, 1
add edx, ebx
shr ebx, 1
;for looping
sub ecx, ebx
inc ecx
mov dword ptr [ebp-4], 0 ; index_i
mov dword ptr [ebp-8], 0 ; index_j
sub dword ptr [ebp-4], 2
;moving to the right place in the arr and taking the arr[i]
shl eax, 1 ; shifing left becuase the arr is word arr
mov ax, word ptr [edx+eax]
push ax
push ax
mov count, 0
call Collatz
xor ebx, ebx
mov bx, ax
PloopColl:
mov esi, edx
add esi, dword ptr [ebp-8]
mov edi, 0 ; reset the var
mov di, word ptr [esi]
push di
mov count, 0
call Collatz
cmp ax, bx
jae continueLColl
add dword ptr [ebp-4], 2
;push eax and ecx for saving them before the swap
push ecx
;beacuse lea dosent work with vars - save ebx and use him to reach the right index
push ebx
mov ebx, dword ptr [ebp-4]
lea eax, [edx + [ebx]]
push eax
mov ebx, dword ptr [ebp-8]
lea ecx, [edx + [ebx]]
push ecx
call Swap
pop ebx
pop ecx
add dword ptr [ebp-8], 2
loop PloopColl
Jmp DonePartitionColl
continueLColl:
add dword ptr [ebp-8], 2
loop PloopColl
DonePartitionColl:
pop ax
add dword ptr [ebp-4], 2
xor ecx, ecx
xor ebx, ebx
mov ebx, dword ptr [ebp-4]
lea ecx, [edx + [ebx]]
push ecx
xor ecx, ecx
xor ebx, ebx
mov ebx, [ebp+16] ;index Left
mov ecx, [ebp+12] ;index Right
sub ecx, ebx
inc ecx
findPivotIndexColl:
shl ecx, 1
cmp ax, [edx+ecx]
je contineColl
shr ecx, 1
loop findPivotIndexColl
contineColl:
lea ecx, [edx + [ecx]]
push ecx
call Swap
mov edx, [ebp+20]
mov ecx, lengthof arr
findPivotIndexLastColl:
shl ecx, 1
cmp ax, [edx+ecx]
je DoneDoneColl
shr ecx, 1
loop findPivotIndexLastColl
DoneDoneColl:
shr ecx, 1
mov eax, ecx ; return the pivot index
pop ebx
pop edx
pop ecx
mov esp, ebp
pop ebp
ret 16
PartitionColl ENDP
;------------------------------------------------------------
;This is the coolQuickSort
;input: Array, Left, Right (12 Bytes)
;output: Nothing
;------------------------------------------------------------
CollQuickSortSecond PROC
push ebp
mov ebp ,esp
sub esp ,8 ; first 4 bytes for choose index, second 4 bytes for newpivotindex
push ecx
push edx
push ebx
mov ebx, [ebp + 16] ; Arr offset
mov edx, [ebp + 12] ; Left
mov ecx, [ebp +8] ;Right
cmp dx, cx
jge DoneCollQuickSortSecond
push ecx ; push right
push edx ; push Left
call ChooseIndex ; the return call is in ax
mov [ebp-4], eax
push ebx ; the address of the arr
push edx ; push left
push ecx ; push right
push [ebp-4] ; push pivot index that returned from Chooseindex
call partitionColl
mov [ebp-8], eax
push ebx ; the address of the arr
push edx ; push left
push ecx
mov ecx, [ebp-8]
mov newpivotindex, ecx
sub newpivotindex ,1
pop ecx
push newpivotindex ; push new pivot index
call collQuickSortSecond
push ebx ; the address of the arr
push ecx
mov ecx,[ebp-8]
mov newpivotindex, ecx
add newpivotindex ,1
pop ecx
push newpivotindex ; push new pivot index
push ecx ; push right
call collQuickSortSecond
DoneCollQuickSortSecond:
pop ebx
pop edx
pop ecx
mov esp, ebp
pop ebp
ret 12
CollQuickSortSecond ENDP
;------------------------------------------------------------
;This Swap function
;input: 2 places in array that we need to swap (8 Bytes)
;output: Nothing
;------------------------------------------------------------
SWAP PROC
push ebp
mov ebp, esp
sub esp, 4
push eax
push ebx
push edx
xor eax, eax
xor ebx, ebx
xor edx, edx
mov ebx, [ebp+12]
mov ax, WORD ptr [ebx]
mov word ptr [ebp-4], ax
mov edx, [ebp+8]
mov ax, WORD ptr [edx]
mov word ptr [ebx], ax
mov ax, word ptr [ebp-4]
mov word ptr [edx], ax
pop edx
pop ebx
pop eax
mov esp, ebp
pop ebp
ret 8
SWAP ENDP
END my_main