The MASM Forum

General => The Campus => Topic started by: PatrickDS on July 16, 2020, 11:48:11 PM

Title: Converting floating point numbers to strings
Post by: PatrickDS on July 16, 2020, 11:48:11 PM
I am learning assembler for a few days now (I usually program with VB.net or Java)
and wanted to do some basic calculations with floating-point numbers to understand the logic of the FPU.


finit                       
fld    dword ptr [value1]
fld    dword ptr [value2]
fadd                             
fstp   dword ptr [sum]


My problem is that I am not sure how to convert my result in [sum] to the screen.

With unsigned integers I usually use:

print str$(sum)


But that doesn't work! I know that this is basic stuff, but I'm really frustrated.  :sad:

I hope that you can help me. Thank you already in advance.  :biggrin:
Title: Re: Converting floating point numbers to strings
Post by: hutch-- on July 17, 2020, 12:13:55 AM
I am working in 64 bit these days but see if one of the MSVCRT functions like "_sprintf" will work for you. In 32 bit MASM32 there is the C runtime function "crt_sprintf". Check the Microsoft documentation to see if it works.
Title: Re: Converting floating point numbers to strings
Post by: HSE on July 17, 2020, 12:14:47 AM
Welcome to the forum!

If you have declared:value1 real4 0.0
value2 real4  0.0
sum real4 0.0
you can use:print real4$(sum)
Same thing with real8:.data
value1 real8 2.0
value2 real8  5.0
sum real8 0.0
.code
finit                       
fld    value1
fld    value2
fadd                             
fstp   sum
print real8$(sum)


You also can use FPU for integers width Fild,,fistp and str$.
Title: Re: Converting floating point numbers to strings
Post by: PatrickDS on July 17, 2020, 12:30:55 AM
Thanks, that helped very much.  :biggrin: :thup:


Quote from: HSE on July 17, 2020, 12:14:47 AM
Welcome to the forum!

If you have declared:value1 real4 0.0
value2 real4  0.0
sum real4 0.0
you can use:print real4$(sum)
Same thing with real8:.data
value1 real8 2.0
value2 real8  5.0
sum real8 0.0
.code
finit                       
fld    value1
fld    value2
fadd                             
fstp   sum
print real8$(sum)


You also can use FPU for integers width Fild,,fistp and str$.
Title: Re: Converting floating point numbers to strings
Post by: raymond on July 17, 2020, 01:31:25 AM
Warm welcome to this forum.

Quotewanted to do some basic calculations with floating-point numbers to understand the logic of the FPU

Did you have a look at this (which is really a part of this site):  http://www.ray.masmcode.com/fpu.html

Title: Re: Converting floating point numbers to strings
Post by: PatrickDS on July 17, 2020, 05:42:16 AM
Yeah, that's the page where I learned the basic mnemonics for the FPU.  :smiley:
Title: Re: Converting floating point numbers to strings
Post by: raymond on July 17, 2020, 09:49:08 AM
You can also use the available library to perform a number of functions, and to also learn how those can be coded to avoid the overhead (and improve speed); the algo for all the functions is provided.