I’ve been working on a very basic MIPS program that computes some stuff on ticket sales. I have all the functionality implemented, but when I do my IF statements (blt –> branch if less than) I am running into some errors. I have the following data stored in temporary registers:
- $t0 = # of VIP tickets.
- $t1 = # of General Admission tickets.
- $t2 = # of Box Office tickets.
- $t4 = Average ticket sales.
I have checked that the values are stored accordingly and the code does work if the condition is FALSE, but if the condition is met it will branch and still print the code before the branch. Here is part of my code:
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
Can anyone see why I am getting a problem with this? The output of my program looks something like this:
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.
EDIT: working code.
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
You just need some unconditional branches so that you don’t execute both the “greater than” and “less than” code paths, e.g.
You need to do this for each of your three if/else blocks.