浮点指令
Float point instructions
可能我不知道如何使用浮点指令。我想读取一个浮点数,进行一些操作并再次将结果打印为浮点数,但是 SPIM 在这里给我一个错误 mov.s $f0, $v0
。我想每当使用浮点指令时,我必须给两个 $f
寄存器,对吧?但是我如何复制 $v0
中的内容,因为系统调用需要它?
这是我的代码
.data 0x10010000
prompt: .asciiz "Write 0 to °C -> °F\Write 1 for °F ->°C;\n"
ctof: .asciiz "Degree in Fahrenheit: "
ftoc: .asciiz "Degree in Celsius: "
error: .asciiz "Error: write 0 o 1\n\n"
.text 0x400000
main: la $a0, prompt
li $v0, 4
syscall #print prompt
li $v0, 5
syscall #read integer
beq $v0, 0, c_to_f
beq $v0, 1, f_to_c
la $a0, error
li $v0, 4
syscall #print error
j main
c_to_f: la $a0, ftoc
li $v0, 4
syscall #print ftoc
li $v0, 6
syscall #read degree as float
mov.s $f0, $v0
mul.s $f0, $f0, 9.0
div.s $f0, $f0, 5.0
add.s $f0, $f0, 32.0
la $a0, ctof
li $v0, 4
syscall #print ctof
move $f12, $f0
li $v0, 2
syscall #print result
li $v0, 10
syscall #exit
系统调用 6 (read_float
) returns $f0
中的值,而不是 $v0
。所以你的 mov.s $f0, $v0
是不必要的,因为你已经有了 $f0
.
中的值
如果你做在GPR中有一个整数值,想把它转换成浮点数,然后把它放在一个浮点寄存器中,那么你可以按如下方式做:
mtc1 $v0,$f0 # Move the value of $v0 into $f0
cvt.d.w $f12,$f0 # Convert the integer value in $f0 into a double-precision
# floating-point value and place the result in $f12
请参阅 面向程序员的 MIPS32™ 架构
第二卷:MIPS32™ 指令集 如果您想阅读更多关于 cvt
和 mtc1
指令的信息。
可能我不知道如何使用浮点指令。我想读取一个浮点数,进行一些操作并再次将结果打印为浮点数,但是 SPIM 在这里给我一个错误 mov.s $f0, $v0
。我想每当使用浮点指令时,我必须给两个 $f
寄存器,对吧?但是我如何复制 $v0
中的内容,因为系统调用需要它?
这是我的代码
.data 0x10010000
prompt: .asciiz "Write 0 to °C -> °F\Write 1 for °F ->°C;\n"
ctof: .asciiz "Degree in Fahrenheit: "
ftoc: .asciiz "Degree in Celsius: "
error: .asciiz "Error: write 0 o 1\n\n"
.text 0x400000
main: la $a0, prompt
li $v0, 4
syscall #print prompt
li $v0, 5
syscall #read integer
beq $v0, 0, c_to_f
beq $v0, 1, f_to_c
la $a0, error
li $v0, 4
syscall #print error
j main
c_to_f: la $a0, ftoc
li $v0, 4
syscall #print ftoc
li $v0, 6
syscall #read degree as float
mov.s $f0, $v0
mul.s $f0, $f0, 9.0
div.s $f0, $f0, 5.0
add.s $f0, $f0, 32.0
la $a0, ctof
li $v0, 4
syscall #print ctof
move $f12, $f0
li $v0, 2
syscall #print result
li $v0, 10
syscall #exit
系统调用 6 (read_float
) returns $f0
中的值,而不是 $v0
。所以你的 mov.s $f0, $v0
是不必要的,因为你已经有了 $f0
.
如果你做在GPR中有一个整数值,想把它转换成浮点数,然后把它放在一个浮点寄存器中,那么你可以按如下方式做:
mtc1 $v0,$f0 # Move the value of $v0 into $f0
cvt.d.w $f12,$f0 # Convert the integer value in $f0 into a double-precision
# floating-point value and place the result in $f12
请参阅 面向程序员的 MIPS32™ 架构
第二卷:MIPS32™ 指令集 如果您想阅读更多关于 cvt
和 mtc1
指令的信息。