Saturday, April 30, 2016

8051 Program – bubble sort ascending order

;sort an array using the Bubble Sort algorithm in ascending order.
;compare adjacent elements and sort with respect to each other.
;number of passes is one less than the number of elements in the array
;and number of iterations in each pass is the
;total number of elements minus the current pass number.
ORG 0H
ARRAY_ADDR EQU 4000H
NO_ELEMENTS EQU 08H
MAIN:
MOV DPTR, #ARRAY_ADDR
MOV A, #02H
MOVX @DPTR, A
INC DPTR
MOV A, #13H
MOVX @DPTR, A
INC DPTR
MOV A, #0A4H
MOVX @DPTR, A
INC DPTR
MOV A, #24H
MOVX @DPTR, A
INC DPTR
MOV A, #76H
MOVX @DPTR, A
INC DPTR
MOV A, #43H
MOVX @DPTR, A
INC DPTR
MOV A, #0BCH
MOVX @DPTR, A
INC DPTR
MOV A, #37H
MOVX @DPTR, A
LCALL SORT
SJMP MAIN
SORT:
MOV R0, #NO_ELEMENTS ;Counter for LOOP1
loop1: ;Outer Loop - Handles number of passes
MOV DPTR, #ARRAY_ADDR ;Point to beginning of array
MOV A, R0 ;Initialize R1 - the counter for LOOP2
MOV R1, A ;to the value of R0 - the number of iterations in each pass is same
;as the number of elements minus serial number of the current pass.
loop2: ;Inner Loop - Handles each pass.
MOVX A, @DPTR ;Copy a number of the array to the accumulator
MOV R2, A ;and store it in R2.
INC DPTR ;Move to the net number
MOVX A, @DPTR ;and store that in the accumulator.
SUBB A, R2 ;Subtract the first from the second.
JNC iteration ;If no carry is generated the second is greater and the numbers are
;in order with respect to each other. Otherwise they must be swapped.
swapnumbers:
MOVX A, @DPTR ;Move the second number to the accumulator.
XCH A, R2 ;Exchange contents of the accumulator and R2. This makes A contain
;the first number and R2 the second.
MOVX @DPTR, A ;Store the first number at the place where the second one was stored.
DEC DPL ;Move to the previous memory location.
MOV A, R2 ;Copy the second number to the accumulator
MOVX @DPTR, A ;and store it in the first number's place.
INC DPTR ;Move to the next memory location.
iteration: DJNZ R1, loop2 ;Move on to the next iteration of the current pass.
pass: DJNZ R0, loop1 ;Move on to the next pass.
RET
END

; bubble sort
; external memory
ORG 0H
START :
MOV R1,#10H ;Outer loop count
OUTLOOP: MOV R0,#10H ;Inner loop count
MOV DPTR,#9000H
INLOOP: CLR C ;Carry=0
MOVX A,@DPTR
MOV R2,A ;R2=first no
INC DPTR
MOVX A,@DPTR ;Acc=second no
MOV R3,A
SUBB A,R3 ;Compare
JNC SKIP
XCH A,R2 ;Exchange
SKIP: MOVX @DPTR,A;Big no -> mem
DEC 82H ;DPL=DPL-1
MOV A,R2
MOVX @DPTR,A ;Small no -> mem
INC DPTR
DEC R0
CJNE R0,#01H,INLOOP
DEC R1
CJNE R1,#01H,OUTLOOP
SJMP $
END

; bubble sort ascending order
; external memory
ORG 0H
MOV R0,#05
L1: MOV DPTR,#9000H
MOV A,R0
MOV R1,A
L2: MOVX A,@DPTR
MOV B,A
INC DPTR
MOVX A,@DPTR
CLR C
MOV R2,A
SUBB A,B
JNC NOEXCHG
MOV A,B
MOVX @DPTR,A
DEC DPL
MOV A,R2
MOVX @DPTR,A
INC DPTR
NOEXCHG:DJNZ R1,L2
DJNZ R0,L1
SJMP $
ORG 9000H
DB 2,5,1,9,7,6
END
; Result: 9000:01, 02, 05,06,07,09



Related topics:
8051 Program - sorting ascending order   |   8051 Program - sorting descending order   |   8051 Program - bubble sort descending order   |   8051 Program - pwm   |   8051 Program - decimal to hex   |   8051 Program - sum of a set of numbers   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment