I can call _printf in nasm with "extern" directive, but it does not work in masm32!
Posted on 2003-04-19 08:22:11 by lbxxy

I think that this function is part of the C runtimes you would need the MSVCRT library :

Posted on 2003-04-19 10:12:41 by donkey
There is a limited version of "printf" called wsprintf in Win32api ...check the SDK
and take care as it is using "C" calling conventions unlike all other WIn32API that uses STDCALL convention
Posted on 2003-04-19 10:30:35 by BogdanOntanu
Afternoon, lbxxy.

Attached is an example using "printf".

A couple of things to note:

1) The prototype for functions similar to printf (i.e. those functions which use 'C' calling conventions and have variable arguments) is like so: printf Proto C :VARARG

2) This is a console program, so you have to reassemble it as a console (i.e. "QEditor->Project->Console Assemble & Link")

Posted on 2003-04-19 21:50:56 by Scronty
Thank you donkey,BogdanOntanu,Scronty, it works just fine.
Posted on 2003-04-20 03:40:16 by lbxxy
Thanks for your souce code, Scronty! it works well , but I have a knew question about your source code .

The 'printf ' is use 'C' call convention . the difference between 'C' and 'stdcall' call convention is who clear the parameter from stack.

-> .model flat, stdcall
-> invoke printf,ADDR szprintftem, dwANumber, ADDR szAString

should I use 'pop' three times here? or function 'ExitProcess' automatic clear?

-> ; loop until "Esc" is pressed (and then exit)
-> @@:
-> invoke GetAsyncKeyState,VK_ESCAPE ; "Esc" to exit
-> .if eax > 1
-> jmp @F
-> .endif
-> jmp @B
-> @@:
-> invoke ExitProcess, NULL
Posted on 2003-04-20 04:13:41 by lbxxy
MASM will generate some code (ie add esp, 0Ch) to "restore" the value of esp. Thus there is no need for 3 pops.
Posted on 2003-04-20 07:01:26 by roticv
Afternoon, lbxxy.

To elaberate on what roticv has said:
If you declare a PROTO as using the 'C' calling convention (i.e. printf Proto C :VARARG), then the assembler will add the necessary value to esp (ie add esp, 0Ch) automagically (if you use the invoke macro).

If you don't tell Masm it's a function which uses 'C' calling conventions, then you do have to manually restore esp yourself (i.e. if you use a bunch of "push blah"s and then "call thefunction").

Remember this if/when you move to another assembler which doesn't restore esp for functions which use "C" calling convention automagically.

Posted on 2003-04-20 07:12:03 by Scronty