1

I'm very new to MIPS. Ok, so my whole program is, about a user enters 10 integers and then we store it in an array, we then calculate the min and max and then print out the unsorted array. Then we begin the bubble sort algorithm where I'm struggling at right now, im not gonna post my whole program because of its a lot, I'm only gonna post my bubble sorting algorithm.

This is my thought process of my algorithm, so first i initialize my counters and size of my array. then I begin my outer loop which is branch to endouter if i == size, then inner loop, branch to endinner if j == size, then get both of my indexes that ima compare, compare then and if my first index is greater than the second index, then swap. I'm getting an endless loop of exception for some reason, anyone helps?

#BubbleSort
    li $t2, 0               #outer counter = 0 i
    li $t1, 9               #size of array 0-9
    li $t8, 1               #inner counter j


outer:  beq $t2,$t1, endouter       #branch to endouter if i < 0

inner:  beq $t8,$t1, endinner   #branch to endinner if j < size
    li $t9, 1
    sub $t9,$t8, $t9        #j - 1
    lw $t4,array($t9)       #load (j-1) into temp
    lw $t5,array($t8)       #load j into temp

    blt $t4,$t5, noswap     #if (j-1) < j branch to no swap
    lw $t4,array($t8)       #swithc (j-1) with j
    lw $t5,array($t9)       #swithc (j-1) with j


 noswap:    add $t8,$t8, 1          #increment j
    j inner

 endinner:
    add $t2,$t2,1
    li $t8, 1
    j outer
 endouter:
Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
Nazar Trut
  • 35
  • 7
  • The endless execution could well be because of some of the other parts of your code that you have left out of your question. Use the debugging features in a simulator like SPIM or MARS to find what the problem is. – Michael Oct 16 '19 at 05:24

1 Answers1

0

Try this :

li $t0, 0               #outer counter = 0 i
li $t1, 40 #9               #size of array 10 * 4 bytes
li $t8, 40             #inner counter j


outer:  blt $t0,$zero, endouter       #branch to endouter if i < 0

inner_j:
subiu $t8,$t8,4
li $t0,0
beq $t8,$t0,endouter   #branch to endouter if j == i
inner_i:


        lw $t4,array($t0)       #load i into temp
        lw $t5,array($t8)       #load j-1 into temp

        blt $t4,$t5, noswap     #if (j-1) < j branch to no swap

    sw $t4,array($t8)       # swap 
    sw $t5,array($t0)       # swap
    addiu $t0,$t0,4
    beq $t0,$t8,inner_j
        j inner_i
 noswap:    
 addiu $t0,$t0, 4          #increment j
 beq $t0,$t8,inner_j
 j inner_i

 endouter:
nissim abehcera
  • 821
  • 6
  • 7