# PICMicrocontollerSortingMethod

## Bubble sort by Regulus Berdin

```> Can anyone point me towards an example of code (asm, not c)
> that will sort  (re-arrange) a group of 10 or so numbers stored in
> consecutive RAM locations into ascending order?  Or descending
> order, doesn't really matter which.

Try this (untested):

cblock
d1,d2,d3,d4,d5,d6,d7,d8,d9,d10
tmp
i
endc

bubble:
movlw   9
movwf   i
movwf   FSR

loop1   movf    INDF,w  ;for i = 9 to 1 'd10 to d2
movwf   tmp     ;tmp = d[d1+i]

loop2   decf    FSR,f   ;for FSR = (d1+i-1) to d1

movf    tmp1,w  ;compare
subwf   INDF,w
skpc            ;change this to skpnc to change sort order
goto   noswap

swap    movf    tmp1,w
xorwf   INDF,f
xorwf   INDF,w
xorwf   INDF,f
movwf   tmp

noswap  movlw   d1
xorwf   FSR,w
skpz
goto   loop2

movlw   d1
movwf   FSR
movf    tmp,w
movwf   INDF    ;d[d1+i] = tmp
decf    FSR,f
decfsz  i,w
goto   loop1

return

;equivalent in C

for (i=10;i>1;i--) {
tmp=d[i];
for (k=i-1;i>0;k--)
if (d[k]>tmp) {
tmp2=tmp;
tmp=d[k];
d[k]=tmp2;
}
d[i]=tmp;
}

```

 file: /Techref/microchip/sort-rb.htm, 1KB, , updated: 2009/2/13 16:03, local time: 2024/9/11 17:09, TOP NEW HELP FIND:  44.192.26.226:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! PIC Microcontoller Sorting Method Bubble sort by Regulus Berdin

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.