flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > Integer to Float
Author
Mercury Knight

Joined: 09 Nov 2008
Posts: 15
Mercury Knight
I am rewriting a board game I wrote for a class project in ASM. My game, a variation of Backgammon, used a lot of floating point math for the graphics, and Visual Studios (both 2005 and 2009) do not like floats, preferring us to use doubles instead. Result is that the game is too slow in redrawing all of the graphics, especially when the screen is maximized.

I just hit a little snafu when I realized that dd was used for both integers and floats that are 4 bytes wide, and have been having a bit of trouble trying to find an alternative solution. My first thought was to use df (define float), except df uses 6 bytes and since it's next to dp, I am assuming it means "define far". For example:

Code:
```WIDTH = 16
dd WIDTH   ; integer
dd 16f        ; float
dd 16.0      ; float

SIZE = WIDTH * WIDTH
dd SIZE      ; also an integer
```

How does one allocate space for WIDTH and SIZE as a float here? Most of the time they would be used as integers, but in some cases they would be used as floats, like when scaling the size of the board to the size of the window.

Any suggestions?

PS: I prefer not to use SSE, the original program is being run on computers that uses the older CPUs that do not have SSE.
19 Sep 2009, 16:15

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Maybe I don't know what you are asking exactly, but I think that the solution is simpler than you imagine.

Code:
```WIDTH equ 16.0
SIZE equ 256.0
```

Other consideration might be to define them as integer like you are doing now, but convert them on the fly.
Code:
```A dd 16
B dd 256

mov  eax,[A]
imul eax,[B],17
mov  [A],eax
mov  [B],eax
;Now, when you want floats:
fild [B] ;but use as float
fmulp ;like here
fst  [A] ;you can store that into the same space

;[---]

;Later you can take the float and turn it back as easily:
fld  [A]
fist [A]
```
20 Sep 2009, 16:10
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 7889
Location: Kraków, Poland
Tomasz Grysztar
The problem here arises from the fact that fasm is not able to do assembly-time floating-point calculations.
It may be possible to write a macro for this (int2float), however. You would have to scan for the highest set bit in the (positive) integer number, set the exponent field appropriately, and fill the mantissa bits with the bits just below this highest bit.
20 Sep 2009, 17:39

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
I first thought about doing something like:
Code:
```macro int2float i
{
dd i#.0
}
```

but it obviously doesn't work.

One needs to do something like:
Code:
```macro int2float i
{
s=(i) and 80000000h
if s=80000000h
b=(not i +1) and 07FFFFFFFh
else
b=(i) and 07FFFFFFFh
end if
bitscan b
e=(bc+127) shl 23
m=(b shl (23-bc)) and 07FFFFFh
dd s + e + m
}
macro bitscan b
{
bc=0
c=b
if b=0
bc=0
else
repeat 30
c=c shr 1
if c<>0
bc=bc+1
end if
end repeat
end if
}

int2float -1234
dd -1234.0
```

EDIT: Just as Tomasz described, I found a VHDL-lab PDF - https://www.fh-muenster.de/el-labor/downloads/vhdlversuch4.pdf
21 Sep 2009, 07:27
Mercury Knight

Joined: 09 Nov 2008
Posts: 15
Mercury Knight
Hey Thanks!

That's exactly what I needed. You just saved me from having to do the work myself lol! Now I can get back to work here!
23 Sep 2009, 02:12
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum