News:

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

Main Menu

Printing floating point not working.

Started by Chris., August 16, 2017, 03:40:24 AM

Previous topic - Next topic

Chris.


jj2007

Quote from: Chris. on August 17, 2017, 10:07:02 PM
I added whole code if it helps

It helps:Finished:
   fstp squareroot
   if 1
    .DATA
$interm8 db "%0.20f",13,10,0
    .CODE
invoke sprintf, addr szBuf, offset $interm8, squareroot
   else
; $interm db "%0.4f + %0.4f",13,10,0
mov eax, dword ptr squareroot
mov edx, dword ptr [squareroot+4]
invoke sprintf, addr szBuf, offset $interm, eax, edx
   endif


If I guessed right, you want to print the squareroot, which you declared inconsistently as dq. Assemblers are tolerant, but they should shout foul instead. QWORD is an integer, you want an 8-byte float. They are commonly called "double" in C land, or REAL8 in assembly. As a matter of fact, the invoke macro knows how to handle them.

P.S.: The if 1 ... else ... endif part is called conditional assembly: just put if 0 to build your old version.

raymond

Chris,
To answer your very first question, the answer is YES. Floating point numbers CAN be printed directly from an fpu register. However, if you don't know how to do it, you can use some other external procedures until you have learned how to use assembly, including fpu mnemonics, to do it yourself. One such procedure is included in the Fpulib available on the same site where you read (but did not understand) the description of the 'fist' instruction. If you are really interested, that library also comes with the code for each of the procedures.

QuoteI thought the "fist" instruction allows you to store fpu numbers into memory or variables.
It does allow you to store it into a memory variable but as its ROUNDED INTEGER, rounded according to the content of the fpu's Control Word (http://www.ray.masmcode.com/tutorial/fpuchap1.htm#cword). Once rounded, that integer CANNOT be converted back to the original float; and trying to print that as a float would only yield garbage.

As for your recent question
QuoteWhat if i want two different variables in one string?
, whether you have two, three, or 100+, the only way in assembly is to build the string one variable at a time yourself in a buffer before printing it.
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html