473,903 Members | 6,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling conventions

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"
Jun 27 '08 #1
10 3278
su***********@g mail.com wrote:
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"
The C language leaves it up to each implementation to use whatever
calling conventions that suit them best. Almost always a C compiler
will default to the calling convention that the targeted host employs.
The most common calling convention is the so-called "C calling
convention" or "cdecl" used on the majority of C implementations .
Programs written to use the Windows API use Microsoft's "stdcall"
convention. Many other conventions like "fastcall" are possible.
Generally you should leave it to the compiler to chose the calling
convention.

<http://www.openwatcom. org/index.php/Calling_Convent ions>
<http://en.wikipedia.or g/wiki/Calling_convent ion>

Jun 27 '08 #2
In article <g2**********@r egistered.motza rella.org>,
santosh <sa*********@gm ail.comwrote:
>Programs written to use the Windows API use Microsoft's "stdcall"
convention.
That's probably almost always true, but it's more directly because
they are using the Windows ABI, not API.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #3
santosh wrote, On 04/06/08 17:35:
su***********@g mail.com wrote:
>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"

The C language leaves it up to each implementation to use whatever
calling conventions that suit them best. Almost always a C compiler
will default to the calling convention that the targeted host employs.
The most common calling convention is the so-called "C calling
convention" or "cdecl" used on the majority of C implementations .
I would not be so sure of that were I use. "cdecl" as I've heard the
term used has all parameters passed on the stack but on several
architectures it is normal to pass some parameters in registers (even
before you get to optimisations). I think the tendency is moving even
more towards parameters being passed in registers (MS have gone this ay
on their 64 bit ABI).
Programs written to use the Windows API use Microsoft's "stdcall"
convention.
Now, would that be 16, 32 and 64 bit Windows on X86/X64 and all the
various mobile versions of Windows?
Many other conventions like "fastcall" are possible.
Generally you should leave it to the compiler to chose the calling
convention.
Yes, leaving it as default for the implementation is almost always the
right thing to do and further more ignoring it is normally the right
thing to do.
<http://www.openwatcom. org/index.php/Calling_Convent ions>
<http://en.wikipedia.or g/wiki/Calling_convent ion>
Those links have plenty of information about other than what I would
consider to be cdecl.
--
Flash Gordon
Jun 27 '08 #4
santosh wrote:
Programs written to use the Windows API use Microsoft's "stdcall"
convention.
That was true under 32 bit windows. 64 bit windows uses "fastcall"
everywhere. It is a convention where 4 registers hold the
first 4 parameters of the function. With 64 bit wide registers
you can pass a long long in a register now.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #5
jacob navia wrote:
>
santosh wrote:
Programs written to use the Windows API use Microsoft's "stdcall"
convention.

That was true under 32 bit windows. 64 bit windows uses "fastcall"
everywhere. It is a convention where 4 registers hold the
first 4 parameters of the function. With 64 bit wide registers
you can pass a long long in a register now.
Just curious...

How does one take the address of one of the first 4 parameters? (I
suppose the compiler would have to do something such as store it on
the stack temporarily and then take that address?)

Note that I have seen implementations (long ago, I forget which
platform) which did exactly as you state -- the first N parameters to
non-varadic functions were passed in registers. (Yet another case of
"no prototype in scope for varadic function == UB".)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jun 27 '08 #6
jacob navia wrote:
santosh wrote:
>Programs written to use the Windows API use Microsoft's "stdcall"
convention.

That was true under 32 bit windows. 64 bit windows uses "fastcall"
everywhere. It is a convention where 4 registers hold the
first 4 parameters of the function. With 64 bit wide registers
you can pass a long long in a register now.
Thanks to both Flash Gordon and jacob navia for the corrections. I admit
unfamiliarity with 64 bit Windows.

Jun 27 '08 #7
In article <48************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.netwrote:
>How does one take the address of one of the first 4 parameters? (I
suppose the compiler would have to do something such as store it on
the stack temporarily and then take that address?)
Yes. As far as C is concerned, there are some expressions in the
calling function, and their values get assigned to some variables
in the called function. The procedure call protocol is just a way
of making that happen; it can be as simple or as complicated as
necessary. For another example, consider passing structs: this
may involve passing an address on the stack or in a register, with
a copy of the struct contents in the called function.
>Note that I have seen implementations (long ago, I forget which
platform) which did exactly as you state -- the first N parameters to
non-varadic functions were passed in registers.
This is common on architectures with a reasonable number of registers,
which more or less means everything except x86. Sparc and PPC are
examples.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #8
Kenneth Brody wrote:
jacob navia wrote:
>santosh wrote:
>>Programs written to use the Windows API use Microsoft's "stdcall"
convention.
That was true under 32 bit windows. 64 bit windows uses "fastcall"
everywhere. It is a convention where 4 registers hold the
first 4 parameters of the function. With 64 bit wide registers
you can pass a long long in a register now.

Just curious...

How does one take the address of one of the first 4 parameters? (I
suppose the compiler would have to do something such as store it on
the stack temporarily and then take that address?)
If you're careful to distinguish between "parameter" and
"argument" the answer should be clear. An "argument" is a
value provided by the caller, and a "parameter" or "formal
parameter" is a function-local variable initialized from an
argument value. If a calling convention requires the first N
argument values to be passed in registers, that doesn't mean
the first N parameters need to reside in registers throughout
their lifetimes. (Turn it around: If the argument values arrive
on a stack, does that mean the optimizer can't put the parameters
in registers?)
Note that I have seen implementations (long ago, I forget which
platform) which did exactly as you state -- the first N parameters to
non-varadic functions were passed in registers. (Yet another case of
"no prototype in scope for varadic function == UB".)
SPARC has done this since it was first designed. The
first N argument values are passed in registers, even for
variadic functions. (Consequence: The <stdarg.hmachin ery
is more involved than for memory-only calling conventions.)

Alpha uses registers to carry at least some arguments;
it's been a long time since I worked with Alpha machines and
I've forgotten the details.

It's my informal impression that most CPU designs less
than about two decades old pass at least some argument values
in registers; registers nowadays are a few decimal orders of
magnitude faster than memory (although that's not an entirely
fair comparison, what with store buffers and caches and all).
The CPUs that rely heavily on memory-resident arguments seem
to be those that have backward-compatibility issues with older
designs.

By the way, sometimes the "argument" value passed by the
calling convention isn't exactly the same as the "argument"
as seen in the C source. For example, some calling conventions
pass struct- and union-valued arguments by putting them in
unnamed memory-resident temporaries and passing pointers instead.
Some functions that return struct or union values are implemented
as if they were void functions with an extra, hidden argument
that points to a result location. What you see in the C may be
a little different than what you see in the debugger.

--
Er*********@sun .com
Jun 27 '08 #9
Kenneth Brody wrote:
jacob navia wrote:
>santosh wrote:
>>Programs written to use the Windows API use Microsoft's "stdcall"
convention.
That was true under 32 bit windows. 64 bit windows uses "fastcall"
everywhere. It is a convention where 4 registers hold the
first 4 parameters of the function. With 64 bit wide registers
you can pass a long long in a register now.

Just curious...

How does one take the address of one of the first 4 parameters? (I
suppose the compiler would have to do something such as store it on
the stack temporarily and then take that address?)
Exactly.

Before calling a function you should subtract 32 bytes from the stack
pointer. The called procedure can (if it wants) store in that space
the values it received in the registers. Lcc-win64 does that in
a debug setting so that the debugger can figure out where the values
of the parameters are...
Note that I have seen implementations (long ago, I forget which
platform) which did exactly as you state -- the first N parameters to
non-varadic functions were passed in registers. (Yet another case of
"no prototype in scope for varadic function == UB".)
Variadic functions are QUITE difficult in this setting, and I
had a LOT of bugs in the code generation. Basically, I store all
the registers that could possibly carry an argument (rcx, rdx,
r8,r9,xmm0,xmm1 ,xmm2,xmm3) into a continuous stack area and
maintain a pointer to the integer or pointer part, and another
pointer to the floating point part. Up to 4 floating point values
could be passed in xmm0-xmm3. Since the procedure doesn't know
at the start what values are being passed to it, it must save
all of them and va_arg() will pull the corresponding value
from either

o the integer or pointer stack, or
o from the floating point stack
o or from the actual stack where values are passed like
structures or long doubles.

This is relatively easy. MUCH more complex are the linux 64 conventions
(a nightmare).
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2973
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 advantage/disadvantage of either of
13
4122
by: RainBow | last post by:
Hi everyone, (Very Sorry, if this is the wrong group in which I am posting this query). Code snippet: //C library typedef int (*PFunc)(int* aArg); void call_c_foo(PFunc aPtrtoFunc) {
16
2314
by: aarklon | last post by:
Hi folks, recently i read the book named assembly language step by step by Jeff Duntemann. in the chapter coding for linux, he has got a paragraph named C calling conventions, which he describes as follows 1)
11
1975
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
4
1251
by: apm | last post by:
Can calling conventions of functions be changed in unmanaged C# in a similar way that they can be changed using C++?
2
3157
by: Geler | last post by:
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...
11
407
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, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
16
517
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
16
3795
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 warnings etc. I've done this in Debug and Release mode without any problems.
0
9999
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11284
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10986
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8049
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7208
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5894
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.