C语言 MIPS(或 SPIM):加载浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2589233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:04:53  来源:igfitidea点击:

MIPS (or SPIM): Loading floating point numbers

cmipsspim

提问by James

I am working on a little mini compiler while trying to learn some MIPS here. Here's my issue:

我正在开发一个小型编译器,同时尝试在这里学习一些 MIPS。这是我的问题:

MIPS has an instruction li (load immediate) which would work like this

MIPS 有一个指令 li(立即加载),它会像这样工作

li ,100

which would load 100 into register 5.

这会将 100 加载到寄存器 5 中。

However, I need to load floats into registers right now and am struggling with figuring out a way to do it...since li $5,2.5 does not work.

但是,我现在需要将浮点数加载到寄存器中,并且正在努力寻找一种方法来做到这一点......因为 li $5,2.5 不起作用。

Anyone have any advice?

有人有什么建议吗?

I am working in C, I was thinking I could somehow get the integer representation of the float I am working with (i.e. so the floats binary representation == the ints binary representation) then load the "integer" into the register and treat it like a float from then on.

我在 C 中工作,我想我可以以某种方式获得我正在使用的浮点数的整数表示(即浮点数二进制表示 == 整数二进制表示)然后将“整数”加载到寄存器中并像对待它一样从此浮动。

Maybe its too late but Im stuck right now.

也许为时已晚,但我现在卡住了。

回答by Zack

MARS does notappear to have any instructions/pseudo instructions that load floating point immediate values into floating registers. Instead, you need to put the floating point value in memory and load the register from memory:

MARS似乎没有任何将浮点立即数加载到浮点寄存器的指令/伪指令。相反,您需要将浮点值放入内存并从内存中加载寄存器:

.data
fp1: .double 2.5
fp2: .double -0.75

.text   
l.d $f0, fp1
l.d $f2, fp2

回答by WhirlWind

You will need to use the floating point registers to load your floats.

您将需要使用浮点寄存器来加载您的浮点数。

Instead of:

代替:

li ,2.5

Try:

尝试:

li.s $f5,2.5

Take a look at mfc1 and mtc1 instructions to move between integer and floating point registers.

查看 mfc1 和 mtc1 指令以在整数和浮点寄存器之间移动。