Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Construct the recursive bubble sort using MIPS. First convert the for loop to a while loop. Only modify the parts that say put code here

Construct the recursive bubble sort using MIPS. First convert the for loop to a while loop. Only modify the parts that say "put code here"

void bubbleSort(int arr[], int n)

{

// An array of 1 element is already sorted

if (n == 1) return;

// Put the last element in the right spot

for (int j=0; j

if (arr[j] > arr[j+1]) {

int tmp = arr[j]; // Again, swap arr[j] and arr[j+1]

arr[j] = arr[j+1];

arr[j+1] = tmp;

}

// Sort the first n-1 elements

bubbleSort(arr, n-1);

}

THIS BELOW IS THE CODE I HAVE COMPLETED SO FAR: Please help me fully complete it and fix any errors! I would appreciate it a lot!

.data nums: .word 0 : 12 # "array" of 12 words to contain values size: .word 12 # size of "array" .text la $s0, nums la $s5, size # load address of size variable lw $s5, 0($s5) # load value of size # Populate with twelve values addi $t1, $zero, 55 sw $t1, 0($s0) addi $t1, $zero, 88 sw $t1, 4($s0) addi $t1, $zero, 0 sw $t1, 8($s0) addi $t1, $zero, 22 sw $t1, 12($s0) addi $t1, $zero, 77 sw $t1, 16($s0) addi $t1, $zero, 44 sw $t1, 20($s0) addi $t1, $zero, 99 sw $t1, 24($s0) addi $t1, $zero, 33 sw $t1, 28($s0) addi $t1, $zero, 110 sw $t1, 32($s0) addi $t1, $zero, 66 sw $t1, 36($s0) addi $t1, $zero, 121 sw $t1, 40($s0) addi $t1, $zero, 11 sw $t1, 44($s0)

################################################################## # AT THIS POINT: $s0 is the address of the start of the array # $s5 is the size (= 12) ################################################################# ################################################################## # PUT CODE HERE FOR CALL # ############################## # SAVE $a registers and $ra ############################## addi $sp, $sp, -12 sw $a0, 0($sp) sw $a1, 4($sp) sw $ra, 8($sp) ############################## # Change $a registers ############################## addi $a0, $a0, 0 addi $a1, $a1, 12 ############################## # jal ############################## jal bubblesort ############################## # RELOAD $a registers and $ra ############################## lw $a0, 0($sp) lw $a1, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 ################################################################## ################################################################## # DO NOT MODIFY la $a0, nums # first argument for print (array) add $a1, $s5, $zero # second argument for print (size) jal print # call print routine. li $v0, 10 # system call for exit syscall # we are out of here. ##################################################################

######################################################################## # PUT CODE HERE FOR FUNCTION bubblesort:

################################################################## # SAVE S REGISTERS ################################################################## addi $s1, $0, 0 # int j = 0; addi $t2, $a1, -1 # $t2 = n - 1 ################################################################## # IF STATEMENT addi $t0, $0, 1 bne $a1, $t0, forLoop j return ############################ # RETURN FROM FUNCTION ############################ return: ################################################################## jr $ra ################################################################## # FOR LOOP ################################################################## forLoop: beq $s1, $t2, end #loop body: if statement addi $s1, $s1, 1 j forLoop end: ################################################################## # RECURSIVE CALL # ###################################### # SAVE $a registers, $ra ###################################### addi $sp, $sp, -12 sw $a0, 0($sp) sw $a1, 4($sp) sw $ra, 8($sp) ###################################### # CHANGE $a registers ###################################### addi $a0, $a0, 0 addi $a1, $a1, -1 ###################################### # jal ###################################### jal bubblesort ###################################### # RELOAD $a registers, $ra ###################################### ################################################################## lw $a0, 0($sp) lw $a1, 4($sp) lw $ra, 8($sp) addi $sp, $sp, 12 ################################################################## # RETURN FROM FUNCTION ################################################################## jr $ra ########################################################################

######################################################################## ######### routine to print the numbers on one line. ######### don't touch anything below this line!!!!

.data space:.asciiz " " # space to insert between numbers head: .asciiz "Sorted array: " .text print:add $s0, $zero, $a0 # starting address of array add $t1, $zero, $a1 # initialize loop counter to array size la $a0, head # load address of print heading li $v0, 4 # specify Print String service syscall # print heading out: lw $a0, 0($s0) # load number for syscall li $v0, 1 # specify Print Integer service syscall # print number la $a0, space # load address of spacer for syscall li $v0, 4 # specify Print String service syscall # output string addi $s0, $s0, 4 # increment address addi $t1, $t1, -1 # decrement loop counter bgtz $t1, out # repeat if not finished jr $ra # return ########################################################################

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

What Is A Database And How Do I Use It

Authors: Matt Anniss

1st Edition

1622750799, 978-1622750795

More Books

Students also viewed these Databases questions