Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;----------------------------------------------------------------------------------------------------
  2. ;----------------------------------------------------------------------------------------------------
  3. ;Program to sort an array of number using the Bubble Sort algorithm in ascending order.
  4. ;----------------------------------------------------------------------------------------------------
  5. ;Input Location: 9000h
  6. ;Output Location: 9000h itself
  7. ;Number of elementS: 010h
  8. ;In the bubble sort algorithm, adjacent elements are compared and sorted with respect to each other.
  9. ;This process is done repeatedly starting from the first element everytime till the array is sorted.
  10. ;Here's an example. The numbers in the array currently being compared are enclosed in quotes:
  11. ;Given array: (5,1,8,2)
  12. ;First Pass:
  13. ;           ("5,3",8,2) -> ("3,5",8,2)  -Swap 5 and 3 since 5 is greater.
  14. ;           (3,"5,8",2) -> (3,"5,8",2)  -5 and 8 are in order so don't swap them. Move on.
  15. ;           (3,5,"8,2") -> (3,5,"2,8")  -8 is greater - swap it's position with 2.
  16. ;Second Pass:
  17. ;           ("3,5",2,8) -> ("3,5",2,8)  -3 and 5 are in order. Do nothing and move on.
  18. ;           (3,"5,2",8) -> (3,"2,5",8)  -5 is greater swap 2's and 5's positions.
  19. ;Third Pass:
  20. ;           ("2,3",5,8) -> ("2,3",5,8)  -3 is greater so swap.
  21. ;This sorts the array as (2,3,5,8) which is in ascending order. Notice that the number of passes is
  22. ;one less than the number of elements in the array and number of iterations in each pass is the
  23. ;total number of elements minus the current pass number.
  24. ;----------------------------------------------------------------------------------------------------
  25. ;----------------------------------------------------------------------------------------------------
  26.  
  27. ORG 00h
  28. ;-----------------------------------Data Required for the Program------------------------------------
  29. MOV R0, #0Fh                    ;Counter for LOOP1
  30. ;----------------------------Sort in Ascending Order Using Bubble Sort-------------------------------
  31. LOOP1:                          ;Outer Loop - Handles number of passes
  32.  
  33.     MOV R7, #20h            ;Point to beginning of array
  34.     MOV A, R0                   ;Initialize R1 - the counter for LOOP2
  35.     MOV R1, A                   ;to the value of R0 - the number of iterations in each pass is same
  36.                                 ;as the number of elements minus serial number of the current pass.
  37.  
  38.     LOOP2:                      ;Inner Loop - Handles each pass.
  39.  
  40.         MOV A, R7           ;Copy a number of the array to the accumulator
  41.         MOV R2, A               ;and store it in R2.
  42.         INC DPTR                ;Move to the net number
  43.         MOV A, R7           ;and store that in the accumulator.
  44.  
  45.         SUBB A, R2              ;Subtract the first from the second.
  46.  
  47.         JNC Continue2           ;If no carry is generated the second is greater and the numbers are
  48.                                 ;in order with respect to each other. Otherwise they must be swapped.
  49.         SwapNumbers:
  50.             MOV A, R7       ;Move the second number to the accumulator.
  51.             XCH A, R2           ;Exchange contents of the accumulator and R2. This makes A contain
  52.                                 ;the first number and R2 the second.
  53.             MOV R7, A       ;Store the first number at the place where the second one was stored.
  54.             DEC DPL             ;Move to the previous memory location.
  55.             MOV A, R2           ;Copy the second number to the accumulator
  56.             MOV R7, A       ;and store it in the first number's place.
  57.  
  58.             INC DPTR            ;Move to the next memory location.
  59.  
  60.     Continue2: DJNZ R1, LOOP2   ;Move on to the next iteration of the current pass.
  61.  
  62. Continue1: DJNZ R0, LOOP1       ;Move on to the next pass.
  63.  
  64. Here: SJMP Here     ;End of program - Loop here indefinitely.
  65. END
  66. ;----------------------------------------------------------------------------------------------------
  67. ;----------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement