所以我的程序有问题。我有一个for循环,里面有一堆多条件if语句,当它运行的时候,它给了我一个无限循环。这是整个程序中循环发生的部分
li $t7, 0 # i=0
bge $t7, $s0, exit_i # i < trackHeight
addi $t7, $t7, 1 # i++
# j forDraw_1 # loop while i=0
i_plus:
li $t8, 0 # j=0
bge $t8, $s1, exit_j # j < trackWidth
addi $t8, $t8, 1 # j++
# j i_plus # loop while j=0
# j_plus:
if_1:
beq $t7, $zero, drawBorder # i==0
beq $t8, $zero, drawBorder # j==0
addi $t9, $s0, -1 # trackHeight -1
beq $t7, $t9, drawBorder # i== t.h -1
addi $t5, $s1, -1 # trackWidth -1
beq $t8, $t5, drawBorder # j== t.w -1
# else if 1
beq $t7, $s2, draw219 # i == x
beq $t8, $s3, draw219 # j == y
# else if 2
#beq $t7, $s2, draw219 # i == x
addi $t4, $s3, -1 # y-1
beq $t8, $t4, draw219 # j == y-1
# else if 3
#beq $t7, $s2, draw56 # i == x
addi $t4, $t4, 2 # y-1 --> y+1
beq $t8, $t4, draw56 # j == y+1
# else if 4
addi $t3, $s2, 1 # x+1
beq $t7, $t3, draw254 # i == x+1
#addi $t4, $t4, -1 # j back to 0
#beq $t8, $s3, draw254 # j == y
# else if 5
addi $t3, $t3, -2 # x+1 --> x-1
beq $t7, $t3, draw127 # i == x-1
#beq $t8, $s3, draw127 # j == y
# else if 6
addi $t4, $zero, 10
ble $t8, $zero, printLane # j>0
div $t8, $t4
mfhi $t5
beqz $t5, printLane
drawBorder:
li $v0, 4
la $a0, outsideBorder
syscall
j if_1
printLane:
li $v0, 4
la $a0, lanes
syscall
draw219:
li $t5, 219
move $a0, $t5
li $v0, 11
syscall
draw56:
li $t5, 56
move $a0, $t5
li $v0, 11
syscall
draw254:
li $t5, 254
move $a0, $t5
li $v0, 11
syscall
draw127:
li $t5, 127
move $a0, $t5
li $v0, 11
syscall
我也试着用不同的格式写,但是也得到了一个无限循环。这是另一种格式。不确定是什么问题,但是我有一种强烈的感觉,它来自循环的初始化
bne $t7, $zero, else_if1 # i==0
bne $t8, $zero, else_if1 # j==0
addi $t9, $s0, -1 # trackHeight -1
bne $t7, $t9, else_if1 # i== t.h -1
addi $t5, $s1, -1 # trackWidth -1
bne $t8, $t5, else_if1 # j== t.w -1
li $v0, 4
la $a0, outsideBorder
syscall
else_if1:
bne $t7, $s2, else_if2 # i == x
bne $t8, $s3, else_if2 # j == y
li $t5, 219
move $a0, $t5
li $v0, 11
syscall
else_if2:
beq $t7, $s2, else_if3 # i == x
addi $t4, $s3, -1 # y-1
bne $t8, $t4, else_if3 # j == y-1
li $t5, 219
move $a0, $t5
li $v0, 11
syscall
else_if3:
beq $t7, $s2, else_if4 # i == x
addi $t4, $t4, 2 # y-1 --> y+1
bne $t8, $t4, else_if4 # j == y+1
li $t5, 56
move $a0, $t5
li $v0, 11
syscall
else_if4:
addi $t3, $s2, 1 # x+1
bne $t7, $t3, else_if5 # i == x+1
#addi $t4, $t4, -1 # j back to 0
beq $t8, $s3, else_if5 # j == y
li $t5, 254
move $a0, $t5
li $v0, 11
syscall
else_if5:
addi $t3, $t3, -2 # x+1 --> x-1
bne $t7, $t3, else_if6 # i == x-1
beq $t8, $s3, else_if6 # j == y
li $t5, 127
move $a0, $t5
li $v0, 11
syscall
else_if6:
addi $t4, $zero, 10
ble $t8, $zero, printLane # j>0
div $t8, $t4
mfhi $t5
beqz $t5, printLane
li $v0, 4
la $a0, lanes
syscall
1条答案
按热度按时间cpjpxq1n1#
一般来说,一个好的嵌套循环结构应该看起来像这样:假设
n
和m
是常量。注意事项:
loop_i_overhead
和loop_j_overhead
是可选的,但如果你需要跳到它们,它们非常方便。其目的是循环
MAX_INT+1
次,但实际上循环计数器在每次迭代开始时重置为零,因此循环将永远继续。将循环计数器初始化移至循环标签 * 上方 * 可解决此问题。