[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sum Float Numbers with FPU but without FADD

Posted on 2005-04-18
9
Medium Priority
?
196 Views
Last Modified: 2012-06-21
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
Comment
Question by:djhex
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:_Katka_
ID: 13812838
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

by:_Katka_
ID: 13812925
"fldz" loads zero ofcourse :)
0
 
LVL 22

Expert Comment

by:grg99
ID: 13814202
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
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 
LVL 10

Author Comment

by:djhex
ID: 13815304
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

by:grg99
ID: 13816628
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

by:_Katka_
ID: 13843598
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

by:djhex
ID: 13843634
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

by:grg99
ID: 13843698
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

by:
_Katka_ earned 1000 total points
ID: 13847098
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

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

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

Join & Ask a Question