News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

FPULib and using it

Started by newAsm, July 19, 2010, 02:05:45 AM

Previous topic - Next topic

newAsm

Hi,

I am new to FpuLib and I tried using it in this example:

                                .data
szData1 db "Data 1: "
szNumber1 db "21.4567",0,13,10,0

szData2 db "Data 2: "
szNumber2 db "17.7890",0,13,10, 0

szData3 db "Data 3: "
szNumber3 db "4.5",0,13,10,0

szD1AddD2 db "Data 1 + Data 2: "
szD1AD2 db 16 dup (0),13,10

szD1SubD2 db "Data 1 - Data 2: "
szD1SD2 db 16 dup (0),13,10

szD1MulD2 db "Data 1 * Data 2: "
szD1MD2 db 16 dup (0),13,10

szD1DivD2 db "Data 1 / Data 2: "
szD1DD2 db 16 dup (0),13,10

                               .data?
Data1 dt ? ; REAL10
Data2 dt ? ; REAL10
Data3 dt ? ; REAL10
AddResult dt ? ; REAL10
SubResult dt ? ; REAL10
MulResult dt ? ; REAL10
DivResult dt ? ; REAL10

                .code
main:
......
invoke lstrlen, offset szData1 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szData1, eax, offset inpLen, 0
invoke lstrlen, offset szData2 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szData2, eax, offset inpLen, 0
invoke lstrlen, offset szData3 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szData3, eax, offset inpLen, 0

;*==== Convert data into REAL10 ====
invoke FpuAtoFL,offset szNumber1, offset Data1, DEST_MEM
invoke FpuAtoFL,offset szNumber2, offset Data2, DEST_MEM
invoke FpuAtoFL,offset szNumber3, offset Data3, DEST_MEM

;*==== Process the data ====
invoke FpuAdd, offset Data1, offset Data2, offset AddResult, SRC1_REAL or SRC2_REAL or DEST_MEM
invoke FpuSub, offset Data1, offset Data2, offset SubResult, SRC1_REAL or SRC2_REAL or DEST_MEM
invoke FpuMul, offset Data1, offset Data2, offset MulResult, SRC1_REAL or SRC2_REAL or DEST_MEM
invoke FpuDiv, offset Data1, offset Data2, offset DivResult, SRC1_REAL or SRC2_REAL or DEST_MEM

;*==== Convert the data to ASCII ====
invoke FpuFLtoA, offset AddResult, 0, offset szD1AD2, SRC1_REAL or STR_REG
invoke FpuFLtoA, offset SubResult, 0, offset szD1SD2, SRC1_REAL or STR_REG
invoke FpuFLtoA, offset MulResult, 0, offset szD1MD2, SRC1_REAL or STR_REG
invoke FpuFLtoA, offset DivResult, 0, offset szD1DD2, SRC1_REAL or STR_REG

;*==== Display the data ====
invoke lstrlen, offset szD1AddD2 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szD1AddD2, eax, offset inpLen, 0

invoke lstrlen, offset szD1SubD2 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szD1SubD2, eax, offset inpLen, 0

invoke lstrlen, offset szD1MulD2 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szD1MulD2, eax, offset inpLen, 0

invoke lstrlen, offset szD1DivD2 ; EAX = string length
invoke WriteFile, hConsoleOut, offset szD1DivD2, eax, offset inpLen, 0


What I got back were in integers only and not float. How can I get the float value back? Why did I use REAL10 - because FpuLib uses it (I think).

Hope you can help me sort this one out. Thanks..GS

Added code tags

MichaelW

For your statements of the form:

invoke FpuFLtoA, offset AddResult, 0, offset szD1AD2, SRC1_REAL or STR_REG

The second parameter should specify the number of decimal digits.
eschew obfuscation

raymond

invoke FpuFLtoA, offset AddResult, 0, offset szD1AD2, SRC1_REAL or STR_REG

If you read the description of the FpuFLtoA function in the Help file, the second parameter SRC2 must indicate the number of fractional decimal places (within the described limits) required for display. By indicating "0" in your calls to the function, you are requesting only the integer portion (i.e. none of the fractional decimal places).

Should you decide to try for the maximum, I would then also suggest that you increase the available space for storing the ascii string from 16 to at least 19 bytes. The string may contain up to 16 digits, a decimal delimiter, possibly a "-" sign if negative, and the terminating 0, for a total of 19 characters.

Also, your line feed and carriage return characters should be as a separate string. Otherwise, they may never be displayed as intended by trying to embed them in the same string as the converted float.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

newAsm

Thank you very much. I missed the documentation on FpuFLtoA on the second parameter and it is now working. Thanks a lot.

The library helped a lot because the thought of writing floating point routines is scary.

Thanks for library as well. Thanks to all concerned and your correction...newAsm

dedndave

it isn't scary at all - in fact, it's fun   :P
have a look at Raymond's FPU tutorial   :U

raymond

I don't know if you are using the latest version of the Fpulib. You can always find the latest one at the following site which is maintained by the author:

http://www.ray.masmcode.com/fpu.html#fpulib

The latest one was expanded to allow the use of REAL4 and REAL8 in addition to the REAL10 type which used to be the only one allowed.

The FPU tutorial mentioned by Dave is also available from the above URL.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dedndave

learning to use the FPU is miles easier than learning SSE - lol

.......... and, all processors that run windows 98 on up have FPU's   :bg