Question

In: Computer Science

Write a MIPS program that uses an implementation of quick sort to sort an array of...

Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language).

Quicksort Implementation - C

int partition(int arr [] , int left , int right) {

int i=left, j=right;

int tmp;
int pivot = arr[(left + right) / 2];

while (i <= j) {
while (arr [ i ] < pivot)

i ++;
while (arr [ j ] > pivot)

j −−;

if (i <= j) {
tmp = arr[i];

arr[i] = arr[j]; arr[j] = tmp;
i ++;
j −−;

}

}

return i ;

}


void quickSort(int arr [] , int left , int right) {

int index = partition(arr, left , right);

if (left < index − 1)

quickSort(arr , left , index − 1);

if (index < right)
quickSort(arr , index , right );

}

Solutions

Expert Solution

Here is the solution of your problem:

Translate C language to MIPS Program

partition:

        daddiu  $sp,$sp,-48

        sd      $fp,40($sp)

        move    $fp,$sp

        sd      $4,16($fp)

        move    $3,$5

        move    $2,$6

        sll     $3,$3,0

        sw      $3,24($fp)

        sll     $2,$2,0

        sw      $2,28($fp)

        lw      $2,24($fp)

        sw      $2,0($fp)

        lw      $2,28($fp)

        sw      $2,4($fp)

        lw      $3,24($fp)

        lw      $2,28($fp)

        addu    $2,$3,$2

        srl     $3,$2,31

        addu    $2,$3,$2

        sra     $2,$2,1

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $2,0($2)

        sw      $2,8($fp)

        b       .L2

        nop

.L4:

        lw      $2,0($fp)

        addiu   $2,$2,1

        sw      $2,0($fp)

.L3:

        lw      $2,0($fp)

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $2,0($2)

        lw      $3,8($fp)

        slt     $2,$2,$3

        bne     $2,$0,.L4

        nop

        b       .L5

        nop

.L6:

        lw      $2,4($fp)

        addiu   $2,$2,-1

        sw      $2,4($fp)

.L5:

        lw      $2,4($fp)

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $2,0($2)

        lw      $3,8($fp)

        slt     $2,$3,$2

        bne     $2,$0,.L6

        nop

        lw      $3,0($fp)

        lw      $2,4($fp)

        slt     $2,$2,$3

        bne     $2,$0,.L2

        nop

        lw      $2,0($fp)

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $2,0($2)

        sw      $2,12($fp)

        lw      $2,0($fp)

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $3,4($fp)

        dsll    $3,$3,2

        ld      $4,16($fp)

        daddu   $3,$4,$3

        lw      $3,0($3)

        sw      $3,0($2)

        lw      $2,4($fp)

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $3,12($fp)

        sw      $3,0($2)

        lw      $2,0($fp)

        addiu   $2,$2,1

        sw      $2,0($fp)

        lw      $2,4($fp)

        addiu   $2,$2,-1

        sw      $2,4($fp)

.L2:

        lw      $3,0($fp)

        lw      $2,4($fp)

        slt     $2,$2,$3

        beq     $2,$0,.L3

        nop

        lw      $2,0($fp)

        move    $sp,$fp

        ld      $fp,40($sp)

        daddiu  $sp,$sp,48

        j       $31

        nop

quickSort:

        daddiu  $sp,$sp,-64

        sd      $31,56($sp)

        sd      $fp,48($sp)

        sd      $28,40($sp)

        move    $fp,$sp

        lui     $28,%hi(%neg(%gp_rel(quickSort)))

        daddu   $28,$28,$25

        daddiu  $28,$28,%lo(%neg(%gp_rel(quickSort)))

        sd      $4,16($fp)

        move    $3,$5

        move    $2,$6

        sll     $3,$3,0

        sw      $3,24($fp)

        sll     $2,$2,0

        sw      $2,28($fp)

        lw      $3,28($fp)

        lw      $2,24($fp)

        move    $6,$3

        move    $5,$2

        ld      $4,16($fp)

        ld      $2,%got_disp(partition)($28)

        move    $25,$2

        nop

        sw      $2,0($fp)

        lw      $2,0($fp)

        addiu   $2,$2,-1

        lw      $3,24($fp)

        slt     $2,$3,$2

        beq     $2,$0,.L10

        nop

        lw      $2,0($fp)

        addiu   $2,$2,-1

        move    $3,$2

        lw      $2,24($fp)

        move    $6,$3

        move    $5,$2

        ld      $4,16($fp)

        ld      $2,%got_disp(quickSort)($28)

        move    $25,$2

        nop

.L10:

        lw      $3,0($fp)

        lw      $2,28($fp)

        slt     $2,$3,$2

        beq     $2,$0,.L12

        nop

        lw      $3,28($fp)

        lw      $2,0($fp)

        move    $6,$3

        move    $5,$2

        ld      $4,16($fp)

        ld      $2,%got_disp(quickSort)($28)

        move    $25,$2

        nop

.L12:

        nop

        move    $sp,$fp

        ld      $31,56($sp)

        ld      $fp,48($sp)

        ld      $28,40($sp)

        daddiu  $sp,$sp,64

        j       $31

        nop

// happy coding :)


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write an ARMv8 program to sort an array of elements. As Imentioned in class, this...
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...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
use quick sort to sort the following array. show each pass and what the pivot is...
use quick sort to sort the following array. show each pass and what the pivot is during the pass. please explain why you are swapping positions. do not use a compiler. 30 5 40 11 20 9 15 2 60 25 80 3 73 35 4 75 20 6
Write a MIPS program that multiples every element in an array of size 10 by 4...
Write a MIPS program that multiples every element in an array of size 10 by 4 without using the multiply instruction.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT