我对MIPS编程很陌生,我想知道你是否可以帮助我。我需要编写一个程序,根据用户定义的内容,将温度(用户输入)从华氏温度转换为摄氏温度,或者将摄氏温度转换为华氏温度。例如,如果用户输入100 C
,则输出将为100 C is the same as 212 F
。
我能够找出用户是否在temp之后放置了C或F并跳转到相应的代码来进行转换,问题就是我现在的方式,程序将输入视为字符串,不是整数。它不会对String进行任何数学运算。但由于某种原因,它将输出一个可笑的大数字,我不知道为什么。有没有办法搜索C或F的整数?非常感谢任何帮助!
这是我到目前为止所拥有的:
.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