ideal
p286n
model tiny
codeseg
org 100h
jmp start
my_arr db 5, 2, 8, 9, 1, 7, 3, 0, 4, 6
; ----------------------------
; | bubble_sort
; | sort an array whose pointer pointed by arr_idx
; | modifies:
; | none
; | algorithm outline: (when size = 10)
; | for i = 0 to 8
; | for j = i+1 to 9
; | if arr_idx[i]>arr_idx[j] then swap(arr_idx[i], arr_idx[j]);
; |
; | i is mapped to SI
; | j is mapped to DI
; | bx hold the base array index
; | ah, al is to aid processing
; | cx is to hold the size
; ----------------------------
proc bubble_sort arr_idx: word, size: word
pusha ; save all registers
mov bx, [arr_idx]
mov cx, [size]
dec cx ; because maximum index bound is always one less than
; the size (i.e. when size is 10, the index is 0..9, not 1..10)
sub si, si
sub di, di
@@loop_i:
mov di, si
inc di ; j = i + 1
@@loop_j:
mov ah, [bx+si] ; AH = arr_idx[i]
mov al, [bx+di] ; AL = arr_idx[j]
cmp ah, al ; if (arr_idx[i] <= arr_idx[j]) then no swap
jle @@noswap
mov [bx+si], al ; else swap
mov [bx+di], ah
@@no_swap:
inc di ; (increase j)
cmp di, cx ; (compare with bound)
jbe @@loop_j ; if below or equal, then go to @@loop_j
; end of loop j
inc si ; increment i
cmp si, cx ; compare with bound
jb @@loop_i ; if below, then loop to @@loop_i
popa ; restore all registers
ret
endp
start:
mov dx, offset my_arr
call bubble_sort, dx, 10
; After this call, the array my_arr is sorted
mov ax, 4c00h
int 21h
end