Author Topic: A question on FPU  (Read 3750 times)

clamicun

  • Member
  • **
  • Posts: 240
A question on FPU
« on: February 08, 2016, 08:32:53 AM »
;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

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: A question on FPU
« Reply #1 on: February 08, 2016, 08:39:57 AM »
;Is there an "easier" way to convert the content of 'FPU_calc REAL8 ?' into a string - MasmBasics ??
Code: [Select]
deb 4, "Result", FPU_calc
Or, more flexible:
Code: [Select]
Let esi=Str$("The result is %4f", FPU_calc)
PrintLine "[", esi, "]"

Simplest version:
Code: [Select]
Let some$=Str$(FPU_calc)

clamicun

  • Member
  • **
  • Posts: 240
Re: A question on FPU
« Reply #2 on: February 08, 2016, 10:23:45 AM »
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

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: A question on FPU
« Reply #3 on: February 08, 2016, 11:05:01 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

  • Member
  • ****
  • Posts: 532
  • <AMD>< 7-32>
Re: A question on FPU
« Reply #4 on: February 08, 2016, 12:39:40 PM »
I think very littlle is very good!
Code: [Select]
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

jj2007

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: A question on FPU
« Reply #5 on: February 08, 2016, 06:40:04 PM »
Code: [Select]
    cls
    print real8$(FPU_calc), 13, 10
    PrintLine Str$(FPU_calc)

Output:
16.650000
4625379776044361319

Guess why...

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: A question on FPU
« Reply #6 on: February 09, 2016, 12:40:39 AM »
many decimal values cannot be precisely represented in the FPU binary formats
what you are seeing is the closest FPU format value

jj2007

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: A question on FPU
« Reply #7 on: February 09, 2016, 01:10:04 AM »
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

  • Member
  • **
  • Posts: 240
Re: A question on FPU
« Reply #8 on: February 09, 2016, 02:02:55 AM »
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

  • Member
  • **
  • Posts: 240
Re: A question on FPU
« Reply #9 on: February 09, 2016, 02:06:45 AM »
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

  • Member
  • *****
  • Posts: 1454
  • The base type of a type is the type itself
    • SmplMath macros
Re: A question on FPU
« Reply #10 on: February 09, 2016, 02:12:33 AM »
REAL8 to string? =>
Code: [Select]
LOCAL buf[32]:CHAR,r8Value:REAL8
...
fn crt_sprintf, &buf, "%.15E", r8Value
MREAL macros - when you need floating point arithmetic while assembling!

clamicun

  • Member
  • **
  • Posts: 240
Re: A question on FPU
« Reply #11 on: February 09, 2016, 03:09:08 AM »
jj,

assembled "floats.zip"

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

clamicun

  • Member
  • **
  • Posts: 240
Re: A question on FPU
« Reply #12 on: February 09, 2016, 03:52:07 AM »
jj,
one last qestion..

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

Now I need the value as string for further use

jj2007

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: A question on FPU
« Reply #13 on: February 09, 2016, 03:56:26 AM »
btw... Did you take a look on Convert8DR.inc ?

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

They 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

  • Member
  • ****
  • Posts: 532
  • <AMD>< 7-32>
Re: A question on FPU
« Reply #14 on: February 09, 2016, 05:21:48 AM »
Hi Clamicun!

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

qWord help you showing what is behind real8$.

The solution is pretty simple:

Code: [Select]
fn crt_sprintf, addr endvalue, "%lf", FPU_calc



Regards. HSE