How do use floating point functions FpuFLtoA and FpuAtoFL correctly?

Started by etow, February 17, 2008, 08:27:36 PM

Previous topic - Next topic

etow

Hi

I am having trouble using the floating point functions FpuFLtoA and FpuAtoFL correctly.
I wish there were more examples to show me.  The help file only gave me one example.

Please help me.

raymond

The help file describes in relatively good detail what is expected from the parameters you pass on to the functions, and what is returned by the functions. It's impossible to provide examples to cover all the combinations of the various parameters.

If you post specific questions related to those various parameters which you may not understand, we will attempt to enlighten you.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Draakie

Does this code make me look bloated ? (wink)

etow

hi

here is my small program and I am not sure why it does not run but gives me a Microsoft error message because it crashes:


.686 ; create 32 bit code
.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

;The MASM32 Runtime Library include file.
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\masm32rt.inc
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\fpulib\fpu.inc
IncludeLib   \masm32\fpulib\fpu.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.Data
Number  Real10 ?
FourDecimals Real10 4.0
modifyNumber Real10 ?

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;<<   Input10 procedure will convert input   <<
;<<   from keyboard into real10 numbers      <<
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

InputR10 Proc pR10:DWord   ;Ptr Real10 the pointer to Real10 value is a DWORD value
    LOCAL r8:REAL8
    invoke crt_scanf, SADD("%lf"), ADDR r8
    mov eax, pR10
    finit
    fld r8
    fstp REAL10 PTR [eax]
    ret
InputR10 EndP

Main Proc ; preserve registers Ebx and Esi
Local WantContinue:DWord

   Mov WantContinue, 0   ; initialize WantContinue to zero

  .While ((WantContinue >= 0) && (WantContinue < -1))
  ; while WantContinue is greater than or equal to 0 And
  ; WantContinue is less than -1 do
  print chr$(13, 10)  ; a carriage return line feed will be outputted to the
                      ; screen
    print chr$("Enter a real number : ")
    Invoke InputR10, Addr Number

    Invoke FpuFLtoA, Addr Number, Addr FourDecimals, Addr modifyNumber, SRC1_FPU
    Or Eax, Eax
    Jz _Exit
    print real10$ (modifyNumber)

_Exit:
    Mov WantContinue, sval(input("Enter -1 to quit program : "))
  .EndW
    print chr$(13, 10)
    Ret
Main EndP

End start

ToutEnMasm

Hello
Perhaps an answer here,
http://www.masm32.com/board/index.php?topic=8156.msg59515#msg59515
And if it is,the soluce
http://www.masm32.com/board/index.php?topic=8022.msg58718#msg58718]

Try also to use only REAL8 (enough precision),I am not sure but for some FPU the format of a real10 is only internal,not IEEE
A debugger is also of good use to know where is the problem.Further are free


Draakie

Does this code make me look bloated ? (wink)

etow

Hi Draakie,

I modified my code but it still crashes.
Here is my code below:
-----------------------------------------------------------


.686 ; create 32 bit code
.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

;The MASM32 Runtime Library include file.
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\masm32rt.inc
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\fpulib\fpu.inc
IncludeLib   \masm32\fpulib\fpu.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.Data
Number  Real10 ?
modifyNumber Real10 ?

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;<<   Input10 procedure will convert input   <<
;<<   from keyboard into real10 numbers      <<
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

InputR10 Proc pR10:DWord   ;Ptr Real10 the pointer to Real10 value is a DWORD value
    LOCAL r8:REAL8
    invoke crt_scanf, SADD("%lf"), ADDR r8
    mov eax, pR10
    finit
    fld r8
    fstp REAL10 PTR [eax]
    ret
InputR10 EndP

Main Proc ; preserve registers Ebx and Esi
Local WantContinue:DWord

   Mov WantContinue, 0   ; initialize WantContinue to zero

  .While ((WantContinue >= 0) && (WantContinue < -1))
  ; while WantContinue is greater than or equal to 0 And
  ; WantContinue is less than -1 do
  print chr$(13, 10)  ; a carriage return line feed will be outputted to the
                      ; screen
    print chr$("Enter a real Number : ")
    Invoke InputR10, Addr Number

    Invoke FpuFLtoA, Addr Number, 4, Addr modifyNumber, SRC1_FPU
    Or Eax, Eax
    Jz _Exit
    print real10$ (modifyNumber)

_Exit:
    Mov WantContinue, sval(input("Enter -1 to quit program : "))
  .EndW
    print chr$(13, 10)
    Ret
Main EndP

End start


Please help much help appreciated

Thanks

raymond

Quote.Data
Number  Real10 ?
modifyNumber Real10 ?
...
...
Invoke FpuFLtoA, Addr Number, 4, Addr modifyNumber, SRC1_FPU

etow
I don't know what is your level of knowledge of the English language. If it's poor, you may want to improve on that aspect before continuing to use libraries with the help file written in English.
If you look at the FpuFLtoA function in the provided help file, you would find for the description of the first parameter:
QuotelpSrc1

This points to an 80-bit REAL number. (This parameter is ignored if uID indicates that the value is already on the FPU.)

And then if you look at the meaning of the various flags for the uID parameter, you do find the following:
QuoteSRC1_FPU     Src1 is already on the FPU

So, you are calling the function by telling it that the REAL10 you want to convert is already on the FPU and to disregard the first parameter!!!

Furthermore, the third parameter is described as follows:
QuotelpszDest

This points to a memory location where the result will be returned. It must be large enough to hold the number of characters returned. At most, it may have 17 characters before the decimal point, one decimal point, 15 decimal digits, and the terminating zero (for a maximum of 34 characters).

And, you provide the address of a variable which is declared as covering only 10 bytes (REAL10).  In some cases, it may be enough but in other cases you may be overwriting the data in other variables.

The function should thus have returned the error code 0 in EAX because it would not have found any valid data in the FPU's top data register (as far as I can tell, it would have been empty). And by checking the return value, your code would simply have jumped over the print instruction. If your program is crashing, it's probably due to some other cause.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

etow

Hi
I read carefully the help file I modified my code below but it still crashes half way into outputting
the real number.  Please help.


.686 ; create 32 bit code
.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

;The MASM32 Runtime Library include file.
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\masm32rt.inc
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\fpulib\fpu.inc
IncludeLib   \masm32\fpulib\fpu.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.Const
decimalPlace = 15

.Data
Number Real10 ?
modifyNumber Byte 100 Dup(?)

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;<<   Input10 procedure will convert input   <<
;<<   from keyboard into real10 numbers      <<
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

InputR10 Proc pR10:DWord
    LOCAL r8:REAL8
    invoke crt_scanf, SADD("%lf"), ADDR r8
    mov eax, pR10
    finit
    fld r8
    fstp REAL10 PTR [eax]
    ret
InputR10 EndP

Main Proc

Local WantContinue:DWord

   Mov WantContinue, 0   ; initialize WantContinue to zero

  .While ((WantContinue >= 0) && (WantContinue < -1))
  ; while WantContinue is greater than or equal to 0 And
  ; WantContinue is less than -1 do
  print chr$(13, 10)  ; a carriage return line feed will be outputted to the
                      ; screen
    print chr$("Enter a real Number : ")
    Invoke InputR10, Addr Number

    print chr$(13, 10, " Outputting the real10 floating point Number :  ")
    print real10$ (Number)
    print chr$(13, 10, " by using real10$ function .", 13, 10, 13, 10)

    Invoke FpuFLtoA, Addr Number, decimalPlace, Addr modifyNumber, SRC1_REAL Or STR_REG
    Or Eax, Eax
    Jz _Error
    print chr$(" Displaying the real10 floating point Number :  ")
    print str$(modifyNumber)
    print chr$(13, 10, " by using FpuFLtoA function to ")
    print str$(decimalPlace)
    print chr$(" decimal places .")
    print chr$(13, 10, 13, 10)
    Jmp _Continue

_Error:
print chr$(13, 10)
    print chr$(" Error displaying floating point number as string ")
    print chr$("by using FpuFLtoA function!", 13, 10, 13, 10)

_Continue:
    Mov WantContinue, sval(input("Enter -1 to quit program : "))
    Invoke ClearScreen
  .EndW

    print chr$(13, 10)
    Ret
Main EndP

End start

ToutEnMasm

Hello,
After testing it :
Minor error,the lines are defined in masm32rt
Quote
.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

Major error
Numbers have sizes and functions works for a defined size
You pass a buffer to the macro str$ (print str$(modifynumber)
This wrong,the macro use dwtoa that work only with dword,dwtoa crash
correction:

Quote
print chr$(" Displaying the real10 floating point Number : ")
print addr modifyNumber                                       ;crash was here print str$(modifynumber)
print chr$(13, 10, " by using FpuFLtoA function to ")