将华氏温度转换为摄氏度的组装程序

问题描述 投票:0回答:1

我的代码中不断出现此错误:

spim:(解析器)文件 C:/Users/Emili/Downloads/Hello World.s 第 62 行出现语法错误 ldc1 $f2, 32.0

.data
prompt: .asciiz "Enter temperature in Fahrenheit: "
result: .asciiz "The temperature in Celsius is: "
prompt2: .asciiz "Convert another temperature? (y/n): "
newline: .asciiz "\n"

.text
.globl main
.ent main

main:
    # Print prompt and read Fahrenheit temperature
    li $v0, 4
    la $a0, prompt
    syscall

    li $v0, 5
    syscall
    move $t0, $v0  # Save Fahrenheit temperature in $t0

    # Call conversion procedure
    jal convertToFahrenheit

    # Print the Celsius temperature
    li $v0, 4
    la $a0, result
    syscall

    li $v0, 2
    mov.s $f12, $f0  # Move Celsius temperature to $f12
    syscall

    # Ask if user wants another conversion
    li $v0, 4
    la $a0, prompt2
    syscall

    li $v0, 12
    syscall
    move $t1, $v0  # Save user's input in $t1

    # Check if input is 'y'
    li $t2, 'y'
    bne $t1, $t2, exit  # Exit program if input is not 'y'

    # Print newline
    li $v0, 4
    la $a0, newline
    syscall

    j main  # Start another iteration of the program

exit:
    # Program is finished running
    li $v0, 10
    syscall
.end main

# Conversion procedure
convertToFahrenheit:
    sub.s $f0, $f12, $f0
    ldc1 $f2, 32.0
    sub.s $f0, $f0, $f2
    ldc1 $f2, 9.0
    div.s $f0, $f0, $f2
    mul.s $f0, $f0, $f2
    jr $ra
assembly mips
1个回答
0
投票

ldc1
l.d
的所有第二个操作数(相同,但恕我直言更佳)都是内存位置,通常指定为数据的简单标签或相对于整数基址寄存器的寻址模式。

虽然整数指令允许使用

li
伪指令,但这些(例如
l.d
)并不等同于浮点指令 - 这些等同于整数单位的
lw

如果您拥有的只是

lw
,您如何将特定的整数常量放入整数寄存器中?当然,从内存中加载它,并给出
lw
您用于该数据项的标签名称。

浮点常量的想法相同。浮点常量对于单精度是 32 位,对于双精度是 64 位,因此无论如何都不太适合指令流,最好从内存加载。

顺便说一句,也适用于字符串文字 -

la
不允许您直接将字符串文字作为第二个参数,因此必须声明在内存中初始化的字符串,然后使用标签或其他东西引用它们。


看起来您不小心混合了单数和双数,所以也许检查一下。如果你真的想混合它们,你必须从单精度转换为双精度和/或反之亦然,不能直接将双精度添加到单精度。


(建议使用

l.s
表示单精度,使用
l.d
表示双精度,而不是
lwc1
ldc1
.s
.d
后缀将帮助提醒您所使用的精度,因为它看起来类似于
 add.s
mul.s
)

© www.soinside.com 2019 - 2024. All rights reserved.