7

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

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

li $5,100

which would load 100 into register 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.

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.

Maybe its too late but Im stuck right now.

WhirlWind
  • 13,974
  • 3
  • 42
  • 42
James
  • 572
  • 3
  • 9
  • 17

2 Answers2

31

MARS does not appear 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:

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

.text   
l.d $f0, fp1
l.d $f2, fp2
Zack
  • 6,232
  • 8
  • 38
  • 68
10

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

Instead of:

li $5,2.5

Try:

li.s $f5,2.5

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

WhirlWind
  • 13,974
  • 3
  • 42
  • 42
  • Thanks, that .s did the trick. I should probably hit the hay if I missed that. Have a good night. – James Apr 07 '10 at 00:50
  • 5
    That seems to be a pseudoinstruction. How would I actually go about doing it? (MARS doesn't support li.s). Only thing I can think of is to load 25 into $t0, load 10 into $t1, convert each to float, and then divide. – John Kurlak Nov 15 '10 at 01:53