I have a function "swapByRef" that works in this code (this code just checks if the function swaps the values).
MODEL small
STACK 10h
DATA SEGMENT
a dw 12h
b dw 0A9h
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
start:
mov ax, DATA
mov ds, ax
push offset a ;push to the stack the adress of the variable a
push offset b ;push to the stack the adress of the variable b
call swapByRef
exit:
mov ax, 4c00h
int 21h
swapByRef proc
mov bp, sp
mov bx, [bp + 2]
mov ax, [bx]
mov si, [bp + 4]
mov cx, [si]
mov [bx], cx
mov[si], ax
ret 4
swapByRef endP
CODE ENDS
END start
But in my code (the bubble sort code) the procedure doesn't swap the values in the array and the array does not get sorted.
MODEL small
STACK 100h
DATA SEGMENT
ARR dw 9,5,7,3,8
len dw 5
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
start:
mov ax, DATA
mov ds, ax
mov bx, offset ARR
sorting:
mov ax, len
dec ax
cmp bx, ax
je redo
mov ax, ARR[bx]
mov dx, ARR[bx+ 2]
cmp al, ah
jg swap
jmp continue
swap:
push offset [ARR + bx]
push offset [ARR + bx + 2]
call swapByRef
continue:
inc bx
jmp sorting
redo:
cmp len, 0
je exit
mov ax, len
dec ax
mov len, ax
xor bl, bl
jmp sorting
exit:
mov ax, 4c00h
int 21h
swapByRef proc
mov bp, sp
mov bx, [bp + 2]
mov ax, [bx]
mov si, [bp + 4]
mov cx, [si]
mov [bx], cx
mov[si], ax
ret 4
swapByRef endP
CODE ENDS
END start
I've tried to debug and still I couldn't find the problem in my code...
Any help will be awesome, thanks.