• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Sum Float Numbers with FPU but without FADD

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
djhex
Asked:
djhex
  • 4
  • 3
  • 2
1 Solution
 
_Katka_Commented:
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
 
_Katka_Commented:
"fldz" loads zero ofcourse :)
0
 
grg99Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
djhexAuthor Commented:
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
 
grg99Commented:
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
 
_Katka_Commented:
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
 
djhexAuthor Commented:
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
 
grg99Commented:
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
 
_Katka_Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now