boogpicker
Newbie level 1
hey,
i am new to mips and have been trying to translate a quicksort algorithm into mips. problem is that when i load it into PCSpim, it gets hung up and just continues to run until I close Spim. can anyone look at what I have and see if you can figure out where its getting hung up?
Oh and i didnt comment it yet.
.data
array: .word 8,2,7,1,5,6,3,4,9,0
length: .word 10
.text
.globl main
main:
la $a0, array
move $a1, $zero
lw $a2, length
sub $a2, $a2, 1
jal QuickSort
li $v0, 10
syscall
################################################################
QuickSort:
# $a0 contains array
# $a1 contains 0
# $a2 contains n-1
# $s0 contains low
# $s1 contains high
# $s2 contains pivotPosition
sub $sp,$sp, 16
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
sw $ra, 12($sp)
move $s0, $a1
move $s1, $a2
if: bge $s0, $s1, end_if
jal Partition
move $s2, $v0
sub $a2, $s2, 1
jal QuickSort
move $a2, $s1
addi $a1, $s2, 1
jal QuickSort
end_if:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
end_QuickSort:
###############################################################
Swap:
move $t0, $a0
move $t1, $a1
sw $t1, 0($t2)
sw $t0, 0($t2)
sw $t0, 0($t2)
end_Swap:
###############################################################
Partition:
# $a0 contains array
# $s0 contains left
# $s1 contains right
# $s2 contains pivot
# $a1 contains low
# $a2 contains high
# $t1 calculations array[low]
# $t0 contains base address
# $t2 contains calculations for array
# $t3 contains array
# $t4 contains calculations for array
# $t5 contains array
sub $sp, $sp, 16
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
sw $ra, 12($sp)
la $t0, array
begin:
move $s0, $a1
move $s1, $a2
mul $t1, $a1, 4
add $t1, $t0, $t1
lw $s2, 0($t1)
while_condition:
bge $s0, $s1, end_while
mul $t2, $s1, 4
add $t2, $t0, $t2
lw $t3, 0($t2)
while_condition_2:
ble $t3, $s2, end_while_2
sub $s1, $s1, 1
j while_condition_2
end_while_2:
while_condition_3:
bge $s0, $s1, end_while_3
mul $t4, $s0, 4
add $t4, $t0, $t4
lw $t5, 0($t4)
bgt $t5, $s2, end_while_3
addi $s0, $s0, 1
j while_condition_3
end_while_3:
if_2:
bge $s0, $s1, end_if_2
mul $t4, $s0, 4
add $t4, $t0, $t4
lw $a0, 0($t4)
mul $t2, $s1, 4
add $t2, $t0, $t2
lw $a1, 0($t2)
jal Swap
end_if_2:
end_while:
mul $t1, $a1, 4
add $t1, $t0, $t1
mul $t2, $s1, 4
add $t2, $t0, $t2
lw $t3, 0($t2)
sw $t3, 0($t1)
sw $s2, 0($t2)
move $v0, $s2
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
end_Partition:
i am new to mips and have been trying to translate a quicksort algorithm into mips. problem is that when i load it into PCSpim, it gets hung up and just continues to run until I close Spim. can anyone look at what I have and see if you can figure out where its getting hung up?
Oh and i didnt comment it yet.
.data
array: .word 8,2,7,1,5,6,3,4,9,0
length: .word 10
.text
.globl main
main:
la $a0, array
move $a1, $zero
lw $a2, length
sub $a2, $a2, 1
jal QuickSort
li $v0, 10
syscall
################################################################
QuickSort:
# $a0 contains array
# $a1 contains 0
# $a2 contains n-1
# $s0 contains low
# $s1 contains high
# $s2 contains pivotPosition
sub $sp,$sp, 16
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
sw $ra, 12($sp)
move $s0, $a1
move $s1, $a2
if: bge $s0, $s1, end_if
jal Partition
move $s2, $v0
sub $a2, $s2, 1
jal QuickSort
move $a2, $s1
addi $a1, $s2, 1
jal QuickSort
end_if:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
end_QuickSort:
###############################################################
Swap:
move $t0, $a0
move $t1, $a1
sw $t1, 0($t2)
sw $t0, 0($t2)
sw $t0, 0($t2)
end_Swap:
###############################################################
Partition:
# $a0 contains array
# $s0 contains left
# $s1 contains right
# $s2 contains pivot
# $a1 contains low
# $a2 contains high
# $t1 calculations array[low]
# $t0 contains base address
# $t2 contains calculations for array
# $t3 contains array
# $t4 contains calculations for array
# $t5 contains array
sub $sp, $sp, 16
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
sw $ra, 12($sp)
la $t0, array
begin:
move $s0, $a1
move $s1, $a2
mul $t1, $a1, 4
add $t1, $t0, $t1
lw $s2, 0($t1)
while_condition:
bge $s0, $s1, end_while
mul $t2, $s1, 4
add $t2, $t0, $t2
lw $t3, 0($t2)
while_condition_2:
ble $t3, $s2, end_while_2
sub $s1, $s1, 1
j while_condition_2
end_while_2:
while_condition_3:
bge $s0, $s1, end_while_3
mul $t4, $s0, 4
add $t4, $t0, $t4
lw $t5, 0($t4)
bgt $t5, $s2, end_while_3
addi $s0, $s0, 1
j while_condition_3
end_while_3:
if_2:
bge $s0, $s1, end_if_2
mul $t4, $s0, 4
add $t4, $t0, $t4
lw $a0, 0($t4)
mul $t2, $s1, 4
add $t2, $t0, $t2
lw $a1, 0($t2)
jal Swap
end_if_2:
end_while:
mul $t1, $a1, 4
add $t1, $t0, $t1
mul $t2, $s1, 4
add $t2, $t0, $t2
lw $t3, 0($t2)
sw $t3, 0($t1)
sw $s2, 0($t2)
move $v0, $s2
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
end_Partition: