How to bubble sort in assembly language for two arrays for M
How to bubble sort in assembly language for two arrays (for MSP430)?
;1. SORT1 sets R4/R5/R6 parameters used to SORT subroutine to sort ARY1
;2. R4-starting address of array
;3. R5-length of array
;4. R6-holds address where sorted array will be stored
;5. SORT2 will sort array ARY2 in the same way
;6. Sort Arrays in increasing order from lowest to highest value.
For ex: ARY1: (17,75,-67,23,36,-7,44,8,-74,18),
ARY2: (54,-4,-23,-19,-72,-7,36,62,0,39)
Solution
;Input Location: 9000h
;Output Location: 9000h
ARY1: (17,75,-67,23,36,-7,44,8,-74,18)
;First Pass:
(17,75,-67,23,36,-7,44,8,-74,18) -> (75,17,-67,23,36,-7,44,8,-74,18)
(75,17,-67,23,36,-7,44,8,-74,18) -> (75,17,-67,23,36,-7,44,8,-74,18)
(75,17,-67,23,36,-7,44,8,-74,18) -> (75,17,23,-67,36,-7,44,8,-74,18)
(75,17,23,-67,36,-7,44,8,-74,18) ->(75,17,23,36,-67,-7,44,8,-74,18)
….
……..
…..
Second pass:
Again check the whole data set
Third pass:
Again check the whole data set
…
….
We have to check up to nine pass:
Until we get:
(-74,-67,-7,8,17,18,23,36,44,75)
#same we can do for the second array also:
ARY2: (54,-4,-23,-19,-72,-7,36,62,0,39)
Program:
1.ORG 00h
2.MOV R0, #0Fh
3.LOOP1: ;Outer Loop - Handles number of passes
4.MOV DPTR, #9000h ;Point to beginning of array34
5.MOV A, R0 ;Initialize R1 - the counter for LOOP235
6.MOV R1, A ;to the value of R0 - the number of iterations in each pass is same
7.LOOP2: ;Inner Loop - Handles each pass.
8.MOVX A, @DPTR ;Copy a number of the array to the accumulator
9.MOV R2, A ;and store it in R2.
10.INC DPTR ;Move to the net number
11.MOVX A, @DPTR ;and store that in the accumulator.
12.SUBB A, R2 ;Subtract the first from the second.
13.JNC Continue2 ;If no carry is generated the second is greater and the numbers are
14.SwapNumbers:
15.MOVX A, @DPTR ;Move the second number to the accumulator.
16.XCH A, R2 ;Exchange contents of the accumulator and R2. This makes A contain
17.MOVX @DPTR, A ;Store the first number at the place where the second one was stored.
18.DEC DPL ;Move to the previous memory location.
19.MOV A, R2 ;Copy the second number to the accumulator
20.MOVX @DPTR, A ;and store it in the first number\'s place.
21.INC DPTR ;Move to the next memory location.
22.Continue2: DJNZ R1, LOOP2 ;Move on to the next iteration of the current pass.
23.Continue1: DJNZ R0, LOOP1 ;Move on to the next pass.
24.Here: SJMP Here ;End of program - Loop here indefinitely.
25.END

