Answered step by step
Verified Expert Solution
Question
1 Approved Answer
MSP 430 Bubble Sort Arrays (HOW TO IMPLEMENT BUBBLE SORTING IN THE SORT SUBROUTINE) ; MSP430 Assembler Code Template for use with TI Code Composer
MSP 430 Bubble Sort Arrays (HOW TO IMPLEMENT BUBBLE SORTING IN THE SORT SUBROUTINE) ; MSP430 Assembler Code Template for use with TI Code Composer Studio ; ; ;------------------------------------------------------------------------------- .cdecls C,LIST,"msp430.h" ; Include device header file ;------------------------------------------------------------------------------- .def RESET ; Export program entry-point to ; make it known to linker. ;------------------------------------------------------------------------------- .text ; Assemble into program memory. .retain ; Override ELF conditional linking ; and retain current section. .retainrefs ; And retain any sections that have ; references to current section. ;------------------------------------------------------------------------------- RESET mov.w #__STACK_END,SP ; Initialize stackpointer StopWDT mov.w #WDTPW|WDTHOLD,&WDTCTL ; Stop watchdog timer ;------------------------------------------------------------------------------- ; Main loop here ;------------------------------------------------------------------------------- ; "Allocate" memory for input and sorted arrays. Note the arrays are 16 bits ; apart in memory, giving ample room for the 10 values you need to sort plus the ; size. ARY1 .set 0x0200 ; Label address 0x0200 as ARY1 ARY1S .set 0x0210 ; Label address 0x0210 as ARY1S ARY2 .set 0x0220 ; Label address 0x0220 as ARY2 ARY2S .set 0x0230 ; Label address 0x0230 as ARY2S ; Clear all registers before using them. This is good practice. clr R4 clr R5 clr R6 clr R7 clr R8 clr R10 clr R11 ; Clear any other registers you are using SORT1 mov.w #ARY1, R4 ; Initialize R4 as a pointer to ARY1 mov.w #ARY1S, R6 ; Initialize R6 as a pointer to ARY1S call #ArraySetup1 ; Call the ArraySetup1 subroutine call #COPY ; Copy elements from ARY1 to ARY1S call #SORT ; Sort elements in ARY1S SORT2 mov.w #ARY2, R4 ; Initialize R4 as a pointer to ARY2 mov.w #ARY2S, R6 ; Initialize R6 as a pointer to ARY2S call #ArraySetup2 ; Call the ArraySetup2 subroutine call #COPY ; Copy elements from ARY2 to ARY2S call #SORT ; Sort elements in ARY2S Mainloop jmp Mainloop ; Halt execution (Infinite loop) ; ARY1 element initialization subroutine ArraySetup1 mov.b #10, 0(R4) ; Define the number of elements mov.b #34, 1(R4) ; Store the first element mov.b #-18, 2(R4) ; Store the second element mov.b #87, 3(R4) ; Store the third element mov.b #-65, 4(R4) mov.b #28, 5(R4) mov.b #-15, 6(R4) mov.b #-49, 7(R4) mov.b #61, 8(R4) mov.b #-77, 9(R4) mov.b #45, 10(R4) ; Store the rest of the elements ret ; ARY2 element initialization subroutine ArraySetup2 mov.b #10, 0(R4) ; Define the number of elements mov.b #90, 1(R4) ; Store the first element mov.b #46, 2(R4) ; Store the second element mov.b #16, 3(R4) ; Store the third element mov.b #-55, 4(R4) mov.b #-39, 5(R4) mov.b #32, 6(R4) mov.b #38, 7(R4) mov.b #12, 8(R4) mov.b #54, 9(R4) mov.b #23, 10(R4) ; Store the rest of the elements ret ; Copy original array to allocated sorted space COPY mov.b 0(R4), R10 ; Save the number of elements in R10 inc.b R10 ; Increment by 1 to account for n mov.w R4, R5 ; Copy R4 to R5 so we maintain R4 mov.w R6, R7 ; Copy R6 to R7 so we maintain R6 LP mov.b @R5+, 0(R7) ; Copy elements using R5/R7 pointers inc.w R7 dec R10 jnz LP ret ; Sort the copy of the array saved in ARY#S space while maintaining the original ; array. Replace the following lines with your actual sorting algorithm. SORT nop ret ; You will implement the famous "bubble sort" algorithm. This involves scanning ; the input array n-1 times. In each scan, you compare two consecutive elements ; from the beginning to the end, swapping them if they are not in ascending ; order. Note that after the first scan is complete, the largest element will be ; pushed all the way to the end of the array. This means subsequent scans should ; only iterate n-1, n-2, etc. times. Every time you return to the beginning for ; a new scan, your n (number of elements at the very beginning of the array) ; must be decremented by 1. In the last scan, only one comparison is made. ; Hints: ; Your sorting algorithm starts with R6 pointing to the array space. You must ; save n in R8, then decrement it by 1 (n-1) to become the number of ; comparisons. Copy R6 to R7 so you keep R6 pointing to the top of the array for ; subsequent scans. Copy n-1 to R9 and use R9 as a loop counter while keeping ; n-1 in R8 for the next scan. In the scan loop, get an element and ; auto-increment R7 (@Rn+) then get the next element without changing R7. ; Compare the two elements and swap if they are not in ascending order. Repeat ; the scan from the front of the array (pointed to by R6), decrementing the ; number of comparisons each time (in R8) ;------------------------------------------------------------------------------- ; Stack Pointer definition ;------------------------------------------------------------------------------- .global __STACK_END .sect .stack ;------------------------------------------------------------------------------- ; Interrupt Vectors ;------------------------------------------------------------------------------- .sect ".reset" ; MSP430 RESET Vector .short RESET
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started