bubblesort.asm ;Bubble sort is a simple sorting algorithm. It by cometjunkie50


									bubblesort.asm ;Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through ;the list to be sorted, comparing two items at a time and swapping them if they are ;in the wrong order. The pass through the list is repeated until no swaps are needed, ;which means the list is sorted. The algorithm gets its name from the way smaller ;elements "bubble" to the top (i.e. the beginning) of the list via the swaps. ; ; ; ; ; ; (BSORT.ASM) Ascending Bubble Sort Program By A. George Created: 2/2/02 Modified: 2/4/02 (fixed typos in comment field) This program contains a subroutine for ascending bubble sort along with test code to demonstrate that it works correctly. EQU EQU EQU ORG DC.B EQU ORG lds ldx ldaa jsr bgnd ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; $4000 $6000 $8000 ; start of data in RAM ; start of code in RAM ; start of stack at end of RAM ; table for use in testing BSORT subroutine ; length of table


DATA 9,1,7,2,4,3,8,6,5,-1 *-TABLE CODE #STACK #TABLE #LEN BSORT ; ; ; ; ;

initialize stack pointer set pointer to start of table set length call ascending bubble sort end of program

BSORT:Subroutine to bubble sort a table of signed bytes in ascending order. Input Parameters: X contains address of table, A contains length Output Parameters: none The logic of this module in pseudocode is as follows: for i = LEN-1 downto 1 for j = 1 upto i get value in table (table[j]) compare with following value (table[j+1]) if GT then swap contents endif endfor endfor Notes: Register Register Register Register A B X Y will will will will be be be be used used used used for outer loop counter (i) and for compares/swaps for inner loop counter (j) as pointer for accessing the table to hold copy of starting address of table


deca tfr psha tfr tfr


; set counter to initial loop count (N-1) ; save copy of start address ; ; ; ; outer loop of BSORT algorithm; will iterate N-1 times save outer loop counter set inner loop counter to value of outer loop counter restore pointer to start of table inner loop of BSORT algorithm; will iterate N-1, then N-2, ... load table[j] compare with table[j+1] if table[j]<=table[j+1], already in order so skip copy table[j+1] to table[j] save table[j] value to table[j+1] increment pointer decrement inner loop counter and loop if not zero

a,b y,x 0,x 1,x SKIP 1,x,0,x 1,x

ILOOP ldaa cmpa ble movb staa inx dbne pula dbne rts


; ; ; ; ; ; ; b,ILOOP ;

; restore outer loop counter a,OLOOP ; decrement outer loop counter and loop if not zero ; end of subroutine BSORT Page 1

To top