flat assembler
Message board for the users of flat assembler.

Index > Windows > problem with printf

Author
Thread Post new topic Reply to topic
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
hello:

I have this little program that uses printf from msvcrt.dll:

Code:

format pe console

include 'win32ax.inc'
entry start

section 'cody' code executable
start:




        cinvoke printf,form,[var1]
        invoke ExitProcess,0


section 'daty' data readable writeable
        var1 dd 23.0
        form db '%f',0


section 'imp' import data readable
        library kernel32,'kernel32.dll',\
        msvcrt,'msvcrt.dll'

        import kernel32,ExitProcess,'ExitProcess'

        import msvcrt,printf,'printf'\
       ,scanf,'scanf'


    


it should print 23.0000 ([var1])
but only prints -1.482884 and always this same number no matter what number I define in var1 . When i debug the program with ollydbg it recognize the number: <%f>=23.00000 but when the printf gets called
it only prints -1.482884

what I'm doing wrong?

thanks.
Post 30 Jul 2007, 05:32
View user's profile Send private message Reply with quote
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
sorry: I use the 'search' and found the solution in another topic:

it should be defined as qword and passed as 2 dwords to the printf

but....
does this mean there is no function to print single-precision numbers?

so if I define a float variable in a c program it's really defined as double?

I'm confused.
Post 30 Jul 2007, 05:58
View user's profile Send private message Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 245
Location: US
Yardman
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 02:28; edited 1 time in total
Post 30 Jul 2007, 06:26
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
this really depends on platform. Microsoft libc could treat float as single precision, while glibc could treat it as double precision. You should first find out more about platform whose libc you want to use. And if you want portability, you can't go without at least some "compatiblity layer", like Yardman demonstrated
Post 30 Jul 2007, 09:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
The C standard specifies that if you don't have a function prototype or use vararg, char/short are passed as int, and float types are passed as double. Check section 2.7, type conversions.
Post 30 Jul 2007, 14:32
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
In other words, you can push a 4 byte onto the FPU and pull it, or you can do something else... I forget what it is. I think it was push a 0 befor or after you push the float you want to display.
Post 31 Jul 2007, 00:35
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Yardman



Joined: 12 Apr 2005
Posts: 245
Location: US
Yardman
[ Post removed by author. ]


Last edited by Yardman on 04 Apr 2012, 02:28; edited 1 time in total
Post 31 Jul 2007, 05:13
View user's profile Send private message Reply with quote
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
thanks Yardman I'll use that.

vid: so if I link to msvcrt.dll will I have problems runing my program in different
versions of windows?


thanks.
Post 31 Jul 2007, 05:32
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
vid: so if I link to msvcrt.dll will I have problems runing my program in different versions of windows?

no, most probably no. But you may have problems if you decide to use another C library, not one from MS.
Post 31 Jul 2007, 10:12
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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.

Powered by rwasa.