The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: Damos on July 19, 2009, 05:37:38 PM

Title: defining a floating point value in data section from an equate
Post by: Damos on July 19, 2009, 05:37:38 PM
Can you help me please? i'm trying to do this:


ScreenResolutionX = 1024

.data
fScreenResolution      dd ScreenResolution.0


I need to generate a floating point value from a constant integer at assembly time.
Is this possible? A macro of some sort?
Title: Re: defining a floating point value in data section from an equate
Post by: jj2007 on July 19, 2009, 05:53:07 PM
No macro needed:

ScreenResolutionX = 1024
...
.data
fScreenResolution REAL4 @CatStr(%ScreenResolutionX, <.0>)
Title: Re: defining a floating point value in data section from an equate
Post by: Damos on July 19, 2009, 06:13:19 PM
You're a star jj2007, thanks for the help. i'm working on a 2d tile game engine and i've just spent the last hour tring to do this, think I need to review some of my masm & macro knowledge. Thanks again.
Title: Re: defining a floating point value in data section from an equate
Post by: Neo on July 20, 2009, 04:30:00 AM
Does it not work with just:

ScreenResolutionX = 1024
...
.data
fScreenResolution REAL4 ScreenResolutionX


Or does that give some sort of unnecessary error?
Title: Re: defining a floating point value in data section from an equate
Post by: jj2007 on July 20, 2009, 07:57:18 AM
Quote from: Neo on July 20, 2009, 04:30:00 AM
Does it not work with just:

ScreenResolutionX = 1024
...
.data
fScreenResolution REAL4 ScreenResolutionX


Or does that give some sort of unnecessary error?

Try yourself:
include \masm32\include\masm32rt.inc

ScreenResolutionX = 1024

.code
fScreenResolution REAL4 ScreenResolutionX

start:
exit
end start


error A2187
Title: Re: defining a floating point value in data section from an equate
Post by: Neo on July 20, 2009, 08:38:04 AM
Quote from: jj2007 on July 20, 2009, 07:57:18 AM
error A2187
Darn, that sucks.  :(

It won't give an error in Inventor IDE, but I can't recall if it encodes it as the integer 1024 or the floating point 1024 (I should fix it if it's not floating point).  It'll also let you do things like doing operations on a float in arbitrary constant expressions, like:
mov  eax,1.0/(1 shl 32) ;moves the floating point 1/(2^32) into eax

I think it'll use it as an integer on operations other than + - * / though, so like:
mov  eax,1.0 shr 23 ;moves the exponent of 1.0 into eax (7Fh)

Edit: It'll treat it as a double instead of a float in some cases, so 1.0 shr 23 may not be 7Fh; I should check back over that code, since it's been a while.  :wink
Title: Re: defining a floating point value in data section from an equate
Post by: dedndave on July 20, 2009, 09:00:47 AM
it doesn't allow "ScreenResolution*1.0"
it would be nice if the assembler could just do math - lol
Title: Re: defining a floating point value in data section from an equate
Post by: jj2007 on July 20, 2009, 09:14:36 AM
Quote from: Neo on July 20, 2009, 08:38:04 AM
Quote from: jj2007 on July 20, 2009, 07:57:18 AM
error A2187
Darn, that sucks.  :(

It's called type checking...
Title: Re: defining a floating point value in data section from an equate
Post by: Neo on July 20, 2009, 09:21:04 AM
Quote from: dedndave on July 20, 2009, 09:00:47 AM
it doesn't allow "ScreenResolution*1.0"
it would be nice if the assembler could just do math - lol
That one's definitely supported in Inventor IDE.  In fact, you could define a constant ScreenResolutionFloat as "ScreenResolution*1.0", and when you hold Ctrl and run the mouse over the name, it'll show you "= 1024.0"  :8)
Title: Re: defining a floating point value in data section from an equate
Post by: jj2007 on July 20, 2009, 09:38:31 AM
Quote from: dedndave on July 20, 2009, 09:00:47 AM
it doesn't allow "ScreenResolution*1.0"
it would be nice if the assembler could just do math - lol
It does, it does...

MyCt = 5
MyDword dd MyCt*5
MyFloat REAL4 @CatStr(%MyCt*5, <.0>)
Title: Re: defining a floating point value in data section from an equate
Post by: dedndave on July 20, 2009, 01:06:55 PM
yesssss - well i would hope it could multiply a real by an integer without catstr
could be useful for scaling
odd that i have never needed to do this
i guess it's because, once the assembler hiccuped on me, i would write code to do the conversion at runtime - lol