Bubble Sort Assembly code please help Complete this MSP430 a
Bubble Sort Assembly code please help
Complete this MSP430 assembly language program where the SORT1 section sets theR4/R5/R6 parameters, which are used by the COPY and SORT subroutines to copy and sortarray ARY1. R4 holds the starting address of the array. R5 holds the length of the array. R6 holdsthe starting location of the sorted array. COPY subroutine copies the contents of array ARY1
into ARY1S. SORT subroutine sorts the elements on ARY1S in place.
SORT2 section is similar to SORT1 above using same registers. Arrays are in decimal notation!
Sort Arrays in increasing order from lowest to highest value. Main Program: [6] for Program setup, and [10] for Sort Subroutine. Use these values for the array elements. ARY1: (17,75,-67,23,36,-7,44,8,-74,18), ARY2: (54,-4,-23,-19,-72,-7,36,62,0,39)
here is what i got so far i am working on the SORT part of it if any help please and thank you.
       clr R4 ;clearing all register being use is a good
        clr R5 ;programming practice
        clr R6
SORT1 mov.w #ARY1, R4 ;initialize R4 as a pointer to array1
        mov.w #ARY1S, R6 ;initialize R4 as a pointer to array1 sorted
        call #ArraySetup1;then call subroutine ArraySetup1
        call #COPY ;Copy elements from ARY1 to ARY1S space
        call #SORT ;Sort elements in ARAY1
 SORT2 mov.w #ARY2, R4 ;initialize R4 as a pointer to array2
        mov.w #ARY2S, R6 ;initialize R4 as a pointer to array2 sorted
        call #ArraySetup2;then call subroutine ArraySetup2
        call #COPY ;Copy elements from ARY2 to ARY2S space
        call #SORT ;Sort elements in ARAY2
Mainloop jmp Mainloop ;Infinite Loop
ArraySetup1    mov.b #10, 0(R4) ; Array element initialization Subroutine
                mov.b #17, 1(R4) ;First start with the number of elements
                mov.b #75, 2(R4) ;and then fill in the 10 elements.
                mov.b #-67, 3(R4)
                mov.b #23, 4(R4)
                mov.b #36, 5(R4)
                mov.b #-7, 6(R4)
                mov.b #44, 7(R4)
                mov.b #8, 8(R4)
                mov.b #-74, 9(R4)
                mov.b #18, 10(R4)
                ret
 ArraySetup2    mov.b #10, 0(R4) ;Define the number of elements in the array
                mov.b #54, 1(R4) ;store an element
                mov.b #-4, 2(R4) ;store an element
                mov.b #-23, 3(R4)
                mov.b #-19, 4(R4)
                mov.b #-72, 5(R4)
                mov.b #-7, 6(R4)
                mov.b #36, 7(R4)
                mov.b #62, 8(R4)
                mov.b #0, 9(R4)
                mov.b #39, 10(R4)
                ret
 COPY            mov.b 0(R4), R10 ;save n (number of elements) in R10
                inc.b R10       ;increment by 1 to account for the byte n to be copied as well
                mov.w R4, R5   ;copy R4 to R5 so we keep R4 unchanged for later use
                mov.w R6, R7   ;copy R6 to R7 so we keep R6 unchanged for later use
                ret
LP                mov.b @R5+, 0(R7) ;copy elements using R5/R7 as pointers
                inc.w R7
                dec R10
                jnz LP
                ret
;Sort the copy of Array saved in the allocated Array-Sorted space, while keeping original Array unchanged ;replace the following two lines with your actual sorting algorithm. ;Sort the copy of Array saved in the allocated Array-Sorted sapce, while keeping original Array unchanged SORT nop
ret
;To bubble sort, you need to scan the array n-1 times, ;In every scan, you compare from top down each two consecutive elements, and you swap them if they are not in ascending order. ;Notice that in the first scan you get the largest element (no matter where it is in the array) pushed all the way to the bottom. ;So your next scan should be n-1 iterations, and then n-2 and so on. ;So every time you come back to the top of the array for a new scan, your n (the number of comparisons) must be decremented by 1. ;In the last scan, you need only one comparison.
Solution
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;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.
;-------------------------------------------------------------------------------
.cdecls C,LIST,\"msp430.h\" ; Include device header file
;-------------------------------------------------------------------------------
.text ; Assemble into program memory
.retain ; Override ELF conditional linking
; and retain current section
.retainrefs ; Additionally retain any sections
;-------------------------------------------------------------------------------
AR1 .SET 0x0200 ;set array memory locations
AR1S .SET 0x0210
AR2 .SET 0x0220
AR2S .SET 0x0230
;-------------------------------------------------------------------------------
RESET mov.w #__STACK_END,SP ; Initialize stackpointer
StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer
;-------------------------------------------------------------------------------
CLR R4 ;clear registers
CLR R5
CLR R6
Sort1
mov.w #AR1, R4
mov.b @R4+, R5
mov.w #AR1S, R6
call #Sort
Sort2
mov.w #AR2, R4
mov.b @R4+, R5
mov.w #AR2S, R6
call #Sort
SORT ;sort algorithm
clr R7
clr R8
clr R9
clr R10
clr R11
clr R12
clr R13
clr R14
clr R14
clr R15
clr r10
mov.b R5, R11 ;counter
dec R11
mov.b @R4+, R7
mov.b @R4, R8
cmp.b R7,R8
* jz No_Swap
* jge No_Swap
mov.b R7, R9
mov.b R8, R7
mov.b R9, R8
inc R10
mov.b r8, 0(r4)
mov.b r7, -1(r4)
dec R11
tst R11
* jnz Swap
cmp.w #0, R10 ; swap counter
* jnz Swap
ret
Mainloop jmp Mainloop
ARRAY1SET
mov.b #10, 0(R4)
mov.b #20, 1(R4)
mov.b #89, 2(R4)
mov.b #-5, 3(R4)
mov.b #13, 4(R4)
mov.b #63, 5(R4)
mov.b #-1, 6(R4)
mov.b #88, 7(R4)
mov.b #2, 8(R4)
mov.b #-88, 9(R4)
mov.b #1, 10(R4)
ARRAY2SET
mov.b #10, 0(R4)
mov.b #90, 1(R4)
** mov.b #-10 2(R4)
mov.b #-45, 3(R4)
mov.b #25, 4(R4)
mov.b #-46, 5(R4)
mov.b #-8, 6(R4)
mov.b #99, 7(R4)
mov.b #20, 8(R4)
mov.b #0, 9(R4)
mov.b #-64, 10(R4)
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
; Stack Pointer definition
;-------------------------------------------------------------------------------
.global __STACK_END
.sect .stack
;-------------------------------------------------------------------------------
; Interrupt Vectors
;-------------------------------------------------------------------------------
.sect \".reset\" ; MSP430 RESET Vector
.short RESET





