-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc06-04.asm
83 lines (59 loc) · 1.7 KB
/
c06-04.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
[org 0x100]
jmp start
data: dw 60, 55
swapflag: db 0
swap:
push ax ; -----------------------;
;
mov ax, [bx + si] ;
xchg ax, [bx + si + 2] ;
mov [bx + si], ax ;
;
pop ax ; -----------------------;
ret
bubblesort:
; handle stack issue for parameters -------------
push bp
mov bp, sp
push ax
push bx
push cx
push si
mov bx, [bp + 6] ; address of data to sort
mov cx, [bp + 4] ; number of elements to sort
; same old code from here -----------------------
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
; handle parameter stack issue at end again -------------------
pop si
pop cx
pop bx ; check removal
; pop ax
pop bp ; bp was the first thing pushed, so last popped!
; stack cleared? ----------------------------------------------
ret 4 ; what is this guy?
start:
mov bx, data
mov cx, 2
push bx
push cx
; make a function call
call bubblesort
; data is now sorted!
mov ax, 0x4c00
int 0x21