473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

C/C++ calling convention

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
2 3127
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jeremy R. Harner | last post by:
I'm trying to use a DLL, but when I try to run it from Visual Basic by going to Run > Start I get a 'Bad DLL Calling Convention' error when I try to access a function inside the DLL. If I compile...
1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
1
by: Leftie | last post by:
Folks, I'm trying to call an unmanaged function from VB.NET and keep getting "Object reference not set to an instance of an object" error. The code that i wrote can be found at:...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
11
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
16
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.