Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;----------------------------------------------------------------------------------------------------
- ;----------------------------------------------------------------------------------------------------
- ;Program to sort an array of number using the Bubble Sort algorithm in ascending order.
- ;----------------------------------------------------------------------------------------------------
- ;Input Location: 9000h
- ;Output Location: 9000h itself
- ;Number of elementS: 010h
- ;In the bubble sort algorithm, adjacent elements are compared and sorted with respect to each other.
- ;This process is done repeatedly starting from the first element everytime till the array is sorted.
- ;Here's an example. The numbers in the array currently being compared are enclosed in quotes:
- ;Given array: (5,1,8,2)
- ;First Pass:
- ; ("5,3",8,2) -> ("3,5",8,2) -Swap 5 and 3 since 5 is greater.
- ; (3,"5,8",2) -> (3,"5,8",2) -5 and 8 are in order so don't swap them. Move on.
- ; (3,5,"8,2") -> (3,5,"2,8") -8 is greater - swap it's position with 2.
- ;Second Pass:
- ; ("3,5",2,8) -> ("3,5",2,8) -3 and 5 are in order. Do nothing and move on.
- ; (3,"5,2",8) -> (3,"2,5",8) -5 is greater swap 2's and 5's positions.
- ;Third Pass:
- ; ("2,3",5,8) -> ("2,3",5,8) -3 is greater so swap.
- ;This sorts the array as (2,3,5,8) which is in ascending order. Notice that the 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 00h
- ;-----------------------------------Data Required for the Program------------------------------------
- MOV R0, #0Fh ;Counter for LOOP1
- ;----------------------------Sort in Ascending Order Using Bubble Sort-------------------------------
- LOOP1: ;Outer Loop - Handles number of passes
- MOV R7, #20h ;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.
- MOV A, R7 ;Copy a number of the array to the accumulator
- MOV R2, A ;and store it in R2.
- INC DPTR ;Move to the net number
- MOV A, R7 ;and store that in the accumulator.
- SUBB A, R2 ;Subtract the first from the second.
- JNC Continue2 ;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:
- MOV A, R7 ;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.
- MOV R7, 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
- MOV R7, A ;and store it in the first number's place.
- INC DPTR ;Move to the next memory location.
- Continue2: DJNZ R1, LOOP2 ;Move on to the next iteration of the current pass.
- Continue1: DJNZ R0, LOOP1 ;Move on to the next pass.
- Here: SJMP Here ;End of program - Loop here indefinitely.
- END
- ;----------------------------------------------------------------------------------------------------
- ;----------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement