Question

In: Computer Science

Write an ARMv8 program to sort an array of elements. As Imentioned in class, this...

Write an ARMv8 program to sort an array of elements. As I mentioned in class, this problem uses a portion of your Programming Assignment 1 where you computed the smallest and largest values in an array. Here we will extend that assignment to find the “index” of the smallest and the index of the largest elements of the array. The following C code segment illustrates how we can sort the element of an array.

For this problem, assume an array with 8 elements and test your code with the array initialized to 17, 12, 3, 7, 31, 27, 5, 19 Submit: A file containing your ARMv8 code, a read me file (if needed), a snapshot of memory showing the values in the array before your program starts execution and a snapshot of memory showing the values in the array after the program completes execution. Optional: You can include additional snapshots to show how you are using Stack, or snapshots of the array elements in between the original values and final sorted values.

You can also test and show results with different array sizes and values.

i =0;

while (i < n)

{

find_smallest (&a, i, n, index); /*returns the index of next smallest element

swap (&a, i, index); /* swap smallest with a[i]

find_largest (&a, i, n, index); /* returns the index of next largest element

swap (&v, n, index); /* swap largest with a[n]

i = i+1;

n = n-1;

}

The three functions called by above C segments are shown next.

void find_smallest (long long int *a, long long int i, long long int n, long long int index)

{

long long int smallest = a[i];

index = i;

for (j = i; j largest)

{

largest = a[j];

index = j;

}

void swap (long long int *a, long long int i, long long int j)

{

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

Solutions

Expert Solution

i have code the folloing c

#include

int main()

{

int i, j, a, n, number[30];

n=8;

printf("Enter the 8 numbers \n");

for (i = 0; i < n; ++i)

scanf("%d", &number[i]);

for (i = 0; i < n; ++i)

{

for (j = i + 1; j < n; ++j)

{

if (number[i] > number[j])

{

a = number[i];

number[i] = number[j];

number[j] = a;

}

}

}

printf("The numbers arranged sortedorder are given below \n");

for (i = 0; i < n; ++i)

printf("%d\n", number[i]);

return 0;

}

armv8 code : program into Armv8 gcc 4.6.4

.LC0:

.ascii "Enter the 8 numbers \000"

.LC1:

.ascii "%d\000"

.LC2:

.ascii "The numbers arranged sortedorder are given below \000"

.LC3:

.ascii "%d\012\000"

main:

stmfd sp!, {fp, lr}

add fp, sp, #4

sub sp, sp, #136

mov r3, #8

str r3, [fp, #-16]

ldr r0, .L11

bl puts

mov r3, #0

str r3, [fp, #-8]

b .L2

.L3:

sub r2, fp, #140

ldr r3, [fp, #-8]

mov r3, r3, asl #2

add r3, r2, r3

ldr r0, .L11+4

mov r1, r3

bl scanf

ldr r3, [fp, #-8]

add r3, r3, #1

str r3, [fp, #-8]

.L2:

ldr r2, [fp, #-8]

ldr r3, [fp, #-16]

cmp r2, r3

movge r3, #0

movlt r3, #1

and r3, r3, #255

cmp r3, #0

bne .L3

mov r3, #0

str r3, [fp, #-8]

b .L4

.L8:

ldr r3, [fp, #-8]

add r3, r3, #1

str r3, [fp, #-12]

b .L5

.L7:

ldr r2, [fp, #-8]

mvn r3, #135

mov r2, r2, asl #2

sub r0, fp, #4

add r2, r0, r2

add r3, r2, r3

ldr r2, [r3, #0]

ldr r1, [fp, #-12]

mvn r3, #135

mov r1, r1, asl #2

sub r0, fp, #4

add r1, r0, r1

add r3, r1, r3

ldr r3, [r3, #0]

cmp r2, r3

ble .L6

ldr r2, [fp, #-8]

mvn r3, #135

mov r2, r2, asl #2

sub r1, fp, #4

add r2, r1, r2

add r3, r2, r3

ldr r3, [r3, #0]

str r3, [fp, #-20]

ldr r2, [fp, #-12]

mvn r3, #135

mov r2, r2, asl #2

sub r0, fp, #4

add r2, r0, r2

add r3, r2, r3

ldr r2, [r3, #0]

ldr r1, [fp, #-8]

mvn r3, #135

mov r1, r1, asl #2

sub r0, fp, #4

add r1, r0, r1

add r3, r1, r3

str r2, [r3, #0]

ldr r2, [fp, #-12]

mvn r3, #135

mov r2, r2, asl #2

sub r1, fp, #4

add r2, r1, r2

add r3, r2, r3

ldr r2, [fp, #-20]

str r2, [r3, #0]

.L6:

ldr r3, [fp, #-12]

add r3, r3, #1

str r3, [fp, #-12]

.L5:

ldr r2, [fp, #-12]

ldr r3, [fp, #-16]

cmp r2, r3

movge r3, #0

movlt r3, #1

and r3, r3, #255

cmp r3, #0

bne .L7

ldr r3, [fp, #-8]

add r3, r3, #1

str r3, [fp, #-8]

.L4:

ldr r2, [fp, #-8]

ldr r3, [fp, #-16]

cmp r2, r3

movge r3, #0

movlt r3, #1

and r3, r3, #255

cmp r3, #0

bne .L8

ldr r0, .L11+8

bl puts

mov r3, #0

str r3, [fp, #-8]

b .L9

.L10:

ldr r2, [fp, #-8]

mvn r3, #135

mov r2, r2, asl #2

sub r0, fp, #4

add r2, r0, r2

add r3, r2, r3

ldr r3, [r3, #0]

ldr r0, .L11+12

mov r1, r3

bl printf

ldr r3, [fp, #-8]

add r3, r3, #1

str r3, [fp, #-8]

.L9:

ldr r2, [fp, #-8]

ldr r3, [fp, #-16]

cmp r2, r3

movge r3, #0

movlt r3, #1

and r3, r3, #255

cmp r3, #0

bne .L10

mov r3, #0

mov r0, r3

sub sp, fp, #4

ldmfd sp!, {fp, pc}

.L11:

.word .LC0

.word .LC1

.word .LC2

.word .LC3


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
/** * This program will sort an n by n array by the first value in...
/** * This program will sort an n by n array by the first value in each row. * Selection sort algorithm is modified to do the sorting. * For example: * <p/> * If the original array is: * 1 2 3 4 5 * 3 4 5 1 2 * 5 2 3 4 1 * 2 3 1 4 5 * 4 2 3 1 5 * <p/> * The array after sorting is: * 1 2...
Write a C program to show sum of 10 elements of array and show the average....
Write a C program to show sum of 10 elements of array and show the average. [10]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT