Implement Selection Sort in MIPS (convert from C) Here is C code: #include <stdi
ID: 3629703 • Letter: I
Question
Implement Selection Sort in MIPS (convert from C)Here is C code:
#include <stdio.h>
void swap (int * unsorted, int index1, int index2)
{
int temp;
temp = unsorted[index1];
unsorted[index1] = unsorted[index2];
unsorted[index2] = temp;
return;
}
void sort (int * unsorted, int count)
{
int i, j;
for (i = 0; i < count - 1; i++)
{
int min = unsorted[i], argmin = i;
for (j = i + 1; j < count; j++)
{
if (unsorted[j] < min)
{
min = unsorted[j];
argmin = j;
}
}
if (i != argmin)
swap(unsorted, i, argmin);
}
return;
}
int main (int argc, char ** argv)
{
int unsorted[] = {5, 2, 6, 9, 0, 1, 3, 7, 4, 8};
int i;
sort(unsorted, 10);
for (i = 0; i < 10; i++)
{
printf("%d ", unsorted[i]);
}
return 0;
}
I have no Idea were to begin on converting this C code. I have never done a C conversion to MIPS Assembly
Explanation / Answer
# Objective: Sort array using selection sort algorithm # Input: $a0 = pointer to first, $a1 = pointer to last # Output: array is sorted in place ########################################################## sort: addiu $sp, $sp, -4 # allocate one word on stack sw $ra, 0($sp) # save return address on stack top: jal max # call max procedure lw $t0, 0($a1) # $t0 = last value sw $t0, 0($v0) # swap last and max values sw $v1, 0($a1) addiu $a1, $a1, -4 # decrement pointer to last bne $a0, $a1, top # more elements to sort lw $ra, 0($sp) # pop return address addiu $sp, $sp, 4 jr $ra # return to caller
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.