1

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       
Tuggs
  • 11
  • 2
  • Many problems with your code. Some examples: `LDS` instruction and `SP` are abused! Use only X and/or Y registers for indices to your data. Even if your data were placed on the stack you could first `TSX` and `TSY` and then use `X` and `Y`. You don't need to initialize `SP` every time around the loop as you don't change it inside the loop. You return (`RTS`) by pulling PC from where exactly, your array data? Obviously, you misunderstand how the CPU works. You exchange `A` and `B` and right after you load new values to both. So, why bother exchange if not used? – tonypdmtr Oct 15 '19 at 16:12

1 Answers1

0

First:

You have:

ldab sp      ; load byte into b where sp points (e.g. Samples[0]=0x12)
addb #1      ; add 1 to b  !-(   b goes from 0x12 to 0x13
ldaa b,sp    ; load byte into a where b+sp points (Samples[0x13]=???)!!
cba          ; compare b&a

What you want to do instead is something like:

...
ldab sp        ; load byte into b where sp points (e.g. Samples[0]=0x12)
ldaa 1,sp      ; load byte into a where sp+1 points (e.g. Samples[1]=0xCD)
cba            ; compare b&a
...

Second:

You test a vs. b (via cba) but don't do anything with the result of the comparison.  After the cba you need to conditionally branch around the swap code — skip around the two stores that accomplish the swap.  The way you choose to skip around determines whether your are sorting increasing or decreasing.


Third:

The swap you want to do is to store b where a came from (in the above) and store a where b came from (in the above).

You have:

 movb 0,sp , y
 staa 0,sp

And I think you want something more like (order for this one is unimportant):

staa sp        ; store a where b came from
stab 1,sp      ; store b where a came from

Fourth:

You are looping enough — as evidenced by the fact that the label loop is never referred to by a conditional branch instruction.  So, this code is incomplete.


why does my loop stop at 3

Not sure but it looks like your swap code is wiping out y, the loop counter.  Try my suggestions.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53