Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with SORT section to go from lowest to highest. Complete this MSP430 a

ID: 3594380 • Letter: N

Question

Need help with SORT section to go from lowest to highest.

Complete this MSP430 assembly language program where the SORT1 section sets the R4/R5/R6 parameters, which are used by the COPY and SORT subroutines to copy and sort array ARY1. R4 holds the starting address of the array. R5 holds the length of the array. R6 holds the 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)

ARY1          .set   0x0200              ;Memory allocation ARY1

ARY1S        .set   0x0210              ;Memory allocation ARYS

ARY2          .set   0x0220              ;Memory allocation ARY2

ARY2S        .set   0x0230              ;Memory allocation AR2S

              clr    R4                  ;Clear Register

              clr    R5                  ;Clear Register

              clr    R6                  ;Clear Register

SORT1        mov.w #ARY1, R4            ;Initialize R4 to point to ARY1 in the memory

mov.w #ARY1S,      R6     ;Initialize R6 to point to ARY1S in the memory where the sorted ARY1 will be stored

              call   #ArraySetup1        ;Create elements are store them in ARY1

              call #COPY               ;Copy the elements from the ARY1 space to ARY1S space              call   #SORT               ;Calling Subroutine Sort with parameter passing in R4 and

R6

SORT2        mov.w #ARY2, R4            ;Initialize R4 to point to ARY2 in the memory

              mov.w #ARY2S,      R6     ;Initialize R6 to point to ARY2S in the memory where the sorted ARY2 will be stored

              call   #ArraySetup2        ;Create elements are store them in ARY2             

call #COPY               ;Copy the elements from the ARY2 space to ARY1S space

call   #SORT               ;Calling Subroutine Sort with parameter passing in R4 and R6

Mainloop     jmp    Mainloop                   ;loop in place for ever

;Array element initialization Subroutine

ArraySetup1 mov.b #10,   0(R4) ;Define the number of elements in the array

              mov.b #17, 1(R4) ;store an element

mov.b #75, 2(R4) ;store an element

mov.b #-67, 3(R4) ;store an element

mov.b #23, 4(R4) ;store an element  

mov.b #36, 5(R4) ;store an element

mov.b #-7, 6(R4) ;store an element

mov.b #44, 7(R4) ;store an element

mov.b #8,    8(R4) ;store an element

mov.b #-74, 9(R4) ;store an element

mov.b #18,   10(R4) ;store an element

              ret

;Array element initialization Subroutine

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) ;store an element

             mov.b #-19, 4(R4) ;store an element

             mov.b #-72, 5(R4) ;store an element

             mov.b #-7, 6(R4) ;store an element

             mov.b #36, 7(R4) ;store an element

             mov.b #62, 8(R4) ;store an element

             mov.b #0,    9(R4) ;store an element

             mov.b #39, 10(R4) ;store an element

              ret

;Copy original Array to allocated Array-Sorted space

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

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

              ret

Explanation / Answer

#include "msp430.h" ; #define controlled is use to include file
NAME main ; set the module name
PUBLIC main ; making main: label visible for all thwe outside modules
ORG 0FFFEh
DC16 init ; set and also reset vector to label 'init'
;******************************************
RSEG DATA16_I ; reserve memory in ram for initialized the values
array1 DS8 11
;ALIGNRAM 1 ; even, Zero will not be inserted

RSEG DATA16_ID ; inital value's (ram vars)
array1_I DC8 17,75,-67,23,36,-7,44,8,-74,18
;EVEN ; if you put every 16 bit vars at first then no need of that
RSEG CSTACK ; pre-declaration - segment
;**********************
RSEG CODE ; place the program at 'CODE'(segment)
init: mov.w #SFE(CSTACK), SP ; set up the stack (SFE= end of)
  
clr.w R12 ; initalizing values will copy here
jmp _test
_loop1 mov.b SFB(DATA16_ID)(R12), SFB(DATA16_I)(R12)
inc.w R12
_test cmp.w #SIZEOF(DATA16_I),R12
jlo _loop1
  
;***********************
main: mov.w #WDTPW+WDTHOLD,WDTCTL ; Stop(watchdog) timer  
mov.w #10,R12 ; size-1(array)
outer_l clr.w R11
inner_l cmp.b array1(R11),array1+1(R11)
jge keepit ; signed Jump(if it will greater or equal)
mov.b array1+1(R11),R13
mov.b array1(R11),array1+1(R11); (swap all the bytes)
mov.b R13,array1(R11)
keepit inc.w R11   
cmp.w R12,R11 ; inner loop will shrink with every turn
jlo inner_l
dec.w R12   
jnz outer_l
JMP $ ; it will jump at current location '$'
; loop(endless)
END


*** BUBBLE SORT PROCESS ***