Part Three You are to write an assembly language program that sorts a list of 32
ID: 3746260 • Letter: P
Question
Part Three You are to write an assembly language program that sorts a list of 32-bit numbers into descending order. The first entry in the list gives the number of data elements to be sorted, and the rest of the list provides the data. The list of data must be sorted "in place", meaning that you are not allowed to create a copy in memory of the list to do the sorting. The list can be defined as part of the data for your assembly language program as follows: List: .word 10, 0, 1, 2, 3, 4, 5, 6, 7, 8,9 Test your algorithm with various data sets and ensure that the list of data is properly sorted in-place in the memory. A good debugging technique for this code is to use the Memory tab in the Monitor Program to view the contents of the list as the sorting algorithm progresses. Each time a breakpoint is reached by the processor (or an instruction is single-stepped), the list can be examined to see how the items are being swappedExplanation / Answer
ARM PROGRAM :-
/*
R4 address of numbers to sort
R5 current number to be compared
R6 offset index for outer loop through numbers
R7 offset index for inner loop
R8 current largest identified value
R9 current offset index of next uncompared value
*/
.global main
main:
push {ip, lr}
MOV R6, #0 @outerloop offset to numbers to be sorted
MOV R7, #0 @innerloop offers to number to be sorted
MOV R9, #0 @init value for index to next uncompared value
LDR R4, =List
LDR R1, [R4,R6]
MOV R1, R1, LSL #2
SUB R1, R1, #4
outerLoop:
MOV R8, #0 @reset Large default for next loop comparison
MOV R7,R6 @copy outerloop offset to next starting offset for the innerloop
innerLoop:
LDR R4, =List @load addr of List to compare to R4
LDR R5,[R4,R7] @load current num to R5 from R4 with offset R7
MOV R1,R5 @move num for output
CMP R5,R8 @is current > Largest so far
BGT swapLargest @if true, swap Largest to current first position then continue
continue:
CMP R7, R1 @ 0 plus 4*4bytes for 5 entries in array
ADD R7, R7,#4 @inc offset by 4 bytes
BLT innerLoop
continueOuterLoop:
CMP R6, R1 @check if we've looped through all values
ADD R6, R6, #4
BLT outerLoop @if not, branch back to start of outer loop
_exit:
POP {ip, lr}
doExit:
MOV R1, #0
MOV R7, #1
SWI 0
swapLargest:
MOV R8,R5 @keep copy of largest in the current loop
LDR R10, [R4,R6] @tmp copy first position to R10
LDR R11, [R4,R7] @tmp copy value in position currently being compared
STR R10, [R4, +R7] @swap first position value to current position being compared
STR R11, [R4, +R6] @swap the current largest value into the current first position
BX lr @return
.data
List:
.word 10,0,1,2,3,4,5,6,7,8,9