I want to sort the following numbers using bubble sort in MASM611:
59H, 39H, 51H, 57H, 24H, 86H, 95H, 93H, 15H, 75H, 42H, 68H, 03H, 06H, 01H
The code I have written works well for all hexadecimal numbers less than 80. But if the array has even one number greater than 80H then it is placed in the beginning.
Can someone point out my mistake or give any solution to this weird behaviour. PS: I'm new to assembly.
Code:
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
ORG 1200H
ARRAY DB 59H, 39H, 51H, 57H, 24H, 86H, 95H, 93H, 15H, 75H, 42H, 68H, 03H, 06H, 01H
count equ 15
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV DX, COUNT-1
OLOOP:
MOV CX, COUNT-1
LEA SI, ARRAY
ILOOP:
MOV AL, [SI]
CMP AL, [SI+1]
JL KEEP
XCHG AL, [SI+1]
MOV [SI], AL
KEEP:
INC SI
LOOP ILOOP
DEC DX
JNZ OLOOP
HLT
CODE ENDS
END START
Debug:
When array has hexadecimal numbers greater than 80:
The numbers which are greater than 80H have been placed before 01H (but the three numbers 86H, 93H, 95H are still in ascending order).
When array have hexadecimal numbers less than 80:
When there are no hexadecimal numbers in the array the output is as expected.