我正在用MIPS编写一个程序,将用户输入的摄氏温度转换为华氏温度。程序循环允许用户进行任意次数的转换。要退出程序,他们应该进入q
。是否有一种方法可以接受输入,可以是整数或字符?我想到的一个可能的解决方案是读取一个字符串并将其转换为int,但我有点不确定如何去做。有没有一种简单的方法来做这种转换?
.globl main
.data
prompt: .asciiz "Please input a Celsius temperature: "
num: .word 32
li $t4, 0
.text
main:
li $v0, 4
la $a0, prompt # prints Prompt
syscall
li $v0, 5 # user input
syscall
move $a0, $v0 # moves input value in $a0
lw $s0, num # Loads 32 in $s0
li $t0, 0 # Sets up 9/5
li $t1, 0
mtc1 $t0, $f2 # Converts into float point numbers
mtc1 $t1, $f30
move $a1, $t3
move $a2, $s0
jal Temp # jumps to function Temp
move $s4, $v0
li $v0, 1
move $a0, $s4
syscall
li $v0, 10
syscall
Temp:
mul $t0, $a0, 9 # 9 * userInput Should equal 180
div $t4, $t0, 5 # value of register in t0 divided by 5
add $t1, $t4, $a2 # Answer above + 32
move $v0, $t1 # places in return register
jr $ra # Jumps to line after function call
我已经提供了一个示例程序,它只接受用户输入的摄氏温度,并将其转换为华氏温度。你的想法可以用while循环来实现。例如:
la $t0,str #str can be the string char "q"
loop: lb $a0,0($t0)
beq $a0,$0,exit
# your main code
addi $t0,$t0,1
j loop
exit:
然而,这不是一件容易但可行的任务。