Question: Write a procedure labeled merge: that will merge two sorted arrays in memory into a single sorted array. Arguments are as follows: x 0 :

Write a procedure labeled merge: that will merge two sorted arrays in memory into a single sorted array. Arguments are as follows:
x0: length of first array
x1: address of first element of the first array
x2: length of the second array
x3: address of first element of the second array
x4: address at which to write the sorted array. Guaranteed to be able to hold x0+x2 elements.
Note: all elements are unsigned 64-bit values. Sorting is from smallest to largest.
Hints:
You should write this function first in Python or C to figure out the process before trying to write it in assembly.
It's a bit easier to write assembly loops that have the index count down.
x1(and x3, and x4) is not just the base of an array, it's also a pointer to the first address in the array. "add x1,x1,8" will change the pointer to the next location in the array if your code doesn't need to "remember" the base.
Don't forget that you may run out of values in one or the other of the lists, and you'll need to continue copying the rest of the other list. Feel free to use code you've figured out from the previous question.
My code:merge:
MOV x5, #0
MOV x6, #0
MOV x7, x1
MOV x8, x3
MOV x9, x4
ADD x10, x0, x2
merge_loop:
CMP x5, x0
B.EQ arr2
CMP x6, x2
B.EQ arr1
LDR x11,[x7]
LDR x12,[x8]
CMP x11, x12
BLE arr1_sm
STR x12,[x9], #8
ADD x6, x6, #1
ADD x8, x8, #8
B merge_loop
arr1_sm:
STR x11,[x9], #8
ADD x5, x5, #1
ADD x7, x7, #8
B merge_loop
arr2:
SUBS x12, x2, x6
B.LE end
ADD x12, x12, #1
ADD x12, x12, x6, LSL #3
ADD x8, x8, x12
B cp_arr2
arr1:
SUBS x12, x0, x5
B.LE end
ADD x12, x12, #1
ADD x12, x12, x5, LSL #3
ADD x7, x7, x12
cp_arr1:
LDR x11,[x7], #-8
STR x11,[x9], #8
SUBS x12, x12, #8
B.NE cp_arr1
cp_arr2:
LDR x11,[x8], #-8
STR x11,[x9], #8
SUBS x12, x12, #8
B.NE cp_arr2
end:
RET
Test:
array0=[1,3,7]
array1=[9,10,11,12,13]
Expected:
[1,3,7,9,10,11,12,13]
Got:
***Run error***
Traceback (most recent call last):
File "__tester__.python3", line 107, in
armsim.run()
File "/usr/local/lib/python3.6/site-packages/armsim.py", line 1268, in run
execute(line)
File "/usr/local/lib/python3.6/site-packages/armsim.py", line 1101, in execute
raise ValueError("Unsupported instruction or syntax error: "+line)
ValueError: Unsupported instruction or syntax error: add x12,x12,x6,lsl 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!