News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

multiplying/dividing a positive decimal number by a positive number

Started by shankle, February 28, 2015, 01:57:08 PM

Previous topic - Next topic

Yuri

I doubt it's a question of format. The code works correctly for me with just "%g".
Quote
---------------------------
Result
---------------------------
9980.69
---------------------------
ОК   
---------------------------

shankle

        3-23-2015
My knowledge about FPU is hovering just above Zero.

    fixed dq  12.0
    n4    dq  0
    buf7  db  8 DUP (0h)
    Value db  '.043'

How do I put .043 into the ST0 FPU register
The result from Fild is incorrect

    fild D[Value] ;load Value into the ST0 register on the fpu stack
    fdiv Q[fixed] ; divide it by 12.0 directly from memory
    fstp Q[n4]    ; pop the results off the stack to memory
    invoke msvcrt:sprintf_s,addr buf7,sizeof buf7,\
                   "%g",[n4]

    invoke TextOut, [hdc],200,425,addr buf7,8
   

jj2007


FORTRANS

Hi,

   FILD is to load from an integer variable.  FLD loads from a real
valued variable.


Fixed   DW      12
Real    DD      12.0

        FILD    [Fixed]
        FLD     [Real]


   Both end up with 12.0 in the FPU.  But look at a listing to see
some differences.

Regards,

Steve N.

dedndave

Value db  '.043'

loading it into the FPU is actually the easy part
your big problem here is converting an ASCII string into a float

now, there are a few ways to go about this
one way is to use the masm32 library StrToFloat function

    .DATA

r08Value REAL8 ?
Value db  '.043'

    .CODE

    INVOKE  StrToFloat,offset Value,offset r08Value

    fld     r08Value     ;load REAL8 into FPU ST(0)


the StrToFloat function only supports REAL8 types

i guess, for GoAsm, the "offset" syntax is a little different

dedndave

another way would be to convert "0.043" to a fixed-point integer of 43
load the integer and divide it by a constant real of 1000.0

still another way might be to load it using BCD math - i'm not very familiar with that
but, Ray's tutorials and libraries might help

shankle

So where is Ray's tutorial?
I read about the lib from 2012 and it had errors.
Have they been fixed?
I will gladly give it a read if I can find it.

jj2007

Quote from: shankle on March 24, 2015, 12:11:29 PM
So where is Ray's tutorial?

Google is your friend: http://www.ray.masmcode.com/tutorial/index.html

dedndave


rrr314159

this tutorial was my "bible" learning the FPU - thanks Raymond!
I am NaN ;)

shankle

Thanks guys,
I should have known about this weeks ago.
In my short perusal so far I think "fpulib" is a gold mine.

BUT, FpuAtoFl shows an error for this instruction.
So I need the equivalent of Raymonds tutorial for GoAsm.
I don't think it exists. :(

And yes, Thanks Raymond :biggrin:

dedndave

FpuAtoFl sounds like one of Ray's library functions - not the same as an instruction
also - Ray's code is probably 32-bit
you may have to adapt to 64-bit code

Yuri

shankle
What happened to _atodbl? Did it stop working for you?


DATA SECTION

fixed dq  12.0
n4    dq  0
buf7  db  16 DUP (0h)
Value db  '.043',0

CODE SECTION

Start:
    invoke msvcrt:_atodbl, addr buf7, addr Value
    fld Q[buf7]
    fdiv Q[fixed]
    fstp Q[n4]
    invoke msvcrt:sprintf_s, addr buf7, sizeof buf7, "%g", [n4]
    invoke user32:MessageBoxA, 0, addr buf7, "Result", 0


Quote
---------------------------
Result
---------------------------
0.00358333
---------------------------
ОК   
---------------------------

shankle

Sorry Yuri,
Nothing wrong with atodbl. Guess I thought Ray's stuff would
replace Msvcrt. So if I understand correctly GoAsm will not
support Rays tutorial and I must use Microsofts Msvcrt:xxxxx.
Sorry for the confusion.

Yuri

The tutorial describes how the FPU works, so it's definitely useful. But fpu.lib is 32-bit, so you can only use it in 32-bit code, like this:


DATA SECTION

DEST_MEM8   EQU   200000h
SRC1_REAL8  EQU   20000h
SRC2_DIMM   EQU   800h
fixed dq  12.0
n4    dq  0
buf7  db  16 DUP (0h)
Value db  '.043',0

CODE SECTION

Start:
    invoke fpu.lib:FpuAtoFL, addr Value, addr buf7, DEST_MEM8
    fld Q[buf7]
    fdiv Q[fixed]
    fstp Q[n4]
    invoke fpu.lib:FpuFLtoA, addr n4, 8, addr buf7, SRC1_REAL8 | SRC2_DIMM
    invoke user32:MessageBoxA, 0, addr buf7, "Result", 0
    ret


Quote
---------------------------
Result
---------------------------
0.00358333
---------------------------
ОК   
---------------------------