简体   繁体   English

在 MIPS 中将浮点数除为整数

[英]divide float point number as integer in MIPS

I have loaded a float point double precission number in two $t registers, now I want to divide it by (-4) (not using fp instructions) and store it back into the $f register.我在两个 $t 寄存器中加载了一个浮点双精度数,现在我想将它除以 (-4)(不使用 fp 指令)并将其存储回 $f 寄存器。

mfc1 $t0, $f0  #$f0 = 0x00000000
mfc1 $t1, $f1  #$f1 = 0x40240000
div $t1, $t1, -4
mfhi $t0 #move the remainder to $t0
mflo $t1 #move the quotient to $t1

mtc1 $t0, $f0
mtc1 $t1, $f1


# store the $f0 result in memory
# print X/(-4)


mov.d $f12, $f0
li $v0, 3
syscall

but this gives very unexpected result which is -2.231744757682269E231但这给出了非常意外的结果,即 -2.231744757682269E231

any help will be appreciated.任何帮助将不胜感激。

Obviously you can't just use an integer division.显然你不能只使用整数除法。 In the general case, you have to break up the numbers into their constituent parts, namely sign, mantissa and exponent, then implement division with integer arithmetic.在一般情况下,您必须将数字分解为它们的组成部分,即符号、尾数和指数,然后用整数算法实现除法。

If you specifically want to divide by -4, you can use the fact that it is a power of 2, so you just need to flip the sign bit and subtract 2 from the exponent.如果你特别想除以 -4,你可以使用它是 2 的幂这一事实,所以你只需要翻转符号位并从指数中减去 2。

Maybe read about floating point representations .也许阅读浮点表示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM