MIPS program that takes an array elements as inline data and prints them in ascending and descending orders
input should be interacive.

I don't know where to fix.... help me to fix.


li $t4, 9999 #$t4 = 9999
la $s0, array #$s0 = address of array[0]
move $a1, $s0 #saving array address in $a1 for later use
addi $s0, -4 # Moved $s0 one word up to accommodate increment
             # later
addi $s0, 4
#print out the prompt
la $a0, askInput
li $v0, 4

#read in the array element value entered by user
li $v0, 5

#store the value in array
move $t0, $v0 
sw $t0, ($s0)
beq $t0, $t4, ProcessArray

j inputLoop

move $s0, $a1 # $s0 = array[0]

lw $s1, ($s0) # load the last element in $s1
addi $s0, 4
lw $s2, ($s0)
beq $s2, $t4, Exit

slt $t5, $s1, $s2 #$t5 = 1 if $s1 < $s2
beq $t5, $0, swap
j ProcessArray

#la $s0, array

move $s0, $a1
#printout the array element array[0]
lw $t3, ($s0)
move $a0, $t3
li $v0, 1
la $a0, comma
li $v0, 4
addi $s0, 4
bne $t3, $t4, printLoop #Checking for 9999

li $v0, 10

move $a0, $s0
sw $s1, 0($a0)
addi $a0, -4
sw $s2, ($a0)
bne $s2, $t4, Exit
j ProcessArray

array: .space 100
arrayAscend: .space 100
arrayDescend: .space 100
askInput: .asciiz "Eneter value of array element or 9999 to end "
output: .asciiz "\nYou entered: "
comma: .asciiz ", "

Well, first, can you give us some more detali about what it is doing, and where it is failing?

I did note that you are calling the swap routine with a branch, and then explicitly jumping back to processArray. I don't know if this is related to the problem you are having, but it seems an odd way to do this. Has your professor covered the Jump/Branch and Link (jal, jalr, bgezal, and bltzal) and Jump Register (jr) instructions yet? Instead of

slt $t5, $s1, $s2 #$t5 = 1 if $s1 < $s2
beq $t5, $0, swap

I would normally use

sub $t5, $s1, $s2   # $t5 is negative if $s1 < $s2
bltzal $t5, swap

which automagically saves the address after the instruction in $ra, and then use

jr $ra

to return from the call; this makes for a more flexible subroutine that can be call from more than one place.

As for how it is failing, I'm guessing that it isn't getting all of the elements sorted in the desired order, correct? That's because your sorting algorithm is incomplete. What you have hit upon is call bubble sort, and it is the sorting algorithm that almost everyone comes up with if asked to do it on their own. The problem with bubble sort is that it doesn't sort the whole list on the first pass. Let's say you have a list of five items:

6, 3, 5, 7, 2

When you loop on it five times, you get

6, 3, 5, 7, 2 ==> 3, 6, 5, 7, 2 ==> 3, 5, 6, 7, 2  ==> 3, 5, 6, 7, 2  ==> 3, 5, 6, 2, 7

Do you see what happened? You got the highest value to the top, but the other values are still out of order. In order to ensure that all the values 'bubble up' to the right spots, you need to repeat the loop as many times as you have elements of the array (actually there is a trick that can stop it earlier than that, but the basic algorithm calls for n passes on n elements). So, what you need to do is have an outer loop around the loop that bubbles the values, and repeats the bubble process once for each element.

There are better algorithms for sorting, but since you already have the basics of the bubble sort algorithm down, it is best to stick to it for now. With a small number of elements, bubble sort is simple enough that it is almost as fast or faster than as the others anyway, since they are usually more complicated.

This article has been dead for over six months. Start a new discussion instead.