flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Integer to Float

Author
Thread Post new topic Reply to topic
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. Sad

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? Smile

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.
Post 19 Sep 2009, 16:15
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
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
add  eax,1
mov  [B],eax
;Now, when you want floats:
fild [A] ;Load int,
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]
    
Post 20 Sep 2009, 16:10
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7795
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.
Post 20 Sep 2009, 17:39
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
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
Post 21 Sep 2009, 07:27
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Mercury Knight



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

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! Cool
Post 23 Sep 2009, 02:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.