asked 209k views
2 votes
Write an ARMv8 procedure which returns the address of the sorted-order position of the first element in an array of unique, 64-bit integers. The procedure takes two parameters: the base address of the unsorted array and the number of elements in the array, in that order. Hint: You don't need to sort the array--I wouldn't ask you to do something so complex on a test!--you only need to find the position where the element would go if the array were sorted. Maybe you can simply count something? We will not run your code. We do not expect perfection. If you have a working idea that shows that you understand how to write code in assembly, you'll get most, if not all, of the credit for this problem. You may use the emulator, but if you do, don't let it make you spend too much time on this problem. Example: If the first parameter is 0x100 (256), the second parameter is 0x8 (8), and the 8 64-bit integers that begin at address 0x100 are 4,2,6,7,0,1,3,5, your method should return 0x120 (288), since the leading 4 belongs in index 4.

asked
User Rassar
by
7.9k points

1 Answer

3 votes

Answer:

1. Define the procedure, specifying the required parameters: the base address of the unsorted array and the number of elements in the array.

2. Set up the necessary registers to store the base address and the number of elements. For example, you can use x0 for the base address and x1 for the number of elements.

3. Initialize a counter variable to keep track of the sorted position. Let's use x2 as the counter register and initialize it to 0.

4. Use a loop to iterate through each element of the array. Start the loop with a comparison to the number of elements, and decrement the counter after each iteration. Here's an example loop structure:

loop:

// Load the current element into a register for comparison

ldr x3, [x0], #8

// Compare the current element with the first element in the sorted position

ldr x4, [x0]

cmp x3, x4

// Branch to the end if the element belongs in the sorted position

b.le end

// Increment the counter if the element is greater

add x2, x2, #1

// Repeat the loop for the next element

subs x1, x1, #1

bne loop

5. At the end of the loop, branch to the end label. This label should be outside the loop, immediately after the loop structure.

end:

// Multiply the counter by 8 (the size of a 64-bit integer) to get the offset in bytes

lsl x2, x2, #3

// Add the offset to the base address to get the address of the sorted position

add x0, x0, x2

// Return the address

ret

Step-by-step explanation:

answered
User Semone
by
7.7k points