我正在尝试使用Mips对mergesort程序进行编码,但它会陷入无限循环中,而我似乎无法让它工作。
.text
.globl main
main:
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, array
li $a1, 3
li $a2, 43
la $a3, helper
jal mergesort #go through mergesort
move $a0, $v0 #move result to address $a0
li $v0, 4 #print out array
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
li $v0, 10 #finish program
syscall
mergesort:
addi $sp, $sp, 4 #save variables
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $a1, 8($sp)
sw $a2, 12($sp)
sw $s1, 16($sp)
sw $s2, 20($sp)
sw $s3, 24($sp)
sw $s4, 28($sp)
sw $s5, 32($sp)
sw $s6, 36($sp)
sw $s7, 40($sp)
slt $t0, $a1, $a2 #comparing low < high
li $t1, 1
bne $t0, $t1, quit #if false finish program
add $s0, $a1, $a2 #s0 = mid (mid = (high+low)/2)
sll $s0, $s0, 1
j mergesort_1 #first recursion
j mergesort_2 #second recursion
j merge #start merge
mergesort_1:
move $a2, $s0 #mid = high
jal mergesort #go through mergesort with new parameters
mergesort_2:
add $s0, $s0, 1 #low = mid+1
move $a1, $s0
jal mergesort #go through mergesort with new parameters
merge:
add $s1, $s1, $a1 #s1 = i = low
add $s3, $s3, $a1 #s3 = k = low
add $s2, $s0, 1 #s2 = l = mid+1
add $s4, $a0, $s1 #s4 = a[i]
srl $s4, $s4, 2
add $s5, $a0, $s2 #s5 = a[j]
srl $s5, $s5, 2
add $s6, $a3, $s3 #s6 = c[k]
srl $s6, $s6, 2
while1:
slt $t0, $s0, $s1 #i <= mid
bne $t0, $zero, while2
slt $t1, $a2, $s2
bne $t1, $zero, while2 #j <= high
#if either fails go to next while loop
add $s4, $a0, $s1 #s4 = a[i]
srl $s4, $s4, 2
add $s5, $a0, $s2 #s5 = a[j]
srl $s5, $s5, 2
slt $t2, $s4, $s5 #a[i] < a[j]
addi $t3, $t3, 1
beq $t2, $t3, if_1 # if condition is true go through if statement
move $s6, $s5 #else, c[k] = a[j]
addi $s3, $s3, 1 #increment
addi $s2, $s2, 1
jal while1 #go through while loop again
if_1:
add $s6, $a3, $s3 #s6 = c[k]
srl $s6, $s6, 2
move $s6, $s4 #c[k] = a[i]
addi $s3, $s3, 1 #increments
addi $s1, $s1, 1
jal while1 #go through while loop again
while2:
slt $t0, $s0, $s1 # if i > mid, end while loop
bne $t0, $zero, while3
add $s4, $a0, $s1 #s4 = a[i]
srl $s4, $s4, 2
add $s6, $a3, $s3 #s6 = c[k]
srl $s6, $s6, 2
move $s6, $s4 #c[k] = a[i]
addi $s3, $s3, 1 #increments
addi $s1, $s1, 1
jal while2 #go through while loop again
while3:
slt $t0, $a3, $s2 # if j > high end while loop
bne $t0, $zero, for_init
add $s5, $a0, $s2 #s5 = a[j]
srl $s5, $s5, 2
add $s6, $a3, $s3 #s6 = c[k]
srl $s6, $s6, 2
move $s6, $s5 #c[k] = a[j]
addi $s3, $s3, 1 #increments
addi $s2, $s2, 1
jal while3 #repeat while loop
for_init:
move $s1, $zero #set parameters for for loop
add $s1, $s1, $a1 #i = low
for:
slt $t0, $s3, $s1 #if i > k end for loop
bne $t0, $zero, quit
add $s7, $a3, $s1 #s7 = c[i]
srl $s7, $s7, 2
add $s4, $a0, $s1 #s4 = a[i]
srl $s4, $s4, 2
move $s4, $s7 #a[i] = c[i]
addi $s1, $s1, 1 #increment
jal for #repeat
quit:
lw $s7, 40($sp) #load values
lw $s6, 36($sp)
lw $s5, 32($sp)
lw $s4, 28($sp)
lw $s3, 24($sp)
lw $s2, 20($sp)
lw $s1, 16($sp)
lw $a2, 12($sp)
lw $a1, 8($sp)
lw $s0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 48 #return to main
jr $ra
.data
array: .word 56,3,46,47,34,12,1,5,10,8,33,25,29,31,50,43
helper: .word 0:50
它昨天正常工作,但由于某种原因我今天无法上班,我不知道发生了什么变化。