Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Your task is to implement a bubble sort algorithm in MIPS assembly

ID: 3723111 • Letter: D

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 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

Explanation / Answer


.data
.align 4
Table: .space 100
msg1: .asciiz "Enter integer: "
msg2: .asciiz " "
msg3: .asciiz " Sorted array is : "
msg4: .asciiz "Enter array size : "
.text
.globl main
main:

#prompt for number of integer
li $v0,4
la $a0,msg4
syscall
li $v0,5
syscall
add $s0,$v0,$zero

sub $s0,$s0,1 #sub one as array is zero based index


#addi $s0,$zero,6
addi $t0,$zero,0
in:
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
add $t1,$t0,$zero
sll $t1,$t0,2
add $t3,$v0,$zero
sw $t3,Table ( $t1 )
addi $t0,$t0,1
slt $t1,$s0,$t0
beq $t1,$zero,in

la $a0,Table
addi $a1,$s0,1 #a1=6
#call buble_sort
jal buble_sort

#print table
li $v0,4
la $a0,msg3
syscall
la $t0,Table
#s0=5
add $t1,$zero,$zero
printtable:
lw $a0,0($t0)
li $v0,1
syscall
li $v0,4
la $a0,msg2
syscall
addi $t0,$t0,4
addi $t1,$t1,1
slt $t2,$s0,$t1
beq $t2,$zero,printtable

li $v0,10
syscall

buble_sort:
#a0=address of table
#a1=sizeof table
add $t0,$zero,$zero #counter1( i )=0

loop1:
addi $t0,$t0,1 #i++
bgt $t0,$a1,endloop1 #if t0 < a1 break;

add $t1,$a1,$zero #counter2=size=6
loop2:

bge $t0,$t1,loop1 #j < = i

#slt $t3,$t1,$t0
#bne $t3,$zero,loop1

addi $t1,$t1,-1 #j--

mul $t4,$t1,4 #t4+a0=table[j]
addi $t3,$t4,-4 #t3+a0=table[j-1]
add $t7,$t4,$a0 #t7=table[j]
add $t8,$t3,$a0 #t8=table[j-1]
lw $t5,0($t7)
lw $t6,0($t8)

bgt $t5,$t6,loop2

#switch t5,t6
sw $t5,0($t8)
sw $t6,0($t7)
j loop2

endloop1:
jr $ra