I'm working on a program that uses bubble sort in order to sort a defined array in descending order. My code seems to loop 3 times, creating an incorrect result as well too, before stopping. This is in HCS12 assembly language.
RAMStart    EQU  $0800
ROMStart    EQU  $4000 
N           EQU  8
 ; absolute address to place my code/constant data
; variable/data section
            ORG RAMStart
 ; Insert here your data definition.
Samples   DC.B  $12,$CD,$0F,$3E,$E6,$1B,$F6,$9B
; code section
            ORG   ROMStart
Entry:
_Startup:
; code section
            ORG   ROMStart
loop
             ldx #N-1
             leay x
loop2
              lds #Samples
              tfr y,b
              tfr x,a
              sba
              exg a,b
              ldab sp
              addb #1
              ldaa b,sp
              cba 
              movb 0,sp , y
              staa 0,sp
              dbne y,loop2
            RTS