Strange that in 7 years as an assembly programmer I've never needed to use floating point numbers before. In BIOS we didn't need 'em, and in "8051 land" we needed 'em but didn't have 'em. :rolleyes:

I'm trying to write a screensaver, just for practice, and I need to scroll an image across the screen. I'm keeping track of the position of the image using a floating point number that's updated every milisecond. When this number is rounded to an integer, it represents the X location of the image.



fld scrollDis ; ST = scrolling speed in px/sec
fmul mSec ; ST = scrolling speed in px/msec
fld floatL ; push old precise location
fxch ; rearrange stack
fsub ; ST = new location
fist newL ; truncate and store as integer
fstp floatL ; pop new precise loactioan
The problem is that newL is always zero. So what am I doing wrong here?
Posted on 2002-02-25 17:05:42 by The Worrier King
Is this what you are trying to do? :
(if not - write your formula, it also usefull to know type of variables in sake of optimization)
newL == FloatToInt(floatL- (scrollDis*mSec):

fld scrollDis
fmul mSec
fsub floatL
fistp newL

The problem I see that assembler usually replace fsub with fsubp
and it might (hard to say without source) resulting in poping zeroes in your case.
I recommend to use debugger, it would show you in a second what is cause of
zero results.
I'm not sure yet that I get your formula, please, write it.
Posted on 2002-02-25 17:59:09 by The Svin

Is this what you are trying to do? :
(if not - write your formula, it also usefull to know type of variables in sake of optimization)
newL == FloatToInt(floatL- (scrollDis*mSec):

fld scrollDis
fmul mSec
fsub floatL
fistp newL

The problem I see that assembler usually replace fsub with fsubp
and it might (hard to say without source) resulting in poping zeroes in your case.
I recommend to use debugger, it would show you in a second what is cause of
zero results.
I'm not sure yet that I get your formula, please, write it.
More like this;
floatL -= scrollDis*mSec
newL = FloatToInt(floatL)

mSec = 0.001
initial value of floatL is screen width
Posted on 2002-02-25 18:15:41 by The Worrier King
fld floatL ;st0 = floatL
fld scrollDis ;st0 = scrollDis ; st1 = floatL
fmul mSec ;st0 = scrollDis*mSec
fsub ;st0 = st1-st0 ; st1 poped (0)
fst floatL ;floatL = floatL-(scrollDis*mSec)
fistp newL ;newL = int(floatL) st0 poped(0)
Posted on 2002-02-25 18:40:23 by The Svin
Hi, your code should work, as should The Svins. And here's another version. ;)
fld   scrollDis ; ST = scrolling speed in px/sec

fmul mSec ; ST = scrolling speed in px/msec
fsubr floatL ; ST = old precise location - ST = new location
fist newL ; truncate and store as integer
fstp floatL ; pop new precise loactioan


As for why you always get 0. First try calling finit before the code.

Secondly, make sure floatL, scrollDis & mSec are all initalised to proper values. For example you may have left floatL unitialised since it gets set at the end of the code, however this could screw things up (if you did it at all :) )

Thirdly, I assume mSec holds a millisecond value which changes with time. Make sure its not an interger, or if it use use fimul mSec when multiplying it.

And finally, it seems from your comments that mSec is a constant to covert px/sec to px/msec. If so what value do you change with time in order to scroll the image?
Posted on 2002-02-26 02:42:18 by Eóin
Thanks guys

I found the problem. scrollDis was defined as an integer, but I was loading it as a float. I just needed to change
fld   scrollDis
to
fild  scrollDis
:rolleyes:Thanks again for all your help, this looks like it could be fun. :grin:
Posted on 2002-02-26 07:07:50 by The Worrier King
... this looks like it could be fun.

When you have the right attitude it always is! :alright:
Posted on 2002-02-26 08:09:07 by Eóin
Eyin:
I have a suggestion -
when we post some code wich have instruction different from
Pplain let us add some simple notation before code,
for example like:
(MMX) (XMM) (PIII) (3Dnow) (686) etc.
What do you think?
Posted on 2002-02-26 23:20:55 by The Svin
I thinks thats a very good idea. I know the ficom instructions had thrown me for a while until I realised they weren't part of the origional FPU instruction set.
Posted on 2002-02-27 03:50:30 by Eóin
Where optimizations matters, it would be good to add also something like:

(this code is optimized for Athlon CPU's).

i.e. not only:

(this code requires MMX).
Posted on 2002-02-27 06:35:26 by Maverick