TRANSLATE THIS C CODE TO MIPS: #include <stdio.h> #include <stdlib.h> void swap(
ID: 3917721 • Letter: T
Question
TRANSLATE THIS C CODE TO MIPS:
#include <stdio.h>
#include <stdlib.h>
void swap(int array[], int i, int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
void bubble_sort(int array[], int n){
int i, j = 0,swapped = 1;
while (swapped)
{
swapped = 0;
for (i = 0; i < n - 1 - j; i++)
{
if (array[i] > array[i + 1])
{
swap(array, i, i + 1);
swapped = 1;
}
}
++j;
}
}
void bubble_sort_descending(int array[], int n){
int i, j = 0,swapped = 1;
while (swapped)
{
swapped = 0;
for (i = 0; i < n - 1 - j; i++)
{
if (array[i] < array[i + 1])
{
swap(array, i, i + 1);
swapped = 1;
}
}
++j;
}
}
void print_array(int array[], int n){
int i;
for (i = 0; i < n; i++)
printf("%d%c", array[i], (i == n - 1) ? ' ' : ' ');
}
int max_value(int array[], int n){
int i, max=array[0];
for(i=0;i<n;i++){
if(max < array[i]){
max=array[i];
}
}
return max;
}
int min_value(int array[], int n){
int i, min=array[0];
for(i=0;i<n;i++){
if(min > array[i]){
min=array[i];
}
}
return min;
}
float find_average(int array[],int n){
int i;
float average=0;
for(i=0;i<n;i++)
average += array[i];
average = (float) average/n;
return average;
}
void find_key(int array[],int n, int key){
int i,flag=0;
for(i=0;i<n;i++){
if(array[i]== key){
printf("The Key : %d was found in position %d of the array ",key,i);
flag=1;
}
}
if(flag==0){
printf("The key: %d entered is not in the array ",key);
}
}
int main()
{
int i=1,j=0,m=0,s=0,x=1, key;
printf("How many numbers would you like to enter ");
scanf("%d",&i);
int *array = NULL;
array = malloc(i * sizeof(int));
for(j=0;j<i;j++)
{
printf("Please enter a number (%d): ",j+1);
scanf("%d",&m);
array[j]= m;
}
while(x){
printf(" Enter 1 if you want sort in ascending order ");
printf("Enter 2 if you want to sort in descending order ");
printf("Enter 3 if you want the max value of the values entered ");
printf("Enter 4 if you want the min value of the values entered ");
printf("Enter 5 if you want to find the average of the numbers entered ");
printf("Enter 6 if you want to find the location of a key ");
printf("Enter 7 if you want to exit the program ");
printf(" ");
scanf("%d",&s);
printf(" ");
switch(s)
{
case 1:
bubble_sort(array,i);
print_array(array,i);
break;
case 2:
bubble_sort_descending(array,i);
print_array(array,i);
break;
case 3:
printf("The max value is: %d ",max_value(array,i));
break;
case 4:
printf("The min value is: %d ",min_value(array,i));
break;
case 5:
printf("The averages of the values entered is : %f ",find_average(array,i));
break;
case 6:
printf("Please enter the key that you want to find in the array ");
scanf("%d",&key);
find_key(array,i,key);
break;
case 7:
x=0;
break;
}
}
return 0;
}
Explanation / Answer
swap:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movl %edx, -32(%rbp)
movl -28(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %eax, -4(%rbp)
movl -32(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl -28(%rbp), %edx
movslq %edx, %rdx
leaq 0(,%rdx,4), %rcx
movq -24(%rbp), %rdx
addq %rcx, %rdx
movl (%rax), %eax
movl %eax, (%rdx)
movl -32(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rax, %rdx
movl -4(%rbp), %eax
movl %eax, (%rdx)
nop
popq %rbp
ret
bubble_sort:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movl $0, -8(%rbp)
movl $1, -12(%rbp)
jmp .L3
.L7:
movl $0, -12(%rbp)
movl $0, -4(%rbp)
jmp .L4
.L6:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %edx
movl -4(%rbp), %eax
cltq
addq $1, %rax
leaq 0(,%rax,4), %rcx
movq -24(%rbp), %rax
addq %rcx, %rax
movl (%rax), %eax
cmpl %eax, %edx
jle .L5
movl -4(%rbp), %eax
leal 1(%rax), %edx
movl -4(%rbp), %ecx
movq -24(%rbp), %rax
movl %ecx, %esi
movq %rax, %rdi
call swap
movl $1, -12(%rbp)
.L5:
addl $1, -4(%rbp)
.L4:
movl -28(%rbp), %eax
subl $1, %eax
subl -8(%rbp), %eax
cmpl %eax, -4(%rbp)
jl .L6
addl $1, -8(%rbp)
.L3:
cmpl $0, -12(%rbp)
jne .L7
nop
leave
ret
bubble_sort_descending:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movl $0, -8(%rbp)
movl $1, -12(%rbp)
jmp .L9
.L13:
movl $0, -12(%rbp)
movl $0, -4(%rbp)
jmp .L10
.L12:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %edx
movl -4(%rbp), %eax
cltq
addq $1, %rax
leaq 0(,%rax,4), %rcx
movq -24(%rbp), %rax
addq %rcx, %rax
movl (%rax), %eax
cmpl %eax, %edx
jge .L11
movl -4(%rbp), %eax
leal 1(%rax), %edx
movl -4(%rbp), %ecx
movq -24(%rbp), %rax
movl %ecx, %esi
movq %rax, %rdi
call swap
movl $1, -12(%rbp)
.L11:
addl $1, -4(%rbp)
.L10:
movl -28(%rbp), %eax
subl $1, %eax
subl -8(%rbp), %eax
cmpl %eax, -4(%rbp)
jl .L12
addl $1, -8(%rbp)
.L9:
cmpl $0, -12(%rbp)
jne .L13
nop
leave
ret
.LC0:
.string "%d%c"
print_array:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movl $0, -4(%rbp)
jmp .L15
.L18:
movl -28(%rbp), %eax
subl $1, %eax
cmpl %eax, -4(%rbp)
jne .L16
movl $10, %ecx
jmp .L17
.L16:
movl $32, %ecx
.L17:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %ecx, %edx
movl %eax, %esi
movl $.LC0, %edi
movl $0, %eax
call printf
addl $1, -4(%rbp)
.L15:
movl -4(%rbp), %eax
cmpl -28(%rbp), %eax
jl .L18
nop
leave
ret
max_value:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movq -24(%rbp), %rax
movl (%rax), %eax
movl %eax, -8(%rbp)
movl $0, -4(%rbp)
jmp .L20
.L22:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
cmpl %eax, -8(%rbp)
jge .L21
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %eax, -8(%rbp)
.L21:
addl $1, -4(%rbp)
.L20:
movl -4(%rbp), %eax
cmpl -28(%rbp), %eax
jl .L22
movl -8(%rbp), %eax
popq %rbp
ret
min_value:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movq -24(%rbp), %rax
movl (%rax), %eax
movl %eax, -8(%rbp)
movl $0, -4(%rbp)
jmp .L25
.L27:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
cmpl %eax, -8(%rbp)
jle .L26
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %eax, -8(%rbp)
.L26:
addl $1, -4(%rbp)
.L25:
movl -4(%rbp), %eax
cmpl -28(%rbp), %eax
jl .L27
movl -8(%rbp), %eax
popq %rbp
ret
find_average:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
pxor %xmm0, %xmm0
movss %xmm0, -8(%rbp)
movl $0, -4(%rbp)
jmp .L30
.L31:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
cvtsi2ss %eax, %xmm0
movss -8(%rbp), %xmm1
addss %xmm1, %xmm0
movss %xmm0, -8(%rbp)
addl $1, -4(%rbp)
.L30:
movl -4(%rbp), %eax
cmpl -28(%rbp), %eax
jl .L31
cvtsi2ss -28(%rbp), %xmm1
movss -8(%rbp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -8(%rbp)
movss -8(%rbp), %xmm0
popq %rbp
ret
.LC2:
.string "The Key : %d was found in position %d of the array "
.LC3:
.string "The key: %d entered is not in the array "
find_key:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movl %edx, -32(%rbp)
movl $0, -8(%rbp)
movl $0, -4(%rbp)
jmp .L34
.L36:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
cmpl %eax, -32(%rbp)
jne .L35
movl -4(%rbp), %edx
movl -32(%rbp), %eax
movl %eax, %esi
movl $.LC2, %edi
movl $0, %eax
call printf
movl $1, -8(%rbp)
.L35:
addl $1, -4(%rbp)
.L34:
movl -4(%rbp), %eax
cmpl -28(%rbp), %eax
jl .L36
cmpl $0, -8(%rbp)
jne .L38
movl -32(%rbp), %eax
movl %eax, %esi
movl $.LC3, %edi
movl $0, %eax
call printf
.L38:
nop
leave
ret
.LC4:
.string "How many numbers would you like to enter"
.LC5:
.string "%d"
.LC6:
.string "Please enter a number (%d): "
.LC7:
.string " Enter 1 if you want sort in ascending order"
.LC8:
.string "Enter 2 if you want to sort in descending order "
.LC9:
.string "Enter 3 if you want the max value of the values entered"
.LC10:
.string "Enter 4 if you want the min value of the values entered"
.LC11:
.string "Enter 5 if you want to find the average of the numbers entered"
.LC12:
.string "Enter 6 if you want to find the location of a key"
.LC13:
.string "Enter 7 if you want to exit the program"
.LC14:
.string "The max value is: %d "
.LC15:
.string "The min value is: %d "
.LC16:
.string "The averages of the values entered is : %f "
.LC17:
.string "Please enter the key that you want to find in the array"
main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl $1, -20(%rbp)
movl $0, -4(%rbp)
movl $0, -24(%rbp)
movl $0, -28(%rbp)
movl $1, -8(%rbp)
movl $.LC4, %edi
call puts
leaq -20(%rbp), %rax
movq %rax, %rsi
movl $.LC5, %edi
movl $0, %eax
call __isoc99_scanf
movq $0, -16(%rbp)
movl -20(%rbp), %eax
cltq
salq $2, %rax
movq %rax, %rdi
call malloc
movq %rax, -16(%rbp)
movl $0, -4(%rbp)
jmp .L40
.L41:
movl -4(%rbp), %eax
addl $1, %eax
movl %eax, %esi
movl $.LC6, %edi
movl $0, %eax
call printf
leaq -24(%rbp), %rax
movq %rax, %rsi
movl $.LC5, %edi
movl $0, %eax
call __isoc99_scanf
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -16(%rbp), %rax
addq %rax, %rdx
movl -24(%rbp), %eax
movl %eax, (%rdx)
addl $1, -4(%rbp)
.L40:
movl -20(%rbp), %eax
cmpl %eax, -4(%rbp)
jl .L41
jmp .L42
.L51:
movl $.LC7, %edi
call puts
movl $.LC8, %edi
call puts
movl $.LC9, %edi
call puts
movl $.LC10, %edi
call puts
movl $.LC11, %edi
call puts
movl $.LC12, %edi
call puts
movl $.LC13, %edi
call puts
movl $10, %edi
call putchar
leaq -28(%rbp), %rax
movq %rax, %rsi
movl $.LC5, %edi
movl $0, %eax
call __isoc99_scanf
movl $10, %edi
call putchar
movl -28(%rbp), %eax
cmpl $7, %eax
ja .L42
movl %eax, %eax
movq .L44(,%rax,8), %rax
jmp *%rax
.L44:
.quad .L42
.quad .L50
.quad .L49
.quad .L48
.quad .L47
.quad .L46
.quad .L45
.quad .L43
.L50:
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call bubble_sort
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call print_array
jmp .L42
.L49:
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call bubble_sort_descending
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call print_array
jmp .L42
.L48:
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call max_value
movl %eax, %esi
movl $.LC14, %edi
movl $0, %eax
call printf
jmp .L42
.L47:
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call min_value
movl %eax, %esi
movl $.LC15, %edi
movl $0, %eax
call printf
jmp .L42
.L46:
movl -20(%rbp), %edx
movq -16(%rbp), %rax
movl %edx, %esi
movq %rax, %rdi
call find_average
cvtss2sd %xmm0, %xmm0
movl $.LC16, %edi
movl $1, %eax
call printf
jmp .L42
.L45:
movl $.LC17, %edi
call puts
leaq -32(%rbp), %rax
movq %rax, %rsi
movl $.LC5, %edi
movl $0, %eax
call __isoc99_scanf
movl -32(%rbp), %edx
movl -20(%rbp), %ecx
movq -16(%rbp), %rax
movl %ecx, %esi
movq %rax, %rdi
call find_key
jmp .L42
.L43:
movl $0, -8(%rbp)
nop
.L42:
cmpl $0, -8(%rbp)
jne .L51
movl $0, %eax
leave
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.