如何在 MIPS 中移动双精度数?
How to move a double in MIPS?
我正在尝试从用户那里读取一个 double,然后将其存储在一个临时寄存器中,如下所示:
li $v0, 7 #syscall for reading double = 7
syscall
mov.d $t0, $f0 #t0= number of gallons
我收到一个错误:
"$t0": 操作数类型不正确
为此使用的正确类型是什么?
$t0
不是浮点寄存器,所以你不能 mov.d
它。请改用其他浮点寄存器之一 ($f1..$f31
)。由于双精度占用两个相邻的浮点寄存器,因此您应该移至偶数浮点寄存器之一 ($f2, $f4, ..., $f30
)。
如果你真的想要将$f0
的值放在$t0
中,你必须先将其转换为单精度浮点数,然后然后使用 mfc1
:
cvt.s.d $f2,$f0 # convert to single-precision (32-bit)
mfc1 $t0,$f2 # copy the single-precision float bit-for-bit to GPR $t0
请注意,$t0
现在将包含一个浮点数,因此在整数算术运算中将无用。
也可以将双精度转换为整数,然后将其移动到 GPR:
cvt.w.d $f2,$f0 # convert to integer by rounding (according to the currently set rounding mode)
mfc1 $t0,$f2 # copy to GPR $t0
因此,如果您最初在 $f0
中有 3.14
,您最终会在 $t0
中得到 3
或 4
,具体取决于当前舍入模式。
我正在尝试从用户那里读取一个 double,然后将其存储在一个临时寄存器中,如下所示:
li $v0, 7 #syscall for reading double = 7
syscall
mov.d $t0, $f0 #t0= number of gallons
我收到一个错误:
"$t0": 操作数类型不正确
为此使用的正确类型是什么?
$t0
不是浮点寄存器,所以你不能 mov.d
它。请改用其他浮点寄存器之一 ($f1..$f31
)。由于双精度占用两个相邻的浮点寄存器,因此您应该移至偶数浮点寄存器之一 ($f2, $f4, ..., $f30
)。
如果你真的想要将$f0
的值放在$t0
中,你必须先将其转换为单精度浮点数,然后然后使用 mfc1
:
cvt.s.d $f2,$f0 # convert to single-precision (32-bit)
mfc1 $t0,$f2 # copy the single-precision float bit-for-bit to GPR $t0
请注意,$t0
现在将包含一个浮点数,因此在整数算术运算中将无用。
也可以将双精度转换为整数,然后将其移动到 GPR:
cvt.w.d $f2,$f0 # convert to integer by rounding (according to the currently set rounding mode)
mfc1 $t0,$f2 # copy to GPR $t0
因此,如果您最初在 $f0
中有 3.14
,您最终会在 $t0
中得到 3
或 4
,具体取决于当前舍入模式。