编写代码的指令如下:
**编写一个SPIM汇编语言程序convert.s。该程序将执行以下操作: 提示用户输入表示摄氏度或华氏度的字母。
C 或 c 表示输入的温度以摄氏度为单位。
F 或 f 表示输入的温度是华氏温度。
如果输入的字母不是 C、c、F 或 f,则打印出错误消息“Wrong Letter!”
提示用户输入一个整数,表示与刚刚输入的字母匹配的温度。
将温度转换为对应的温度。
这里有两个公式供您参考:F = (9 * C / 5) + 32
C = 5 * (F - 32) / 9
使用适当的消息打印出转换后的值。**
这是我写的代码:
## Program name: convert.s
##
## Purpose:
## -It's going to prompt the user to enter F, f, C, or c
## -It will convert from Celsius to Fahrenheit or from Fahrenheit to Celsius
## -If the user enters a different letter, it will not convert and it's going to show an error
## Formula of conversion:
## F = (9 * C / 5) + 32
##
## C = 5 * (F - 32) / 9
##
## t0- Temperature integer
## t1- Choice between C or F
## t2- t1 choice
## a0- points to strings to be printed
##
##
#################################################
# #
# text segment #
# #
#################################################
.text
.globl__start
__start: #Execution starts here
la $a0, Userchoice #Print prompt on terminal
li $a1, 3 #System call to print
li $v0, 8
syscall #Out a string
move $t1, $a0
## User's choice
lb $t2, ($t1) #get byte from the input letter
beq $t2, 'C', Celsius # jump to celsius
beq $t2, 'c', Celsius
beq $t2, 'F', Fahrenheit #jump to fahrenheit
beq $t2, 'f', Fahrenheit
j error #if user enters wrong letter, jump to error
## F to C
Fahrenheit:
la $a0, promtFahrenheit
li $v0, 4
syscall
li $v0,5 #Reads integer
syscall
jal F2C
move $t3, $t0
j printFarenheit
F2C
sub $t0, $v0, 32 #Subtract 32 to F
mul $t0, $t0, 5 #Multiply by 5
div $t0, $t0, 9 #Divide by 9
jr $ra
## C to F
Celsius:
la $a0, promptCelsius
li $v0, 4
syscall
li $v0, 5 #reads integer
syscall
jal C2F
move $t3, $v0
j printCelsius
C2F:
mult $t0, $v0, 9 #Multiply by 9
div $t0, $t0, 5 #Divide by 5
addi $t0, $t0, 32 #Add 32
jr $ra
## Error
error:
la $a0, error #Print error
li $v0, 4
syscall
j end #program ends here
## Print
printCelsius:
la $a0, ansC #print string
li $v0, 4
syscall
move $a0, $t3 #print result
li $v0, 4
syscall
la $a0, endl #print out a new line
li $v0, 4
syscall
j end #end program
printFahrenheit:
la $a0, ansC #print string
li $v0, 4
syscall
move $a0, $t3 #print result
li $v0, 1
syscall
la $a0, endl #print out a new line
li $v0, 4
syscall
j end #end program
## End Program
end:
la $a0, endl # print out and start new line
li $v0, 4
syscall
li $v0, 10
syscall #bye
#################################################
# #
# data segment #
# #
#################################################
.data
Userchoice: .space 3
prompt: .asciiz "Type 'C' to enter temperature in Celsius\nand 'F' to enter temperature in Fahrenheit: "
promptCelsius: .asciiz "Enter temperature in Celsius: "
ansC: .asciiz "The temperature in Celsius is: "
promptFahrenheit: .asciiz "Enter temperature in Fahrenheit: "
ansF: .asciiz "The temperature in Celsius is: "
error: .asciiz "Incorrect letter! Try again.\n"
endl: .asciiz "\n"
# End of file convert.s
但是当我将此代码添加到 PCSPim 时,我得到一个错误,我不知道如何修复它!请帮忙!!