The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Rockoon on October 30, 2007, 12:45:24 AM

Title: Assemble-time floating point math
Post by: Rockoon on October 30, 2007, 12:45:24 AM

Trying to build a table from some floating point equates but keep running into an error.. have tried several techniques but none work..

a stripped down example of the problem:

f0 equ 1.1011
f1 equ 0.50110

.data

float4 f0 - f1

The error is:

"error A2008: syntax error : floating point constant"

..what are the work-arounds for this issue?
Title: Re: Assemble-time floating point math
Post by: raymond on October 30, 2007, 03:39:17 AM
Quote..what are the work-arounds for this issue?
Declare and initialize them in the data section instead of using equates.
Title: Re: Assemble-time floating point math
Post by: Rockoon on October 30, 2007, 04:16:00 AM
Quote from: raymond on October 30, 2007, 03:39:17 AM
Quote..what are the work-arounds for this issue?
Declare and initialize them in the data section instead of using equates.


How exactly is that going to help?


Title: Re: Assemble-time floating point math
Post by: Rockoon on October 30, 2007, 06:48:34 AM
Ah, I discovered that Float4 is a macro in macros.inc

replacing Float4 with Real4 gives a different error: "error A2187: must use floating-point initializer"

The problem still stands .. is it possible to do floating point math at assemble-time?
Title: Re: Assemble-time floating point math
Post by: u on October 30, 2007, 07:00:56 AM
No, it's a limitation of masm. You need to find a workaround for your needs. (computing the stuff outside, or making a tiny app to compute+store them in an .inc file)
Title: Re: Assemble-time floating point math
Post by: ToutEnMasm on October 30, 2007, 07:32:01 AM
Hello,
Floating points numbers are a human view.
They must be translate (IEEE) by the compiler,result is put in data.Any language do that,it isn't particular to masm.
C++ hide this,but the resulting code is the same.FPN are in data.
With masm,things are made in two steps.Create a constant,put it in a data and the translation is made.
Macros are only a help to do that.


Title: Re: Assemble-time floating point math
Post by: Rockoon on October 30, 2007, 08:35:35 AM
Quote from: Ultrano on October 30, 2007, 07:00:56 AM
(computing the stuff outside, or making a tiny app to compute+store them in an .inc file)


ah well.. guess it is time to try a new assembler.. abslutely no reason for a source file to require a support binary :(

Title: Re: Assemble-time floating point math
Post by: MichaelW on October 30, 2007, 09:55:56 AM
But what assembler would that be?

For  NASM (http://alien.dowling.edu/~rohit/nasmdoc3.html):

"NASM cannot do compile-time arithmetic on floating-point constants."

For  FASM (http://flatassembler.net/docs.php?article=manual#1.2.4):

"flat assembler does not allow any floating point operations at compilation time"

For  GoAsm (http://www.jorgon.freeserve.co.uk/GoasmFrame.htm) I could not find any similar statement, but I suspect that it cannot.

In my test with GAS, this statement:

.double 1.1011 - 0.50110

Resulted in:

Error: junk at end of line, first unrecognized character is "-"

For TASM I don't know and I have no reasonable way to test.

EDIT: And I forgot PoAsm, but it's too late to test.



Title: Re: Assemble-time floating point math
Post by: Mark Jones on October 30, 2007, 06:23:09 PM
Time to make your own assembler! :bdg
Title: Re: Assemble-time floating point math
Post by: Rockoon on October 31, 2007, 03:20:28 AM
Well, HLA supports this.

Unfortunately its HLA (no effense Hyde, but I have never been a fan of pointless syntax sugar)

Now considering two alternatives:

o) using an ansi-C compiler for the table definitions
o) creating macros that manipulate floating point values as strings

Note: GoAsm doesnt support this, and doesn't seem to raise an error either (unless easycode is hiding it from me)
Title: Re: Assemble-time floating point math
Post by: raymond on October 31, 2007, 07:10:16 PM
Quotecreating macros that manipulate floating point values as strings

In order to do that, you need to be entirely familiar with the floating point format and it will not be an easy task. If you need to brush up on the IEEE floating point format, you can have a glance at:
http://www.ray.masmcode.com/tutorial/fpuchap2.htm#floats

Raymond
Title: Re: Assemble-time floating point math
Post by: Rockoon on November 01, 2007, 08:18:50 AM
I assure you that I am extremely fluent with IEEE float representation.. 32 bit, 64 bit, and 80 bit.. :) I've twiddled them from time to time....

I am also familiar with the opengl 16-bit float specification but I am not sure that any GPU hardware actualy impliments them in that specific format
Title: Re: Assemble-time floating point math
Post by: raymond on November 01, 2007, 11:53:30 PM
QuoteI assure you that I am extremely fluent with IEEE float representation..
Glad to hear that. You are in a minority. :clap: The link was offered just in case you were part of the 99% of programmers who don't have any clue about that format.
Title: Re: Assemble-time floating point math
Post by: MichaelW on November 04, 2007, 08:20:30 AM
The addition part of this turned out to be fairly straightforward, but I have no idea how to reasonably do the subtraction, and I don’t even want to think about multiplication or division. There is no error handling, and I haven’t thoroughly tested it, but it basically seems to work up to the point that either component (integer or fractional) exceeds 2^32-1.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    FPADD MACRO arg1, arg2

      LOCAL i1,f1,i2,f2,szf1,szf2,nf,ni,tf,ti,tout

      ;; --------------------------------------
      ;; Extract integer and fractional parts.
      ;; --------------------------------------

      ppos INSTR <arg1>, <.>
      i1 SUBSTR <arg1>, 1, ppos - 1
      f1 SUBSTR <arg1>, ppos + 1
      ppos INSTR <arg2>, <.>
      i2 SUBSTR <arg2>, 1, ppos - 1
      f2 SUBSTR <arg2>, ppos + 1

      ;; -------------------------------------------------------
      ;; Pad fractional parts to effectively align radix point.
      ;; -------------------------------------------------------
     
      szf1 = @SizeStr(%f1)
      szf2 = @SizeStr(%f2)
      WHILE szf1 LT szf2
        f1 CATSTR f1, <0>
        szf1 = @SizeStr(%f1)
      ENDM
      WHILE szf2 LT szf1
        f2 CATSTR f2, <0>
        szf2 = @SizeStr(%f2)
      ENDM

      ;; ------------------------------------
      ;; Perform the addition on both parts.
      ;; ------------------------------------

      nf = f1 + f2
      ni = i1 + i2

      ;; ---------------------------------------
      ;; Shift any carry into the integer part.
      ;; ---------------------------------------

      IF @SizeStr(%nf) GT szf1
        tf TEXTEQU @SubStr(%nf,2)
        ni = ni + 1
      ELSE
        tf TEXTEQU @SubStr(%nf,1)
      ENDIF
      ti TEXTEQU @SubStr(%ni,1)

      tout CATSTR ti, <.>, tf
      EXITM <tout>

    ENDM

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      x   dq 2147483648.2147483648
      y   dq 2147483647.2147483647
      z   dq 0
      zz  dq FPADD(2147483648.2147483648, 2147483647.2147483647)
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    fld   x
    fadd  y
    fstp  z
    invoke crt_printf, chr$("%f%c"),z,10
    invoke crt_printf, chr$("%f%c"),zz,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

Title: Re: Assemble-time floating point math
Post by: Rockoon on November 04, 2007, 11:05:43 AM
ah string math.. thats another strategy.. tx