I am trying to write a code which takes a,b,c or d as user input then branches them depending on what the user wants to. For some reason when I pick one of the 4 options the code just falls through all the branches and the program ends. Is it because there can only be one branch statement used?
.data
#messages
options: .asciiz "\nPlease enter a, b, c or d: "
youEntered: .asciiz "\nYou picked: "
bmiMessage: .asciiz "\nThis is the BMI Calculator!"
tempMessage: .asciiz "\nThis converts Farenheit to Celcius!"
weightMessage: .asciiz "\nThis converts Pounds to Kilograms!"
sumMessage: .asciiz "\nThis is the sum calculator!"
repeatMessage: .asciiz "\nThat was not a, b, c or d!"
#variables
input: .space 4 #only takes in one character
.text
main:
#display "options" messsage
li $v0, 4
la $a0, options
syscall
#user input
li $v0, 8
la $a0, input
li $a1, 4
syscall
#display "youEntered" messsage
li $v0, 4
la $a0, youEntered
syscall
#display option picked
li $v0, 4
la $a0, input
syscall
#branching
beq $a0, 'a', bmi
beq $a0, 'b', temperature
beq $a0, 'c', weight
beq $a0, 'd', sum
#end of main
li $v0, 10
syscall
bmi:
li $v0, 4
la $a0, bmiMessage
syscall
temperature:
li $v0, 4
la $a0, tempMessage
syscall
weight:
li $v0, 4
la $a0, weightMessage
syscall
sum:
li $v0, 4
la $a0, sumMessage
syscall
repeat:
li $v0, 4
la $a0, repeatMessage
syscall
Anything will be helpful Thanks guys!
When you have code like:
label:
# some code
another_label:
# some more code
after # some code
executes, control just continues on to # some more code
. You probably want to jump unconditionally to your end of main code:
label:
# some code
j done
another_label:
# some more code
j done
# ...
done:
# end of main
This, coupled with your branches, gives exclusive block semantics like a C if
-else
chain:
if (something) {
// do something
}
else if (something_else) {
// do something else
}
// end of main
Secondly, $a0
is an address, not a value, so the branch comparisons will fail. Try loading the value located at the address into a register, then using the register value to compare against 'a'
, 'b'
and so on.
For example,
lb $t0, input
beq $t0, 'a', bmi
beq $t0, 'b', temperature
beq $t0, 'c', weight
beq $t0, 'd', sum