Assembly Merge Sort RISC-V Assignment Answers
Your question:
write a risc-v assembly code to implement mergesort ,finish following code,store 1st test in 0x10001000 and 2nd in 0x10002000 3rd in 0x10003000
plz dont use chatgpt completely.
.data
test1: .word
16,2,4,16,4,10,12,2,14,8,4,14,6,4,2,10,12,6,10,2,14,14,6,8,16,8,16,6,12,10,8,123
test2: .word
470,405,225,197,126,122,56,33,-81,-275,-379,-409,-416,-496,-500
test3: .word
412,-474,443,171,-23,247,221,7,40,221,-90,61,-9,49,-80,-80,221,-379,-161,-397,-173,276,-197,221,-12,-145,101
TEST1_SIZE: .word 32
TEST2_SIZE: .word 15
TEST3_SIZE: .word 27
Assignment Help Answers with Step-by-Step Explanation:
test3: .word 412,-474,443,171,-23,247,221,7,40,221,-90,61,-9,49,-80,-80,221,-379,-161,-397,-173,276,-197,221,-12,-145,101
TEST1_SIZE: .word 32
li ra, -1
# MergeSort function
# Calculate the midpoint
srai t2, t1, 1
# Recursively sort the first half
addi sp, sp, -8
# Recursively sort the second half
addi sp, sp, -8
# Merge the two sorted halves
lw t4, 0(t0) # Load the address of the first half
mergeLoop:
bge t7, t3, secondHalfDone # If the first half is exhausted, copy the second half
j copySecond
copyFirst:
sw t10, 0(t6) # Copy the element from the second half
addi t8, t8, 4 # Move the pointer in the second half
secondHalfDone:
jal copyRestOfFirstHalf # Copy the remaining elements from the first half
beqz t7, done # If the merged array is empty, return
bge t8, t7, done # Check if the array is already sorted
copyRestOfFirstHalf:
lw t9, 0(t7) # Load an element from the first half
copyRestOfSecondHalf:
lw t10, 0(t8) # Load an element from the second half
end:
ret
la t0, test1
lw t1, TEST1_SIZE
addi sp, sp, 8
# Sort the second array
sw t1, 4(sp)
jal mergeSort
addi sp, sp, -8
sw t0, 0(sp)
j done
ret:


