How do you get the parameters of the program from the stack?
Like in C:

int main(int argc, char *argv[])

I know it involves the esp register.
Could someone help me out?
Posted on 2008-02-07 09:38:06 by ASMFreak
typically esp = return va, esp+4 = param1 and so on
for ebp frames its = param1 and so on...

this you could have found out for yourself via some debugging..
Posted on 2008-02-07 10:01:06 by evlncrn8
Depending on what calling convention you want to use:




I have found StdCall to be the most efficient though.
Posted on 2008-02-07 10:22:39 by XCHG
Thanks XCHG :D That helped tons.
But why does nasmx give syntax errors on this:
mov eax, dword ptr

Error: comma or end of line expected
And yes i have an end of line ;)

Posted on 2008-02-07 10:40:03 by ASMFreak
There is no such thing as PTR in NASM.

You have to declare PTR before you use it. I also define OFFSET:

%IDEFINE OFFSET
%IDEFINE PTR


Posted on 2008-02-07 10:47:45 by XCHG
Sorry for being such a noob :oops:, but this crashes:
%include "C:\nasmx\inc\nasmx.inc"
%include "C:\nasmx\inc\win32\windows.inc"
%include "C:\nasmx\inc\win32\user32.inc"
%include "C:\nasmx\inc\win32\kernel32.inc"
%include "C:\nasmx\inc\win32\msvcrt.inc"
global main

string db "%s", 0

main:
push ebp
mov ebp, esp
mov eax, dword ptr
mov ebx, dword ptr
invoke printf, string, eax
invoke printf, string, ebx
pop ebp
invoke ExitProcess, dword 0
Posted on 2008-02-07 10:56:24 by ASMFreak
Ok forget about the code it's shit :P
Could someone post a working example? I would be really greatfull.
Posted on 2008-02-07 12:33:03 by ASMFreak

Ok forget about the code it's s*** :P
Could someone post a working example? I would be really greatfull.


Um... yeah... not that easy.

The command line parameters are not put on the stack automatically. C does this for you. In ASM, you'll have to make the appropriate function calls to do the equivalent.
Posted on 2008-02-07 16:09:34 by SpooK

How do you get the parameters of the program from the stack?
Like in C:

int main(int argc, char *argv[])

I know it involves the esp register.
Could someone help me out?


call GetCommandLineA
eax = pointer to asciiz string of parameters for your program excluding the executable name. the otherways are useful to retrieve parameters for functions though. You were asking for the parameters for the program itself.

http://msdn2.microsoft.com/en-us/library/ms683156.aspx
Posted on 2008-02-07 18:55:55 by jakor
oh yeh, damn calling conventions, forgot about that...
also not mentioned is the 'fastcall' type...
where the params are passed via registers (and then stack, once you run out of registers)...
this is what x64 uses...
Posted on 2008-02-07 23:50:00 by evlncrn8

oh yeh, damn calling conventions, forgot about that...
also not mentioned is the 'fastcall' type...
where the params are passed via registers (and then stack, once you run out of registers)...
this is what x64 uses...


Also, for x64 FASTCALL, the parameters are passed left to right and it is the caller's responsibility to clean up the stack (like CDECL) not the callee (like STDCALL).

Fortunately, NASMX's invoke accounts for this already ;)
Posted on 2008-02-08 09:38:14 by SpooK