Solved

# Sum Float Numbers with FPU but without FADD

Posted on 2005-04-18
193 Views
Please advice me in how to use the FPU. I can use FPU but I cant use FADD.

I know fpu a an 80 bit register. but I dont know how to load to REAl8 values in the fpu and how to return to my c program
0
Question by:djhex

LVL 10

Expert Comment

Hi, try

fild dword ptr [ebp-\$04] // single precision
fild qword ptr [ebp-\$04] // double precision

where [ebp-\$04] is first function parameter
the you can add it just like

finit // initializes fpu
fld1 // loads 1 into fpu stack -> st(0)=1
fldz // loads 1 again into fpu stack -> st(0)=0; st(1)=1
faddp st(1),st(0) // adds stack value from offset 1 to stack value from offset 0 -> st(0)=1+0=1;st(1)=void

regards,
Kate

0

LVL 10

Expert Comment

"fldz" loads zero ofcourse :)
0

LVL 22

Expert Comment

Um, the 80-bit floating-point registers may look like nice wide places to do arithmetic, but the FPU just doesnt have any instructions for using the registes for anything other than FP operations. There are no shift and no masking, no OR, no bit test instructions at all.   You need all of those to do the math yourself.

I suggest you go back to using double-precision math, using adc and sbb.  They work just fine.

0

LVL 10

Author Comment

I can use FPU registers the only thing I cant use its FADD and FSUB for addition and substraction

If I have this

SumarFloat  Proc var1:real4, var2:real4

endp

What code must I put inside to load var1 and var2 in two different FPU registers.

After it I can manipulate the bits.

and suppose I already sum the two float numbers. How can I return to my c program?

DO I have to put RET??
0

LVL 22

Expert Comment

The 80-bit floating-point registers may look like nice wide places to do arithmetic, but the FPU just *does not  have* any instructions for using the registers for anything other than FP operations.

There are no shift,  no masking, no OR, no bit test instructions at all.

You need all of those to do the math yourself.

I suggest you go back to using 32-bit registers, and double-precision math, using adc and sbb.  They work just fine.

The FP registers are not suited for this kind of thing at all.

0

LVL 10

Expert Comment

Load it as I mentioned before:

fild dword ptr [ebp-\$04] -> loads var2 into ST(0)
fild dword ptr [ebp-\$08] -> loads var2 into ST(0) and moves var2 into ST(1)

you can sum them using:

faddp st(1),st(0) -> ST(0)=ST(1)+ST(0) -> ST(0)=var1+var2

Then there two ways of returning floating-point values:

1) stored in ST(0)
2) stored as x bytes on the top of the stack in

push eax -> creates 32bit stack space
fist dword ptr [esp] -> moves ST(0) into that free 32bit space

The value is then returned. Most common is approach 1)

regards,
Kate

0

LVL 10

Author Comment

When returning Intenger values I usually moved the return value to EAX register. Now What I have to do If my C functions receives a float or double?
What is the syntax for returning those values on Assembler

0

LVL 22

Expert Comment

It varies between compilers, the easiest way is to call the function from C and peek at the generated code.  But you seem to ignore each and everything I suggest so why am I continuing?

0

LVL 10

Accepted Solution

Hi, djhex, I don't understand..but I just showed you how, read my previous post :)

(ST(x) is position in FPU stack)

regards,
Kate
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusualâ€¦
Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, frâ€¦
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

#### 759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!