C/C++ calling convention

I

Ivan Vecerina

:A theoretical question: Sorry if its a beginner question.
Theoretical and beginner questions are ok in this NG,
but questions that are paltform specific are OT here.
A MS-specific or assembly/x86 newsgroup would be more
approriate for your post.

: Here is a quote from the MSDN explaning the C/C++ calling convention..
It
: demonstrates that the calling function is responsible to clean the stack
: pointer and it does it by the command "add esp,8" after returning
from
: the called function.
Note that even on a single platform (MSVC compiler on x86), various
calling conventions may exist (as is the case).
The one you refer to apears to be a classic C-calling convention,
which has the advantage of supporting variadic parameters
( the ... , such as for printf and scanf ).

: My questions:
: 1. Is the stack pointer common in a certain thread(or process)?
Each thread has its own stack pointer.

: 2. How does the called function get the parameters, is it by performing
: twice pop functions?
No. In this context it is accessed by using a stack-pointer relative
addressing. The caller will clean-up the stack.
Also, when calling a subroutine, the return address will
typically be pushed on the stack as well, and would
have to be popped first.

Again, other parameter-passing conventions exist.

: 3. If it pops twice why is there any need to clean the stack?
N/A.


hth -Ivan
 
S

SuperKoko

Geler said:
A theoretical question: Sorry if its a beginner question.

Here is a quote from the MSDN explaning the C/C++ calling convention.. It
demonstrates that the calling function is responsible to clean the stack
pointer and it does it by the command "add esp,8" after returning from
the called function.

My questions:
1. Is the stack pointer common in a certain thread(or process)?
There is exactly one stack for each thread (and since there can be
several threads for each process, there can be several stacks in a
process).
2. How does the called function get the parameters, is it by performing
twice pop functions?
No. A least not with the __cdecl calling convention

Usually, with the __cdecl calling conventions, parameters are accessed
with addressing relative to ESP (stack pointer register) or EBP.
For instance:

int add(int a,int b) {return a+b;}

Assembly code could be:
mov eax,[esp+4] ; note that [esp] contains the code return address
add eax,[esp+8]
ret

It may also use the "standard stack frame":
push ebp
mov ebp,esp
mov eax,[ebp+8]
add eax,[ebp+8]
pop ebp ; If there were space for automatic variables on the stack,
there would be a mov esp,ebp instruction before this one.
ret

add(5,3);
would generate:
push 3
push 5
call _add
add esp,8
3. If it pops twice why is there any need to clean the stack?
int _cdecl CFunc(int a, int b);
This type of method to get function arguments is bad, because it
requires to first pop the return address.
Furthermore, it would be a quite stupid idea with the __cdecl calling
convention.
Theorically, it could be used with the __stdcall calling convention:

_add:
pop edx
pop eax
pop ecx
add eax,ecx
jmp edx ; or "push edx" followed by "ret"
 
G

Geler

A theoretical question: Sorry if its a beginner question.

Here is a quote from the MSDN explaning the C/C++ calling convention.. It
demonstrates that the calling function is responsible to clean the stack
pointer and it does it by the command "add esp,8" after returning from
the called function.

My questions:
1. Is the stack pointer common in a certain thread(or process)?
2. How does the called function get the parameters, is it by performing
twice pop functions?
3. If it pops twice why is there any need to clean the stack?
int _cdecl CFunc(int a, int b);

calling function called function
-------------------------------------------

push b _CFunc PROC NEAR
push a .
call _CFunc .
add esp,8 .
. RET
. _CFunc ENDP
.

int _cdecl CVarFunc(int a, ...);

Thanks for any refernce to my questions.

Berger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top