-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathc06-03.asm
74 lines (51 loc) · 1.42 KB
/
c06-03.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
[org 0x100]
jmp start
data: dw 60, 55, 45, 50
swapflag: db 0
swap:
push ax ; -------------------------;
; push cx ; ------------------; ;
; ;
mov ax, [bx + si] ; ;
xchg ax, [bx + si + 2] ; ;
mov [bx + si], ax ; ;
; ;
dec cx ; ;
; do some storage here ; ;
; pop cx ; ------------------; ;
pop ax ; -------------------------;
ret
bubblesort:
push ax ; three new pushes
push cx
push si
dec cx
shl cx, 1
mainloop:
mov si, 0 ; use as array index
mov byte[swapflag], 0 ; reset swap flag for this iteration
innerloop:
mov ax, [bx + si]
cmp ax, [bx + si + 2]
jbe noswap
call swap ; another call here
mov byte[swapflag], 1
noswap:
add si, 2
cmp si, cx
jne innerloop
cmp byte[swap], 1
je mainloop
; pops in reverse order
pop si
pop cx
pop ax
ret ; notice this!!
start:
mov bx, data
mov cx, 4
; make a function call
call bubblesort
; data is now sorted!
mov ax, 0x4c00
int 0x21