Re: old skool vc++ 1.0
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 30 Jun 2007 22:18:02 -0400
PASCAL == _stdcall. The word "PASCAL" is now a noise word representing obsolete code and
should be removed from all files and replaced by an appropriate word like WINAPI or
CALLBACK, both of which are __stdcall.
Here's the deal:
In a __cdecl linkage (the normal default), arguments are pushed on the stack in
right-to-left order, a CALL is executed, and upon return from the CALL, the stack pointer
is incremented by the number of bytes which were pushed (stacks grow from high memory to
low memory, so "push" decrements the stack pointer and "pop" increments it:
int f(int a, int b, int c) { return a+b+c; }
f(1,2,3)
push 3
push 2
push 1
call f
add esp, 0x0C // = 12, three 4-byte ints
f: push ebp
mov ebp, esp
the stack is now
EBP + 16: 3 // c
EBP + 12: 2 // b
EBP + 8 1 // a
EBP + 4 return address
EBP: old EBP
the code is therefore
mov eax, DWORD PTR[EBP+8]
add eax, DWORD PTR[EBP+12]
add eax, DWORD PTR[EBP+16]
finally, we return
mov esp, ebp
pop ebp
ret
In a __stdcall, a platform-specific faster call mechanism is used. In this case, the
nmber of parameters must be fixed at compile time, and the parameters are cleaned up the
callee:
int __stdcall f(int a, int b, int c);
push 3
push 2
push 1
call f
....next line of code
Note there is no code to increment the stack pointer to remove the parameters
f: push ebp
mov ebp, esp
...add instructions as above
mov esp, ebp
pop ebp
ret 0x0C
Here, the ret instruction cleans off the parameters. Note that it takes 1 CPU clock cycle
(350picoseconds on a 2.8GHz machine) to both pop the return address and strip the
parameters.
joe
On Mon, 25 Jun 2007 05:17:01 -0700, rodchar <rodchar@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
thanks everyone for some direction. regarding my original post as i keptJoseph M. Newcomer [MVP]
trying to trace back to gain some understanding i kept reading something that
would lose me, usually the first few sentences of an article. I think I have
enough information to go on for now.
rod.
"Doug Harrison [MVP]" wrote:
On Sun, 24 Jun 2007 22:03:00 -0700, rodchar
<rodchar@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
how far will i have to go back in order for this to make sense? what's the
best way to relate this stuff to what i think i understand and have some work
experience in c#.net?
Your original question was about the PASCAL macro. Calling conventions for
native code have no relevance in .NET, unless perhaps you're trying to
write P/Invoke declarations. If you want to understand it anyway, you need
to have a basic understanding of how functions are called at the assembly
level, including how arguments are passed, which is typically through
registers and/or on the stack, return addresses are stored, stack frames
are specified, etc. Since you've subsequently asked what a "register" is,
I'd say you need to start with Assembly Language 101.
--
Doug Harrison
Visual C++ MVP
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Prev by Date: Re: HtmlHelp Errors in VS 2005
- Next by Date: Re: Programatically creating text box
- Previous by thread: Re: HtmlHelp Errors in VS 2005
- Next by thread: Re: Programatically creating text box
- Index(es):
Relevant Pages
|