-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.asm
More file actions
74 lines (67 loc) · 1.29 KB
/
SelectionSort.asm
File metadata and controls
74 lines (67 loc) · 1.29 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
IDEAL
MODEL small
STACK 100h
DATASEG
array db 12, -5, 0, 34, -10, 7
arrayLen dw 6
CODESEG
start:
mov ax, @data
mov ds, ax
push [arrayLen]
push offset array
call SelectionSort
add sp, 4
exit:
mov ax, 4c00h
int 21h
;receives length of the array and offset via stack
proc SelectionSort
push bp
mov bp, sp
mov dx, [bp+6] ;length of the array
mov di, [bp+4] ;offset of the array
mov cx, dx
dec cx
SortLoop:
mov ax, cx
inc ax
mov si, di
call FindMin
;swap:
mov ah, [byte ptr di]
mov [di+bx], ah
mov [di], al
inc di
Loop SortLoop
pop bp
ret
endp SelectionSort
;receives length of the array in ax and offset in si
;returns minimal value in ax and index of this value in bx
proc FindMin
push cx
push si
mov cx, ax
dec cx
xor ax, ax
mov al, [byte ptr si]
mov bx, si
FindLoop:
inc si
mov ah, [byte ptr si]
cmp ah, al
jl ChangeMin
Mark:
Loop FindLoop
cbw
pop dx ; dx = original si
sub bx, dx
pop cx
ret
ChangeMin:
mov al, ah
mov bx, si
jmp Mark
endp FindMin
END start