The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: FlySky on July 04, 2007, 04:24:52 PM

Title: FADD for calculations
Post by: FlySky on July 04, 2007, 04:24:52 PM
Hey there guys,

I am just trying to do some calculations using the floating point stack. Here is my problem. I have a function pushing a certain amount on the stack. fld dword ptr [esi+0x490]. Than the FCHS function makes it minus to speak it the easy way. Than I have a FADD instruction doing the addition and pushing the result back on the stack. +- = - . How would I make that result always being 1. float. I have been thinking like this:

I push for example 20. float on the stack using the instruction FLD dword ptr [esi+0x490]. Than FCHS makes it -20 than the FADD is making it zero. So I was thinking before the FADD I add 1. float but how would I do that? Or can any tell me how I can calculate using FADD but making the result always end up with being 1. float.

Title: Re: FADD for calculations
Post by: u on July 04, 2007, 06:03:09 PM
if what you want to do is
result = var1+(-var1) + 1
then

fld1
fld var1
fld ST
fchs
; now ST(0) = -var1 ;  ST(1) = var1; ST(2) = 1
fadd
fadd
fstp result

Quite useless code
Title: Re: FADD for calculations
Post by: raymond on July 05, 2007, 03:29:54 AM
FlySky

You may want to have a look at the following tutorial if you intend to start using the FPU. It may save you a few headaches and accelerate your learning curve.

http://www.ray.masmcode.com/fpu.html

Raymond
Title: Re: FADD for calculations
Post by: FlySky on July 05, 2007, 04:24:41 PM
Thanks for both replies guys,

I will start reading those FPU tutorials. I will keep you guys updated. It's the first time yeah that I am messing with the floating point stack this way.