-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathc06-05.asm
92 lines (64 loc) · 2.02 KB
/
c06-05.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
[org 0x100]
jmp start
data: dw 60, 55
; swapflag: db 0 ; Globals are bad! Let's make this local.
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
sub sp, 2 ; make space on the stack, just below BP
; only if you want to do local variables
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
mov word [bp - 2], 0 ; has to be a word
innerloop:
mov ax, [bx + si]
cmp ax, [bx + si + 2]
jbe noswap
call swap ; another call here
; mov byte[swapflag], 1
mov word [bp - 2], 1
noswap:
add si, 2
cmp si, cx
jne innerloop
cmp word [bp - 2], 1
je mainloop
; handle parameter stack issue at end again -------------------
pop si
pop cx
pop bx
pop ax
mov sp, bp ; sp should be restored
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