Question: Description: Your task is to implement a bubble sort algorithm in MIPS assembly language. Your program must implement the following steps: Prompt the user for

Description: Your task is to implement a bubble sort algorithm in MIPS assembly language. Your program must implement the following steps: Prompt the user for a number that defines the size of an array Prompt the user to enter n integers number of integers based on the size of the array Store all integer values in an array in memory Sort all integer values Print out the sorted integer list

My programing is sorting fine if I enter an array size 10 and numbers 2, 4, 6, 5, 1, 0, 7, 9, 8, 4 but if I enter an array size of 5 and enter numbers 5, 4, 3, 2, 1 my output is 0, 1, 2, 3, 4 please help thanks.

#all data for the program which are stored in RAM .data #next 4 lines are stored as strings in RAM message: .asciiz "Enter the size of array then press enter:" enterNum: .asciiz "Enter number: " space: .asciiz " " sortedArray: .asciiz "Sorted Array: "

#code section (instructions) .text

#prompt user to enter array size li $v0,4 la $a0,message syscall

#getting array size and storing in s0 li $v0,5 syscall add $s0, $v0, $zero

######################################################### ##### begining of loopInput ### ######################################################### #next 2 line are used for loop counters subi $s0, $s0, 1 #sub one array size for loops addi $t0, $zero, 0 #adding 1 every loop through addi $s7, $sp, 0

loopInput: #prompts user to enter number li $v0,4 la $a0,enterNum syscall #gets number from user addi $v0, $zero, 5 syscall add $t1, $t0, $zero sw $t1, 0($s7) addi $t0, $t0, 1 slt $t1, $s0, $t0 addi $s7, $s7, 4 beq $t1,$zero,loopInput

######################################################### ##### end of loopInput ### #########################################################

#call function to sort array jal sort

#print message sorted array li $v0, 4 la $a0, sortedArray syscall

########################################################## ##### printing array below ### ########################################################## #loop counter for print array add $t1, $zero, $zero addi $s7, $sp, 0

printArray: lw $a0, 0($s7) li $v0, 1 syscall li $v0, 4 la $a0, space syscall addi $s7, $s7, 4 addi $t1, $t1, 1 slt $t2, $s0, $t1 beq $t2, $zero, printArray

j exitProgram #jump to end of program

########################################################## ##### function for sorting the array ###### ########################################################## sort: add $t0, $zero, $zero #counter for loop1 i = 0 addi $s2, $s0, 1 #s2 equals array at original size j addi $s7, $sp, 0 #setting off set of array to zero

#next 2 line are to the end of the array add $t1, $s2, $zero subi $t1, $t1, 4

loop1: addi $t0, $t0, 1 #incrementing counter for loop1 i++ bgt $t0, $s2, exit #if t0 < s2 sorting is finished

loop2:

bge $t0, $t1, loop1 #if j < = i return to loop1

subi $t1, $t1, 1 #decrementing counter for loop1 j--

mul $t4, $t1, 4 #using to add to stack pointer reg for proper positioning end of array addi $t3, $t4, -4 #using to add to stack pointer reg for proper positioning begining og array add $t7, $t4, $s7 #getting t7 in proper position t7 = j add $t8, $t3, $s7 #getting t8 in proper position t8 = j-1 lw $t5, 0($t7) #loading position of t7 in t5 for swaping lw $t6, 0($t8) #loading position of t8 in t6 for swaping

bgt $t5, $t6, loop2 #if t5 > t6 run loop2 again

#next 2 lines are swaping t5 and t6 "sorting the array" sw $t5, 0($t8) sw $t6, 0($t7) j loop2

exit: jr $ra ######################################################################### # End Program ######################################################################### exitProgram: li $v0, 10 syscall

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!