Question
I'm currently dealing with lab that ask me to finish the SORT algorithm. Down here is the main skeleton of the code that is nearly
I'm currently dealing with lab that ask me to finish the SORT algorithm. Down here is the main skeleton of the code that is nearly done.
What needed to be done is the SORT algorithm that is bolded down below. There is also a hint but Im completely lost. Please help me coding thepart based on what i've done up there. Explanation will be high appreciated.
;Memory allocation of arrays must be done prior to the RESET & StopWDT
ARY1 .set 0x0200 ;Memory allocation ARY1
ARY1S .set 0x0210
ARY2 .set 0x0220
ARY2S .set 0x0230
clr R4
clr R5
clr R6 ;
SORT1 mov.w #ARY1, R4 ;Initialize R4 to point to ARY1 in the memory
mov.w #ARY1S, R6
call #ArraySetup1 ;Create elements are store them in ARY1
call #COPY
call #SORT
SORT2 mov.w #ARY2, R4
mov.w #ARY2S, R6
call #ArraySetup2
call #COPY
call #SORT
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 ;store the rest of the 10 elements 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 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.
;Hints:
;Your sorting algorithm starts with R6 as a pointer to the array
;you need to save n (number of elements) in R8, then decrement it by 1 (n-
1) to become the number of comparisons.
;Copy R6 to R7 so you keep R6 unchanged as it points to the top of the
array for every new scan.
;Copy n-1 to R9 and use R9 as a loop counter, while keeping the current n-
1 value in R8 for the next scan.
;In the scan loop get an element and auto increment pointer R7, then get
next element without changing R7.
;Compare the two elements, if not in ascending order, swap them.
;Repeat the scan from the top as pointed to by (R6), and every time
decrement the number of comparisons (R8).
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