Re: old skool vc++ 1.0

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 kept
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

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • [PATCH] trivial: fix typos concerning "address"
    ... struct io7 *io7; ... + * Hook the return address and push it in the stack of return address ... unsigned int flags; ...
    (Linux-Kernel)
  • Re: can any bdy help me
    ... i have developed a c-code to push integers into a stack. ... now when the inputs given are int type the program behaviour is as ... You don't check the return value of scanf() - a Bad Thing. ... The natural prototype for push() in your code is much more likely to ...
    (comp.lang.c)
  • Re: stack available
    ... 0003 push EBP ... 0004 mov EBP, ESP ... program stops always at a push (not enough stack?) in the "code ... part" (the first push in func1). ...
    (comp.lang.asm.x86)
  • Re: Passing parameters & return value on the stack
    ... I was wandering if there's any way push N parameters on the stack, ... I can access parameters by [EBP + xx], but I don't know how to ... mov, ebx ...
    (alt.lang.asm)
  • Re: stack
    ... This is a very simple implementation of push and pop functions of a ... int top; ... void push(struct stack *p,int n) ...
    (comp.lang.c)