News:

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

Main Menu

A question on FPU

Started by clamicun, February 08, 2016, 08:32:53 AM

Previous topic - Next topic

clamicun

;this does it - why ... I still am on problems with "FPU"

include \masm32\include\masm32rt.inc
include Convert8DR.inc   ;You have to download the zipFile to make it working

;=========
show_string MACRO arg1,arg2
LOCAL ws_msg
LOCAL stringlabel

.data
ws_msg    TCHAR 60 dup(?),0
stringlabel TCHAR '%s',0

.code
    pushad
    INVOKE wsprintf,addr ws_msg,addr stringlabel,reparg(arg2)
    INVOKE MessageBox,0,addr arg1,addr ws_msg,0
    popad
ENDM
;==========

.data
startvalue db "0.00",0
endvalue   db 20 dup(?),0

value1 db "1.11",0
value2 db "2.22",0
value3 db "3.33",0
value4 db "4.44",0
value5 db "5.55",0

FPU_calc    REAL8 ?
;-----------------

.code
start:

call Calculate


INVOKE ExitProcess,0

;============
Calculate proc

FINIT  ; It is good programming practice to take the precaution of initializing FPU before starting any computation
FFREE st(0)

show_string startvalue,"start value"

INVOKE crt_atof,offset startvalue
INVOKE crt_atof,offset value1
fadd st(0),st(1)                 ; add ST(0)+ST(1)

INVOKE crt_atof,offset value2
fadd st(0),st(1)                

INVOKE crt_atof,offset value3
fadd st(0),st(1)             

INVOKE crt_atof,offset value4
fadd st(0),st(1)   
             
INVOKE crt_atof,offset value5
fadd st(0),st(1)

fstp FPU_calc         ;Store real number and pop ST(0)

;So the result is in st(0)
;I use 'Convert8DR.inc' written by RuiLoureiro,- it works well, but I do not understand his macros... 
;Is there an "easier" way to convert the content of 'FPU_calc REAL8 ?' into a string - MasmBasics ??
 
INVOKE  ConvertReal8DR, offset FPU_calc, offset endvalue

show_string endvalue,"result add"

ret
Calculate endp
;============

end start

jj2007

;Is there an "easier" way to convert the content of 'FPU_calc REAL8 ?' into a string - MasmBasics ??
deb 4, "Result", FPU_calc

Or, more flexible:
Let esi=Str$("The result is %4f", FPU_calc)
PrintLine "[", esi, "]"


Simplest version:
Let some$=Str$(FPU_calc)

clamicun

Hi jj
Did you really check this ?

;my code
;So the result is in st(0)
INVOKE  ConvertReal8DR, offset FPU_calc, offset endvalue
show_string endvalue,"result add"   ;correct result

;and now what ?
;===
;your codes
;deb 4, "Result", FPU_calc       ; ???

;Or, more flexible:
;Let esi=Str$("The result is %4f", FPU_calc)             ;no result !!!
;PrintLine "[", esi, "]"  ;assembled as console           -Nothing

;Simplest version:
;Let some$=Str$(FPU_calc)                                    ;Error "some" not defined

;===

jj2007

Quote from: clamicun on February 08, 2016, 10:23:45 AM
Hi jj
Did you really check this ?

Of course. But if you use some$, you must define it previously ;-)
Full code attached. I wonder what ConvertReal8DR does.

> ;So the result is in st(0)
Well, not really. You popped it, right?

HSE

I think very littlle is very good!

include \masm32\include\masm32rt.inc
   
.data
    startvalue qword 0.0
    value1 qword 1.11
    value2 qword 2.22   
    value3 qword 3.33
    value4 qword 4.44
    value5 qword 5.55
    FPU_calc qword 0.0

.code

start:
    call main
    inkey
    exit

main proc

    finit
    fld startvalue
    fadd value1
    fadd value2
    fadd value3
    fadd value4
    fadd value5
    fstp FPU_calc

    cls
    print real8$(FPU_calc), 13, 10

  ret

main endp
end start
Equations in Assembly: SmplMath

jj2007

    cls
    print real8$(FPU_calc), 13, 10
    PrintLine Str$(FPU_calc)


Output:
16.650000
4625379776044361319

Guess why...

dedndave

many decimal values cannot be precisely represented in the FPU binary formats
what you are seeing is the closest FPU format value

jj2007

In this case, the explanation is much simpler: Str$() thinks it's a qword integer:

FPU_calc qword 0.0  ; if the assembler was mean, it would throw an error for this abuse of types ;-)

clamicun

To HSE,
sometimes very little is not good enough...

The values can not be qwords or dwords. They are "strings" and are downloaded from a database.

print real8$(FPU_calc), 13, 10 is fine, but I need the "endvalue".
Thank you

clamicun

To jj,
Convert8DR.inc. Written by:   RuiLoureiro         @masmforum
What it does?
It is attached. So check it out.
Like I said. I do not get it entirely.

qWord

REAL8 to string? =>

LOCAL buf[32]:CHAR,r8Value:REAL8
...
fn crt_sprintf, &buf, "%.15E", r8Value
MREAL macros - when you need floating point arithmetic while assembling!

clamicun

jj,

assembled "floats.zip"

Works perfectly.
Thanks a lot.
Mic
btw... Did you take a look on Convert8DR.inc ?

clamicun

jj,
one last qestion..

Let esi=Str$("%4f",FPU_calc)
PrintLine  esi

Now I need the value as string for further use

jj2007

Quote from: clamicun on February 09, 2016, 03:09:08 AM
btw... Did you take a look on Convert8DR.inc ?

I took a look. Resembles my Float2Asc routine - 500+ lines of scarcely commented code ::)

Quote from: clamicun on February 09, 2016, 02:02:55 AMThey are "strings" and are downloaded from a database.

So you want to process a textfile? Can you post an example of that database, and pseudo code what you want to do with it? StringToArray is pretty powerful for such things.

> Now I need the value as string for further use
esi is a string. You can print it, use it in a MsgBox, concatenate it, put it in an array, write it to disk...

Let esi=Str$("%4f",FPU_calc)
FileWrite "SomeFile.txt", esi
Let esi="Hello "+esi+" World"
Let My$(0)="["+esi+"]"
...

HSE

Hi Clamicun!

Of course, just a game. The clue was "real8$".

qWord help you showing what is behind real8$.

The solution is pretty simple:

fn crt_sprintf, addr endvalue, "%lf", FPU_calc



Regards. HSE
Equations in Assembly: SmplMath