Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS (or SPIM): Loading floating point numbers

Tags:

c

mips

spim

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.

like image 361
James Avatar asked Apr 07 '10 00:04

James


People also ask

What are the floating point instructions in MIPS?

The MIPS has a floating point coprocessor (numbered 1) that operates on single precision (32-bit) and double precision (64-bit) floating point numbers. This coprocessor has its own registers, which are numbered $f0-$f31.

How many floating point registers are there in MIPS?

MIPS has 32 single precision (32 bit) floating point registers. $f0 is not special (it can hold any bit pattern, not just zero).


1 Answers

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
like image 106
Zack Avatar answered Sep 21 '22 18:09

Zack