I am very new to MIPS programing and I was wondering if you could help me out. I need to write a program that converts a temperature (input by the user) from either Fahrenheit to Celsius, or Celsius to Fahrenheit depending on what the user defines. For example, if the user inputs 100 C
the output would be 100 C is the same as 212 F
.
I am able to find out if the user puts a C or an F after the temp and jump to the appropriate code to do the conversions, the problem is the way I have it now, the program treats the input as a String, not an integer. It won't do any math on a String. But for some reason, it will output a ridiculously large number and I dont know why. Is there a way to search an integer for C or F? Any help is greatly appreciated!
Here's what I have so far:
.text
main:
# Print String 1
la $a0, str1 # put the address of the string to display in register $a0
li $v0, 4 # This is the system service to display the string
syscall
la $a0, memAddr
li $v0, 8 # Loads System service to read string
syscall
la $s0, ($a0) # Copies the user input to $s0
la $t1, memAddr
loop:
lb $t2, ($t1)
beq $t2, 'C', Fahrenheit
beq $t2, 'F', Celsius
add $t1, $t1, 1
j loop
Fahrenheit:
# Print user input
li $v0, 4 # Loads System service to print integer
move $a0, $s0
syscall
# Print String 2
la $a0, str2
li $v0, 4
syscall
# Celsius to Fahrenheit: F = C*(9/5)+32
li $t0, 32
mtc1 $t0, $f0
cvt.s.w $f0, $f0
mtc1 $s0, $f3
cvt.s.w $f3, $f3
li.s $f1, 1.8
mul.s $f2, $f3, $f1
add.s $f4, $f2, $f0
# Convert float to int
cvt.w.s $f4, $f4
mfc1 $t0, $f4
# Print result
li $v0, 1 # Loads System service to print integer
move $a0, $t0
syscall
# Print String 5
la $a0, str5
li $v0, 4
syscall
j Exit
Celsius:
# Print user input
li $v0, 4 # Loads System service to print integer
move $a0, $s0
syscall
# Print String 2
la $a0, str2
li $v0, 4
syscall
# Fahrenheit to Celsius: C = (F-32)*(5/9)
li.s $f1, 0.55555
sub.s $f2, $f3, $f0
mul.s $f4, $f2, $f1
# Convert float to int
cvt.w.s $f4, $f4
mfc1 $t0, $f4
# Print result
li $v0, 1 # Loads System service to print integer
move $a0, $t0
syscall
# Print String 4
la $a0, str4
li $v0, 4
syscall
j Exit
Exit:
li $v0, 10
syscall
.data
str1: .asciiz "Enter a number to convert: \n"
str2: .asciiz " is the same as: "
str4: .asciiz " C"
str5: .asciiz " F"
memAddr: .space 40