By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

C/C++ calling convention

P: n/a
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
Jun 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Geler" <ge***@Ilans.co.il> wrote in message
news:e6**********@news2.netvision.net.il...
: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

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jun 7 '06 #2

P: n/a

Geler wrote:
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"

Jun 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.