flat assembler
Message board for the users of flat assembler.

Index > Main > Using cinvoke printf

Author
Thread Post new topic Reply to topic
vua72



Joined: 22 Jan 2011
Posts: 2
vua72
I'm trying to use calling of cinvoke for console i/o.
With integers it works fine.
But with floating point it correct works only with dq, not dd format.
What is wrong?

Code:
 format pe; console
 entry start
 include 'win32ax.inc'
 section '.code' code readable executable

start:
   cinvoke scanf, fl, varq
   cinvoke printf, fl, dword[varq], dword[varq+4]
   cinvoke printf, cr
   cinvoke scanf, f4, vard
   cinvoke printf, f4, dword[vard] ; not work
exit:
   invoke ExitProcess, 0;

section '.data' data readable writeable
 cr db 13,10,0
 f4 db '%f', 0
 fl db '%lf', 0
 varq dq ?
 vard dd ?
section '.idata' import data readable writeable
library msvcrt, 'msvcrt.dll', kernel32, 'kernel32.dll'
import msvcrt, printf, 'printf', getchar,'getchar', scanf,'scanf'
import kernel32, ExitProcess,'ExitProcess'    
    
Post 26 Mar 2011, 15:10
View user's profile Send private message Reply with quote
klavs.pr



Joined: 20 Apr 2010
Posts: 20
Location: Latvia
klavs.pr
Not sure, but isn't floating point numbers always at least 64bit long?
Post 26 Mar 2011, 15:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17665
Location: In your JS exploiting you and your system
revolution
'%f' and '%lf' both expect a qword floating point number. Change your line to this and you will see the result.
Code:
   cinvoke printf, f4, dword[varq], dword[varq+4]    
Post 26 Mar 2011, 23:25
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
the printf function only accepts 64bit floats so you'll have to store it as a double:

Code:
fld [vard]
fstp [vartempq]    
Post 27 Mar 2011, 13:55
View user's profile Send private message Reply with quote
vua72



Joined: 22 Jan 2011
Posts: 2
vua72
madmatt wrote:
the printf function only accepts 64bit floats so you'll have to store it as a double:

Code:
fld [vard]
fstp [vartempq]    

Thanks, where I can read about this?
Post 09 Apr 2011, 18:09
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
vua72 wrote:
madmatt wrote:
the printf function only accepts 64bit floats so you'll have to store it as a double:

Code:
fld [vard]
fstp [vartempq]    

Thanks, where I can read about this?


To read more about printf (also scroll down to 'format specifications'):
http://msdn.microsoft.com/en-us/library/wc7014hz.aspx

A little better printf example:
Code:
proc MAIN
     local  mydouble:QWORD, myfloat:DWORD

     mov     [myfloat], 3.141
     fld     [myfloat]
     fstp    [mydouble]
     cinvoke printf, "fp number %f", double [mydouble]
endp    

_________________
Gimme a sledge hammer! I'LL FIX IT!
Post 09 Apr 2011, 21:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.