Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you put this C code into MIPS: #include <stdio.h> #include <stdlib.h> void s

ID: 3917682 • Letter: C

Question

Can you put this C code into 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:

push rbp

mov rbp, rsp

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov DWORD PTR [rbp-32], edx

mov eax, DWORD PTR [rbp-28]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-4], eax

mov eax, DWORD PTR [rbp-32]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov edx, DWORD PTR [rbp-28]

movsx rdx, edx

lea rcx, [0+rdx*4]

mov rdx, QWORD PTR [rbp-24]

add rdx, rcx

mov eax, DWORD PTR [rax]

mov DWORD PTR [rdx], eax

mov eax, DWORD PTR [rbp-32]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rdx, rax

mov eax, DWORD PTR [rbp-4]

mov DWORD PTR [rdx], eax

nop

pop rbp

ret

bubble_sort:

push rbp

mov rbp, rsp

sub rsp, 32

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov DWORD PTR [rbp-8], 0

mov DWORD PTR [rbp-12], 1

jmp .L3

.L7:

mov DWORD PTR [rbp-12], 0

mov DWORD PTR [rbp-4], 0

jmp .L4

.L6:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov edx, DWORD PTR [rax]

mov eax, DWORD PTR [rbp-4]

cdqe

add rax, 1

lea rcx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rcx

mov eax, DWORD PTR [rax]

cmp edx, eax

jle .L5

mov eax, DWORD PTR [rbp-4]

lea edx, [rax+1]

mov ecx, DWORD PTR [rbp-4]

mov rax, QWORD PTR [rbp-24]

mov esi, ecx

mov rdi, rax

call swap

mov DWORD PTR [rbp-12], 1

.L5:

add DWORD PTR [rbp-4], 1

.L4:

mov eax, DWORD PTR [rbp-28]

sub eax, 1

sub eax, DWORD PTR [rbp-8]

cmp DWORD PTR [rbp-4], eax

jl .L6

add DWORD PTR [rbp-8], 1

.L3:

cmp DWORD PTR [rbp-12], 0

jne .L7

nop

leave

ret

bubble_sort_descending:

push rbp

mov rbp, rsp

sub rsp, 32

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov DWORD PTR [rbp-8], 0

mov DWORD PTR [rbp-12], 1

jmp .L9

.L13:

mov DWORD PTR [rbp-12], 0

mov DWORD PTR [rbp-4], 0

jmp .L10

.L12:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov edx, DWORD PTR [rax]

mov eax, DWORD PTR [rbp-4]

cdqe

add rax, 1

lea rcx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rcx

mov eax, DWORD PTR [rax]

cmp edx, eax

jge .L11

mov eax, DWORD PTR [rbp-4]

lea edx, [rax+1]

mov ecx, DWORD PTR [rbp-4]

mov rax, QWORD PTR [rbp-24]

mov esi, ecx

mov rdi, rax

call swap

mov DWORD PTR [rbp-12], 1

.L11:

add DWORD PTR [rbp-4], 1

.L10:

mov eax, DWORD PTR [rbp-28]

sub eax, 1

sub eax, DWORD PTR [rbp-8]

cmp DWORD PTR [rbp-4], eax

jl .L12

add DWORD PTR [rbp-8], 1

.L9:

cmp DWORD PTR [rbp-12], 0

jne .L13

nop

leave

ret

.LC0:

.string "%d%c"

print_array:

push rbp

mov rbp, rsp

sub rsp, 32

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov DWORD PTR [rbp-4], 0

jmp .L15

.L18:

mov eax, DWORD PTR [rbp-28]

sub eax, 1

cmp DWORD PTR [rbp-4], eax

jne .L16

mov ecx, 10

jmp .L17

.L16:

mov ecx, 32

.L17:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

mov edx, ecx

mov esi, eax

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call printf

add DWORD PTR [rbp-4], 1

.L15:

mov eax, DWORD PTR [rbp-4]

cmp eax, DWORD PTR [rbp-28]

jl .L18

nop

leave

ret

max_value:

push rbp

mov rbp, rsp

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov rax, QWORD PTR [rbp-24]

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-8], eax

mov DWORD PTR [rbp-4], 0

jmp .L20

.L22:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

cmp DWORD PTR [rbp-8], eax

jge .L21

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-8], eax

.L21:

add DWORD PTR [rbp-4], 1

.L20:

mov eax, DWORD PTR [rbp-4]

cmp eax, DWORD PTR [rbp-28]

jl .L22

mov eax, DWORD PTR [rbp-8]

pop rbp

ret

min_value:

push rbp

mov rbp, rsp

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov rax, QWORD PTR [rbp-24]

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-8], eax

mov DWORD PTR [rbp-4], 0

jmp .L25

.L27:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

cmp DWORD PTR [rbp-8], eax

jle .L26

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-8], eax

.L26:

add DWORD PTR [rbp-4], 1

.L25:

mov eax, DWORD PTR [rbp-4]

cmp eax, DWORD PTR [rbp-28]

jl .L27

mov eax, DWORD PTR [rbp-8]

pop rbp

ret

find_average:

push rbp

mov rbp, rsp

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

pxor xmm0, xmm0

movss DWORD PTR [rbp-8], xmm0

mov DWORD PTR [rbp-4], 0

jmp .L30

.L31:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

cvtsi2ss xmm0, eax

movss xmm1, DWORD PTR [rbp-8]

addss xmm0, xmm1

movss DWORD PTR [rbp-8], xmm0

add DWORD PTR [rbp-4], 1

.L30:

mov eax, DWORD PTR [rbp-4]

cmp eax, DWORD PTR [rbp-28]

jl .L31

cvtsi2ss xmm1, DWORD PTR [rbp-28]

movss xmm0, DWORD PTR [rbp-8]

divss xmm0, xmm1

movss DWORD PTR [rbp-8], xmm0

movss xmm0, DWORD PTR [rbp-8]

pop 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:

push rbp

mov rbp, rsp

sub rsp, 32

mov QWORD PTR [rbp-24], rdi

mov DWORD PTR [rbp-28], esi

mov DWORD PTR [rbp-32], edx

mov DWORD PTR [rbp-8], 0

mov DWORD PTR [rbp-4], 0

jmp .L34

.L36:

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-24]

add rax, rdx

mov eax, DWORD PTR [rax]

cmp DWORD PTR [rbp-32], eax

jne .L35

mov edx, DWORD PTR [rbp-4]

mov eax, DWORD PTR [rbp-32]

mov esi, eax

mov edi, OFFSET FLAT:.LC2

mov eax, 0

call printf

mov DWORD PTR [rbp-8], 1

.L35:

add DWORD PTR [rbp-4], 1

.L34:

mov eax, DWORD PTR [rbp-4]

cmp eax, DWORD PTR [rbp-28]

jl .L36

cmp DWORD PTR [rbp-8], 0

jne .L38

mov eax, DWORD PTR [rbp-32]

mov esi, eax

mov edi, OFFSET FLAT:.LC3

mov eax, 0

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:

push rbp

mov rbp, rsp

sub rsp, 32

mov DWORD PTR [rbp-20], 1

mov DWORD PTR [rbp-4], 0

mov DWORD PTR [rbp-24], 0

mov DWORD PTR [rbp-28], 0

mov DWORD PTR [rbp-8], 1

mov edi, OFFSET FLAT:.LC4

call puts

lea rax, [rbp-20]

mov rsi, rax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call __isoc99_scanf

mov QWORD PTR [rbp-16], 0

mov eax, DWORD PTR [rbp-20]

cdqe

sal rax, 2

mov rdi, rax

call malloc

mov QWORD PTR [rbp-16], rax

mov DWORD PTR [rbp-4], 0

jmp .L40

.L41:

mov eax, DWORD PTR [rbp-4]

add eax, 1

mov esi, eax

mov edi, OFFSET FLAT:.LC6

mov eax, 0

call printf

lea rax, [rbp-24]

mov rsi, rax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call __isoc99_scanf

mov eax, DWORD PTR [rbp-4]

cdqe

lea rdx, [0+rax*4]

mov rax, QWORD PTR [rbp-16]

add rdx, rax

mov eax, DWORD PTR [rbp-24]

mov DWORD PTR [rdx], eax

add DWORD PTR [rbp-4], 1

.L40:

mov eax, DWORD PTR [rbp-20]

cmp DWORD PTR [rbp-4], eax

jl .L41

jmp .L42

.L51:

mov edi, OFFSET FLAT:.LC7

call puts

mov edi, OFFSET FLAT:.LC8

call puts

mov edi, OFFSET FLAT:.LC9

call puts

mov edi, OFFSET FLAT:.LC10

call puts

mov edi, OFFSET FLAT:.LC11

call puts

mov edi, OFFSET FLAT:.LC12

call puts

mov edi, OFFSET FLAT:.LC13

call puts

mov edi, 10

call putchar

lea rax, [rbp-28]

mov rsi, rax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call __isoc99_scanf

mov edi, 10

call putchar

mov eax, DWORD PTR [rbp-28]

cmp eax, 7

ja .L42

mov eax, eax

mov rax, QWORD PTR .L44[0+rax*8]

jmp rax

.L44:

.quad .L42

.quad .L50

.quad .L49

.quad .L48

.quad .L47

.quad .L46

.quad .L45

.quad .L43

.L50:

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call bubble_sort

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call print_array

jmp .L42

.L49:

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call bubble_sort_descending

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call print_array

jmp .L42

.L48:

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call max_value

mov esi, eax

mov edi, OFFSET FLAT:.LC14

mov eax, 0

call printf

jmp .L42

.L47:

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call min_value

mov esi, eax

mov edi, OFFSET FLAT:.LC15

mov eax, 0

call printf

jmp .L42

.L46:

mov edx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, edx

mov rdi, rax

call find_average

cvtss2sd xmm0, xmm0

mov edi, OFFSET FLAT:.LC16

mov eax, 1

call printf

jmp .L42

.L45:

mov edi, OFFSET FLAT:.LC17

call puts

lea rax, [rbp-32]

mov rsi, rax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call __isoc99_scanf

mov edx, DWORD PTR [rbp-32]

mov ecx, DWORD PTR [rbp-20]

mov rax, QWORD PTR [rbp-16]

mov esi, ecx

mov rdi, rax

call find_key

jmp .L42

.L43:

mov DWORD PTR [rbp-8], 0

nop

.L42:

cmp DWORD PTR [rbp-8], 0

jne .L51

mov eax, 0

leave

ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote