please help me to convert this quicksort in C code to MIPS version of it. below
ID: 3697195 • Letter: P
Question
please help me to convert this quicksort in C code to MIPS version of it.
below things are quicksort with c-code and unfinished MIPS quicksort
please finish MIPS quicksort
#include
int data[] = {8,5,14,10,12,3,9,6,1,15,7,4,13,2,11};
int size = 15;
//Below functions is for debugging purpose only. Don't implement this
/*
void debug(){
int i;
for(i=0;i printf("%d, ", data[i]);
printf(" ");
}
*/
int partition(int * data, int start, int end){
int left=start;
int right=end;
int pivot=data[start];
while(left
while (data[right] > pivot){
right--;
}
while((left < right) && (data[left] <= pivot)){
left++;
}
if(left int temp = data[right];
data[right] = data[left];
data[left] = temp;
}
}
data[start]=data[right];
data[right]=pivot;
return right;
}
void quick_sort(int *data, int start, int end){
int pivot_position;
if(start < end){
pivot_position = partition(data, start, end);
quick_sort(data, start, pivot_position-1);
quick_sort(data, pivot_position+1, end);
}
}
void main(){
quick_sort(data,0,size-1);
//debug();
}
---------------------------------------------------------------------------------------
.data
data: .word 8,5,14,10,12,3,9,6,1,15,7,4,13,2,11
size: .word 15
#use below sample if above example is too large to debug
#data: .word 4,2,5,3,1
#size: .word 5
.text
partition:
# TODO: fill in your code here
jr $ra
quick_sort:
# TODO: fill in your code here
jr $ra
main:
la $a0, data #load address of "data"."la" is pseudo instruction, see Appendix A-66 of text book.
addi $a1, $zero, 0
lw $a2, size #loads data "size"
addi $a2, $a2, -1
addi $sp, $sp, -4
sw $ra, 0($sp)
jal quick_sort #quick_sort(data,0,size-1)
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
Explanation / Answer
# Quicksort.asm .data array: .word 4, 9, 12, -5, 2, 54, 8 , 1 line: .asciiz " " .text main: addi $sp, $sp, -4 # Push the stack pointer down to hold one value sw $ra, 0 ($sp) # Store the return address on the stack la $a0, array # Array into a0 addi $a1, $zero, 0 # Left most element (p) addi $a2, $zero, 7 # Right most element (r) jal quicksort addi $t0, $zero, 0 # Counter loop addi $t1, $zero, 8 # where the loop ends la $t7, array # loads base address of array into t7 topprintloop: beq $t0, $t1, endprintloop sll $t3, $t0, 2 # calculates j * 4 addi $t3, $t7, 0 # Calculates the address of array[j] lw $t4, 0($t3) # load value at array[j] addi $v0, $zero, 1 # sets v0 to 1 for syscall addi $a0, $t4, 0 # load int to print into a0 syscall # prints it addi $v0, $zero, 4 # sets v0 to 4 for syscall la $a0, line # loads string to print into a0 syscall # prints it addi $t0, $t0, 1 # increment counter by 1 j topprintloop # jumps to topprintloop endprintloop: lw $ra, 0 ($sp) # Restore the return address from the stack addi $sp, $sp, 4 # Pop the stack jr $ra quicksort: addi $sp, $sp, -20 # Store it to the stack sw $ra, 0 ($sp) sw $s0, 4 ($sp) sw $s1, 8 ($sp) sw $s2, 12 ($sp) sw $s3, 16 ($sp) addi $s0, $a0, 0 # store into s0 array pointer addi $s1, $a1, 0 # store into s1 left most element (p) addi $s2, $a2, 0 # store into s2 right most element (r) # store into s3 q blt $s1, $s2, prep # jumps and links to prep if pRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.