473,573 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3140
"Geler" <ge***@Ilans.co .il> wrote in message
news:e6******** **@news2.netvis ion.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
7151
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 the program to an executable and run it everything works fine....that is I don't get the 'Bad DLL Calling Convention' and the function works as it's...
1
2308
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 static and non-static class member function? 4. Could it be possible to specify different calling convention for each "global" or each class member...
8
2949
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 p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the...
1
3040
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: http://www.nomorepasting.com/paste.php?pasteID=5973 The dll (if some kind soul has the time to compile and run the code) can be found at:
18
4333
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters,...
11
3182
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 Esa_Init. When I make a call, VS2005 reports "AccessViolationException" and refers to the 4th parameter (EsaT_State_Handle). I don't know how to...
10
3242
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 programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
16
3770
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 compiled a DLL of the Python source code using the pythoncode VC++ project in the PCbuild folder of the source download and this works 100% without any...
6
11563
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 another convention unless they have an ellipsis argument.) I can force GCC and other compilers to use such a convention by declaring all methods with...
0
7771
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8009
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8198
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5289
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1296
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.