hi all!
i have to do: a = b - c
and check if a is 0. (b, c are floating numbers)
i have used this piece of code for checking if a is 0:


fsub
fldz
fcomp st(1)
fnstsw ax
sahf
jz @zero

this works correctly but...is there another way?
i mean, can i work directly on fsub and taking advantage of some particular flag (modified by fsub) ???

regards,
ZaiRoN
Posted on 2002-08-06 09:20:58 by ZaiRoN
Try this
[size=9].DATA


Data1 REAL8 5.5
Data2 REAL8 3.7
g_dbBuffer1 DB "It Wasn't A Zero", 0

.DATA?

Data3 DQ ?
g_dbBuffer2 DB 20 DUP(?)

.CODE

START:

fld Data1
fld Data2
fsub
fldz
fcomip st(0), st(1)
jz __zero

fst Data3
invoke FloatToStr, Data3, OFFSET g_dbBuffer2
invoke MessageBox, 0, OFFSET g_dbBuffer1, 0, 0

__zero:

fst Data3
invoke FloatToStr, Data3, OFFSET g_dbBuffer2
invoke MessageBox, 0, OFFSET g_dbBuffer2, 0, 0
invoke ExitProcess, 0

END START[/size]
You can use fcomip to compare floating point values and set the eflags register. On this code if A is zero you will only see the difference between the 2 numbers else you will see 2 message boxes(first one is the string "It Wasn't A Zero" and second one will be the difference between the 2 numbers. :)
Posted on 2002-08-06 10:03:28 by stryker
Note that fcomi(p) is a 686 and above instruction only.
It is not therfore an instruction you can use if you want legacy compatability.

Mirno
Posted on 2002-08-06 10:40:00 by Mirno
You can also use ftst instead of the fldz/fcomp combination.

"FTST compares ST0 with zero and sets the FPU flags accordingly. ST0 is treated as the left-hand side of the comparison, so that a `less-than' result is generated if ST0 is negative."

so the resulting code might look like:



fsub
ftst
fnstsw ax
sahf
jz @zero


It might also be better to directly compare against the ax (or ah) register instead of using sahf because sahf is vector decoded. I'm not sure about that, though, so hopefully someone with a little more knowledge will comment on that.
Posted on 2002-08-06 10:47:31 by kmart9200
BTW remove the first occurance of

fst Data3
invoke FloatToStr, Data3, OFFSET g_dbBuffer2

my mistake :) I overlooked the code when I was copying the MessageBox code.
Posted on 2002-08-06 10:47:50 by stryker
thx guys!

kmart9200:
you are right! the code you have posted doesn't work correctly.
this is the correct way:


fsub
ftst
fnstsw ax
and ah, 40h
jnz @zero

bye,
ZaiRoN
Posted on 2002-08-06 14:12:09 by ZaiRoN