In: Computer Science
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;
}
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