我正在尝试编写一个快速排序代码,该代码将对指令位于http://www.cs.uni.edu/~fienup/cs1410s19/homework/hw7.pdf中的数组(其hw)进行排序
以下是代码。组装时程序运行正常,但运行时会在MARS中出现错误
.data
array: .word 6 3 1 4 5 2 3 8 9 7 5
length: .word 11
NewLine: .asciiz "\n"
.text
.globl main
main:
la $a0, array
li $a1, 0
lw $t0, length
addi $a2, $t0, -1
jal quickSort
li $v0, 10
syscall
quickSort:
addi $sp, $sp, -20
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s3, 16($sp)
sw $ra, 20($sp)
move $s0, $a0
move $s1, $a1
move $s2, $a2
bge $s1, $s2, end_quickSort
jal partition
move $s3, $v0
move $a0, $s0
move $a1, $s1
addi $a2, $s3, -1
jal quickSort
move $a0, $s0
addi $a1, $s3, 1
move $a2, $s2
jal quickSort
end_quickSort:
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 20
jr $ra
partition:
addi $sp, $sp, -20
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $ra, 20($sp)
move $s0, $a0
move $s1, $a1
move $2, $a2
move $6, $s1
mul $s4, $s1, 4
add $s4, $s0, $s4
lw $s3, 0($s4)
addi $t4, $s4, 4
for:
addi $t2, $s2, 1
mul $t3, $t2, 4
add $t3, $s0, $t3
for_compare:
bge $t4, $t3, end_for
lw $t5, 0($t4)
bge $t5, $s3 if_end
addi $s4, $s4, 4
addi $t6, $t6, 1
move $a1, $s4
move $a2, $t4
jal swap
end_for:
move $a1, $s4
mul $t7, $s1 4
add $t7, $s0, $t7
move $a2, $t7
jal swap
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 20
move $v0, $t6
jr $ra
if_end:
addi $t4, $t4 4
j for_compare
swap:
lw $t0, 0($a1)
lw $t1, 0($a2)
sw $t0, 0($a2)
sw $t1, 0($a1)
jr $ra
addi $t0, $zero, 0
addi $t5, $zero, 0
while:
bge $t5, $s7, exit
lw $t6, array($t0)
addi $t0, $t0, 4
addi $t5, $t5, 1
li $v0, 1
move $a0, $t6
syscall
li $v0, 4
la $a0, NewLine
syscall
j while
exit:
li $v0, 10
syscall
使用MARS,我得到以下信息:行60:运行时异常位于0x00400094:地址超出范围0x7fbffff4。我在做什么错了?