我一直致力于一个非常基本的MIPS计划,该计划计算一些关于门票销售的东西。我已经实现了所有功能,但是当我执行我的IF语句(blt - >分支,如果小于)时,我遇到了一些错误。我将以下数据存储在临时寄存器中:
- $t0 = # of VIP tickets.
- $t1 = # of General Admission tickets.
- $t2 = # of Box Office tickets.
- $t4 = Average ticket sales.
我已检查过相应的值存储值,如果条件为FALSE,代码确实有效,但如果条件满足,它将分支并仍然在分支之前打印代码。这是我的代码的一部分:
blt $t0, $t4, VIP_LESS # if $t0 < $t4 then VIP
b VIP_GREAT
VIP_LESS:
li $v0, 4
la $a0, less_avg_vip
syscall
VIP_GREAT:
li $v0, 4
la $a0, great_avg_vip
syscall
blt $t1, $t4, GEN_LESS # if $t1 < $t4 then GEN
b GEN_GREAT
GEN_LESS:
li $v0, 4
la $a0, less_avg_general
syscall
GEN_GREAT:
li $v0, 4
la $a0, great_avg_general
syscall
blt $t2, $t4, BOX_LESS # if $t2 < $t4 then BOX_LESS
b BOX_GREAT
BOX_LESS:
li $v0, 4
la $a0, less_avg_box
syscall
BOX_GREAT:
li $v0, 4
la $a0, great_avg_box
syscall
li $v0, 4
la $a0, endl
syscall
任何人都可以看到我为什么遇到这个问题?我的程序的输出看起来像这样:
VIP: 1349 tickets
General: 5278 tickets
Box: 4367 tickets
Average: 3215 tickets
VIP: Less than average.
General: Greater than average.
General: Less than average.
Box: Greater than average.
Box: Less than average.
编辑:工作代码。
blt $t0, $t4, VIP_LESS # if $t0 < $t4 then VIP
li $v0, 4
la $a0, great_avg_vip
syscall
j GENERAL_IF
VIP_LESS:
li $v0, 4
la $a0, less_avg_vip
syscall
GENERAL_IF:
blt $t1, $t4, GEN_LESS # if $t1 < $t4 then GEN
li $v0, 4
la $a0, great_avg_general
syscall
j BOX_IF
GEN_LESS:
li $v0, 4
la $a0, less_avg_general
syscall
BOX_IF:
blt $t2, $t4, BOX_LESS # if $t2 < $t4 then BOX_LESS
li $v0, 4
la $a0, great_avg_box
syscall
j END_IF
BOX_LESS:
li $v0, 4
la $a0, less_avg_box
syscall
END_IF:
li $v0, 4
la $a0, endl
syscall
答案 0 :(得分:2)
您只需要一些无条件分支,这样就不会执行“大于”和“小于”代码路径,例如
blt $t0, $t4, VIP_LESS # if $t0 < $t4 then VIP
li $v0, 4
la $a0, great_avg_vip
syscall
<<< you need an unconditional branch here to VIP_GE
VIP_LESS:
li $v0, 4
la $a0, less_avg_vip
syscall
xor $a0, $a0, $a0
VIP_GE: <<< label here so that you can skip previous block
...
您需要为三个if / else块中的每一个执行此操作。