Here are some macros to declare and invoke cdecl functions.
Hope to be of use to someone.
Posted on 2002-12-12 06:05:40 by rdo
Yes,the cdecl functions are interesting,you can even use the string functions from C run-time dlls.

Regards,

Vortex
Posted on 2002-12-12 08:44:54 by Vortex
Why does invoke work with _cdecl functions already without this macro?
Posted on 2002-12-12 15:03:19 by CyberGuy
Parameter passing order for both conventions is the same, the diference is that stdcall functions clear stack before exit while cdecl does not.
So invoke macro works for cdecl but after it you have to clear the stack by yourself.
Posted on 2002-12-13 02:13:45 by rdo
Thanks, BTW this is very useful for me.
Posted on 2002-12-13 15:19:52 by CyberGuy
Rdo thanks for your macros,here is an example:




format PE CONSOLE 4.0

include 'include\macro\stdcall.inc'
include 'include\macro\import.inc'
include 'cproc.inc'

section '.data' data readable writeable
welcome db 'Hello friends!',13,10,13,0
upr db 'this an example',0
m1 db 'strupr("this an example")=%s',13,10,0
m2 db 'Lenght of this string=%d',13,10,0
m3 db 'strcat("Flat ","assembler")=%s',13,10,0
string1 db 'Flat '
rb 9
db 0
string2 db 'assembler',0
section '.code' code readable executable

entry start

start:

cinvoke printf,welcome
cinvoke strupr,upr
cinvoke printf,m1,upr
cinvoke strlen,upr
cinvoke printf,m2,eax
cinvoke strcat,string1,string2
cinvoke printf,m3,string1

invoke ExitProcess,0
section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
msvcrt,'msvcrt.dll'

kernel:
import ExitProcess,'ExitProcess'

msvcrt:
import printf,'printf',\
strupr,'_strupr',\
strlen,'strlen',\
strcat,'strcat'

Posted on 2002-12-14 04:43:58 by Vortex
I'm glad you guys like the macros. Here is another sample: qsort the command line arguments


format PE console
entry start

include 'include/macro/import.inc'
include 'include/macro/stdcall.inc'
include 'cproc.inc'

section '.data' data readable writeable
argc dd ?
argv dd ?
env dd ?
fmt db '%s ',0dh,0ah,0

section '.code' code readable executable

start:


cinvoke __getmainargs,argc,argv,env,0

dec [argc]
add [argv],4
cinvoke qsort,[argv],[argc],4,compare

jmp tst
next:
mov eax,[argv]
cinvoke printf,fmt,dword [eax]
add [argv],4
dec [argc]
tst:
cmp [argc],0
je exit
jmp next

exit:
invoke ExitProcess,0

cproc compare,.arg1,.arg2
center
mov eax,[.arg1]
mov ecx,[.arg2]
cinvoke strcmp,dword [eax],dword [ecx]
cret

section '.idata' import readable writeable
library kernel,'kernel32.dll',\
msvcrt,'msvcrt.dll'

kernel:
import ExitProcess,'ExitProcess'

msvcrt:
import __getmainargs,'__getmainargs',\
printf,'printf',\
qsort,'qsort',\
strcmp,'strcmp'
Posted on 2002-12-14 07:25:52 by rdo
Yes,nice idea of using the qsort function

Regards,

Vortex
Posted on 2002-12-14 11:34:52 by Vortex
Nice.:)
However, on WinXP, I have a problem with stack frame, if is used similar.
Fix it, in "center" macro:
dynamic_size = ($ - dynamic_data + 3) - ($ - dynamic_data + 3) mod 4
(Stack align on dword).
Thanks.
Posted on 2002-12-15 00:19:58 by tserk
thanks
here is updated:
Posted on 2002-12-15 05:39:47 by rdo
MSVCRT.DLL, is credible, will be always supported by MS.
At any event, she is in the Winxp.
Thanks for the nice idea, Rdo!
PS
Special thanks initial author, Vortex ;)
Posted on 2002-12-15 07:18:02 by tserk
You are welcome Tserk.

Here is another example for real numbers:




format PE CONSOLE 4.0

include 'include\macro\stdcall.inc'
include 'include\macro\import.inc'
include 'cproc.inc'

section '.data' data readable writeable
pi dq 3.141592
msg db 'PI=%f',0
section '.code' code readable executable

entry start

start:
mov esi,pi
cinvoke printf,msg,dword [esi],dword [esi+4]
invoke ExitProcess,0

section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
msvcrt,'msvcrt.dll'

kernel:
import ExitProcess,'ExitProcess'

msvcrt:
import printf,'printf'



This example works fine under WinXP. Tserk,Rdo if you find a bug
I will be gratefull for your feedback

Regards,

Vortex
Posted on 2002-12-16 03:11:28 by Vortex
The Win98 on my PC.
Acces to WINXP is limited :(
Posted on 2002-12-16 03:26:15 by tserk
No problem Tserk,These examples will
work fine on Win9.x/XP

Regards,

Vortex
Posted on 2002-12-16 03:33:01 by Vortex
Vortex, you should be aware that win95 is not shipped with msvcrt, it comes after it, in win95 osr2 and with ie but don't remember wich version. I remember there is a thread about that somewhere in win32asm forums.

Missing msvcrt is very very rare case but just to warm you.
Posted on 2002-12-16 04:01:56 by rdo
Thanks Rdo,Win95b contains the MS VC Run-time library(msvcrt.dll) Maybe not %100 but,crtdll.dll can be
an alternative for msvcrt.dll.

The problem is that I haven't a manual with detailed descriptions for these dlls (just like win32.hlp
for WinAPI)

Regards,

Vortex
Posted on 2002-12-16 04:15:00 by Vortex
For the moment,maybe the only way to explore the functions of C run-time dlls is to examine the EXEs while
debugging.

A small example:




#include <stdio.h>
void main()
{
char *friend="amigo";
printf("Hello %s",friend);
}



Thanks to Maverick's nice method,compile with:

CL.EXE /I\vc6\include /c /Ogtyb2 /Gs /G6 /Gz /Zp1 /FAs /Fa%1.asm /Fo%1.OBJ %1.C
\masm32\bin\link /ENTRY:main /SUBSYSTEM:CONSOLE /LIBPATH:c:\masm32\lib %1.obj msvcrt.lib

After running Olly debugger,
Posted on 2002-12-16 05:41:34 by Vortex


..test_number = 89ABCDEFh
push ..test_number

ccall ...
cinvoke ...
invoke ...
stdcall ...

pop eax
.if eax,e,..test_number
invoke MessageBox,0,stack_ok,stack_ok,0
.endif

Thanks :)
Posted on 2002-12-17 05:18:46 by tserk