Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: x86 Stack Confusion

Reply
Thread Tools

Re: x86 Stack Confusion

 
 
Grumble
Guest
Posts: n/a
 
      01-09-2006
[ Hello comp.lang.c,

As far as I understand, C89 and C99 leave the details of parameter
passing up to the implementation. For that reason, I've claimed in
comp.lang.asm.x86 that there is no such thing as "the C calling
convention". Am I mistaken? ]

David J. Craig wrote:

> Grumble wrote:
>
>> Randy Hyde wrote:
>>
>>> Grumble wrote:
>>>
>>>> shellcode wrote:
>>>>
>>>>> [...] The stack function subtracts 4 from esp and saves ebp
>>>>> (to comply with the C calling convention).
>>>>
>>>> Just a nit:
>>>>
>>>> There is no "C calling convention".
>>>
>>> Actually, just to "nit" right back at you, there *is* a
>>> *convention*. What there isn't is a requirement.

>>
>> How can you claim there is such a thing as the "C calling
>> convention" when C compilers run on hundreds of different
>> platforms?
>>
>> What you (probably) have in mind is the so-called "Function Calling
>> Sequence" defined in the Intel386 Architecture Processor
>> Supplement of the System V Application Binary Interface (ABI)
>> document.
>>
>> http://www.caldera.com/developers/devspecs/abi386-4.pdf

>
> There is a 'C calling convention' in that right to left passing of
> arguments, how those arguments are passed based upon size
> adjustments, and that the caller is responsible to 'cleanup' the call
> after it returns. This right to left passing is required for
> functions that allow a variable number of arguments.


comp.lang.c readers: are any of these assertions correct? I don't see
any reference to "right to left passing of arguments" or cleanup or
"size adjustments" in the C89 draft.

http://rm-f.net/~orange/devel/specif...c89-draft.html

I think float parameters in variadic functions are promoted to double ??
but that is the only "size adjustment" I can think of.

--
Regards, Grumble
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      01-09-2006

"Grumble" <(E-Mail Removed)> wrote in message
news:43c24999$0$27306$(E-Mail Removed)...
>[ Hello comp.lang.c,
>
> As far as I understand, C89 and C99 leave the details of parameter
> passing up to the implementation. For that reason, I've claimed in
> comp.lang.asm.x86 that there is no such thing as "the C calling
> convention". Am I mistaken? ]
>
> David J. Craig wrote:
>
>> Grumble wrote:
>>
>>> Randy Hyde wrote:
>>>
>>>> Grumble wrote:
>>>>
>>>>> shellcode wrote:
>>>>>
>>>>>> [...] The stack function subtracts 4 from esp and saves ebp
>>>>>> (to comply with the C calling convention).
>>>>>
>>>>> Just a nit:
>>>>>
>>>>> There is no "C calling convention".
>>>>
>>>> Actually, just to "nit" right back at you, there *is* a
>>>> *convention*. What there isn't is a requirement.
>>>
>>> How can you claim there is such a thing as the "C calling
>>> convention" when C compilers run on hundreds of different
>>> platforms?
>>>
>>> What you (probably) have in mind is the so-called "Function Calling
>>> Sequence" defined in the Intel386 Architecture Processor
>>> Supplement of the System V Application Binary Interface (ABI)
>>> document.
>>>
>>> http://www.caldera.com/developers/devspecs/abi386-4.pdf

>>
>> There is a 'C calling convention' in that right to left passing of
>> arguments, how those arguments are passed based upon size
>> adjustments, and that the caller is responsible to 'cleanup' the call
>> after it returns. This right to left passing is required for
>> functions that allow a variable number of arguments.

>
> comp.lang.c readers: are any of these assertions correct? I don't see
> any reference to "right to left passing of arguments" or cleanup or
> "size adjustments" in the C89 draft.
>
> http://rm-f.net/~orange/devel/specif...c89-draft.html
>
> I think float parameters in variadic functions are promoted to double ??
> but that is the only "size adjustment" I can think of.


IMHO, as long as variadic functions are supported, and [thus] the semantics
of 7.15 (Variable arguments) are supported, it's up to the implementation:
there's certainly no need for a mandatory right-left [or whatever] and the
std says nothing whatsoever about the term 'stack'.


 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      01-09-2006

Grumble wrote:
> [ Hello comp.lang.c,
>
> As far as I understand, C89 and C99 leave the details of parameter
> passing up to the implementation. For that reason, I've claimed in
> comp.lang.asm.x86 that there is no such thing as "the C calling
> convention". Am I mistaken? ]
>
> David J. Craig wrote:
>
> > Grumble wrote:
> >
> >> Randy Hyde wrote:
> >>
> >>> Grumble wrote:
> >>>
> >>>> shellcode wrote:
> >>>>
> >>>>> [...] The stack function subtracts 4 from esp and saves ebp
> >>>>> (to comply with the C calling convention).
> >>>>
> >>>> Just a nit:
> >>>>
> >>>> There is no "C calling convention".
> >>>
> >>> Actually, just to "nit" right back at you, there *is* a
> >>> *convention*. What there isn't is a requirement.
> >>
> >> How can you claim there is such a thing as the "C calling
> >> convention" when C compilers run on hundreds of different
> >> platforms?
> >>
> >> What you (probably) have in mind is the so-called "Function Calling
> >> Sequence" defined in the Intel386 Architecture Processor
> >> Supplement of the System V Application Binary Interface (ABI)
> >> document.
> >>
> >> http://www.caldera.com/developers/devspecs/abi386-4.pdf

> >
> > There is a 'C calling convention' in that right to left passing of
> > arguments, how those arguments are passed based upon size
> > adjustments, and that the caller is responsible to 'cleanup' the call
> > after it returns. This right to left passing is required for
> > functions that allow a variable number of arguments.

>
> comp.lang.c readers: are any of these assertions correct? I don't see
> any reference to "right to left passing of arguments" or cleanup or
> "size adjustments" in the C89 draft.
>
> http://rm-f.net/~orange/devel/specif...c89-draft.html
>
> I think float parameters in variadic functions are promoted to double ??
> but that is the only "size adjustment" I can think of.


"C calling convention" is a term used to refer to the
implementation-specific details of how function calls are performed,
usually defined as part of a platform-specific ABI. This convention
differs from platform to platform but often consists of the following
details:

* Arguments passed on a stack from right to left
* The function performing the call prepares the arguments on the stack
for the called function and removes them from the stack when the
function returns
* Registers that are expected to be used for specific purposes and
which registers are expected to be preserved and overwritten during a
function call.

So, as far as Standard C is concerned, there is no "C calling
convention", any such convention would be part of the implementation
defined details not specified by the Standard. The calling conventions
themselves are based on the architecture and the implementation on that
architechture and are just that, conventions. The x86 platform has a
set of conventions that almost all implementations follow, for example
that the EAX, ECX, and EDX registers are not expected to be preserved
through a function call. Although these conventions must be followed
to conform to Intel's x86 ABI, it is entirely possible to create a
Standard-conforming implementation without following them.

Robert Gamble

 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      01-09-2006
Grumble wrote:
> [ Hello comp.lang.c,
>
> As far as I understand, C89 and C99 leave the details of parameter
> passing up to the implementation. For that reason, I've claimed in
> comp.lang.asm.x86 that there is no such thing as "the C calling
> convention". Am I mistaken? ]


There is no "C calling convention" imposed by the C standard --
instead, on the x86 platform the convention exists that is adhered to
by a number of C compilers (and other platforms have a similar scheme
-- its also called "the ABI".) It provides a way of mapping call
parameters in C to the assembly registers and stack in a standard way.
This is how it is possible for Fortran and C libraries to call each
other, even when you vary compilers on each side. As an example, the
typical Pascal compilers for x86 from yester year used another
convention called "Pascal calling convention" which, in fact, older
Microsoft C compilers also supported as an option.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-09-2006
Grumble wrote:
>
> As far as I understand, C89 and C99 leave the details of
> parameter passing up to the implementation. For that reason,
> I've claimed in comp.lang.asm.x86 that there is no such thing as
> "the C calling convention". Am I mistaken? ]


No. However, C is almost unique among languages in having the
error prone variadic functions available, which in turn mandate
having the first parameter in a known place, and makes the reverse
order (assuming a stack, which is not necessary) attractive.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> Grumble wrote:
>
>>[ Hello comp.lang.c,
>>
>>As far as I understand, C89 and C99 leave the details of parameter
>>passing up to the implementation. For that reason, I've claimed in
>>comp.lang.asm.x86 that there is no such thing as "the C calling
>>convention". Am I mistaken? ]

>
>
> There is no "C calling convention" imposed by the C standard --
> instead, on the x86 platform the convention exists that is adhered to
> by a number of C compilers (and other platforms have a similar scheme
> -- its also called "the ABI".) It provides a way of mapping call
> parameters in C to the assembly registers and stack in a standard way.
> This is how it is possible for Fortran and C libraries to call each
> other, even when you vary compilers on each side. As an example, the
> typical Pascal compilers for x86 from yester year used another
> convention called "Pascal calling convention" which, in fact, older
> Microsoft C compilers also supported as an option.
>


There is no C calling convention. The "C" calling convention is defined
by the compiler that compiles the operating system, usually written
in C.

Under the linux OS, the gcc compiler defines the calling convention
rules, and other compilers must obey whatever gcc decides is the
calling convention. If not, they would be faced with the
herculean task of rewriting the whole OS.

Under the windows OS, the compiler that compiled the OS is MSVC,
and other compilers must follow the conventions of that compiler.

In newer systems, like the new x64 systems, there is no "right to left",
nor "left to right" since four or more arguments are passed in
the register file.

For instance, you can pass up to 4 arguments to a function directly
in registers under windows 64 bits. Linux 64 bits is even more ambitious
and much more parameters are passed in registers using both the
integer and the floating point register file. (for x86 systems)

In general, in each system there is something like an "ABI" (Application
Binary Interface) that defines what the conventions of the OS are. Since
C is a language that interfaces directly to the OS, calling conventions
are highly OS specific.

Most compilers give the user the possibility of choosing the calling
convention for either all the functions or for specific functions
using markers in the declaration header of each function. For instance:

int _stdcall fn(int a,int b) { /* ... */ }

In most compilers the "cdecl" marker is the default, i.e. the ABI
defined calling convention. Note that the standard never defines
"cdecl".

jacob
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      01-09-2006
"Chuck F. " wrote:

> Grumble wrote:
>>
>> As far as I understand, C89 and C99 leave the details of
>> parameter passing up to the implementation. For that reason,
>> I've claimed in comp.lang.asm.x86 that there is no such thing as
>> "the C calling convention". Am I mistaken? ]

>
> No. However, C is almost unique among languages in having the error prone
> variadic functions available, which in turn mandate having the first
> parameter in a known place, and makes the reverse order (assuming a stack,
> which is not necessary) attractive.


When a knowledgeable person says "C calling convention", *this* is what he
means. There is nothing analogous to printf() with it's indeterminate
number of parameters in, Pascal, for example.


 
Reply With Quote
 
=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=
Guest
Posts: n/a
 
      01-09-2006
"osmium" <(E-Mail Removed)> writes:
> When a knowledgeable person says "C calling convention", *this* is
> what he means. There is nothing analogous to printf() with it's
> indeterminate number of parameters in, Pascal, for example.


Yes, there is. Write() and WriteLn() both take a variable number of
arguments. There is however no way for the programmer to define a
procedure which takes a variable number of arguments.

DES
--
Dag-Erling Smørgrav - (E-Mail Removed)
 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      01-09-2006
Robert Gamble wrote:

> Grumble wrote:
>
>>[ Hello comp.lang.c,
>>
>>As far as I understand, C89 and C99 leave the details of parameter
>>passing up to the implementation. For that reason, I've claimed in
>>comp.lang.asm.x86 that there is no such thing as "the C calling
>>convention". Am I mistaken? ]
>>
>>David J. Craig wrote:
>>
>>
>>>Grumble wrote:
>>>
>>>
>>>>Randy Hyde wrote:
>>>>
>>>>
>>>>>Grumble wrote:
>>>>>
>>>>>
>>>>>>shellcode wrote:
>>>>>>
>>>>>>
>>>>>>>[...] The stack function subtracts 4 from esp and saves ebp
>>>>>>>(to comply with the C calling convention).
>>>>>>
>>>>>>Just a nit:
>>>>>>
>>>>>>There is no "C calling convention".
>>>>>
>>>>>Actually, just to "nit" right back at you, there *is* a
>>>>>*convention*. What there isn't is a requirement.
>>>>
>>>>How can you claim there is such a thing as the "C calling
>>>>convention" when C compilers run on hundreds of different
>>>>platforms?
>>>>
>>>>What you (probably) have in mind is the so-called "Function Calling
>>>>Sequence" defined in the Intel386 Architecture Processor
>>>>Supplement of the System V Application Binary Interface (ABI)
>>>>document.
>>>>
>>>>http://www.caldera.com/developers/devspecs/abi386-4.pdf
>>>
>>>There is a 'C calling convention' in that right to left passing of
>>>arguments, how those arguments are passed based upon size
>>>adjustments, and that the caller is responsible to 'cleanup' the call
>>>after it returns. This right to left passing is required for
>>>functions that allow a variable number of arguments.

>>
>>comp.lang.c readers: are any of these assertions correct? I don't see
>>any reference to "right to left passing of arguments" or cleanup or
>>"size adjustments" in the C89 draft.
>>
>>http://rm-f.net/~orange/devel/specif...c89-draft.html
>>
>>I think float parameters in variadic functions are promoted to double ??
>>but that is the only "size adjustment" I can think of.

>
>
> "C calling convention" is a term used to refer to the
> implementation-specific details of how function calls are performed,
> usually defined as part of a platform-specific ABI. This convention
> differs from platform to platform but often consists of the following
> details:
>
> * Arguments passed on a stack from right to left
> * The function performing the call prepares the arguments on the stack
> for the called function and removes them from the stack when the
> function returns
> * Registers that are expected to be used for specific purposes and
> which registers are expected to be preserved and overwritten during a
> function call.


The calling convention on IA-64 is rather different:
http://blogs.msdn.com/oldnewthing/ar.../13/58199.aspx
http://www.intel.com/design/itanium/...ads/245358.htm

--
Regards, Grumble
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-09-2006
jacob navia <(E-Mail Removed)> writes:
[snip]
> There is no C calling convention. The "C" calling convention is defined
> by the compiler that compiles the operating system, usually written
> in C.
>
> Under the linux OS, the gcc compiler defines the calling convention
> rules, and other compilers must obey whatever gcc decides is the
> calling convention. If not, they would be faced with the
> herculean task of rewriting the whole OS.
>
> Under the windows OS, the compiler that compiled the OS is MSVC,
> and other compilers must follow the conventions of that compiler.
>
> In newer systems, like the new x64 systems, there is no "right to left",
> nor "left to right" since four or more arguments are passed in
> the register file.
>
> For instance, you can pass up to 4 arguments to a function directly
> in registers under windows 64 bits. Linux 64 bits is even more ambitious
> and much more parameters are passed in registers using both the
> integer and the floating point register file. (for x86 systems)
>
> In general, in each system there is something like an "ABI" (Application
> Binary Interface) that defines what the conventions of the OS are. Since
> C is a language that interfaces directly to the OS, calling conventions
> are highly OS specific.
>
> Most compilers give the user the possibility of choosing the calling
> convention for either all the functions or for specific functions
> using markers in the declaration header of each function. For instance:
>
> int _stdcall fn(int a,int b) { /* ... */ }
>
> In most compilers the "cdecl" marker is the default, i.e. the ABI
> defined calling convention. Note that the standard never defines
> "cdecl".


All of this is x86-specific.

I have no idea whether "most compilers" have things called "_stdcall"
or "cdecl", but I've never used either.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
x64 vs x86.. surprising results in performance (x86 better)? markm75 Windows 64bit 7 01-09-2008 06:41 PM
Why is there an x86 emu if a processor is x86-64? =?Utf-8?B?RWxsaW90IEh1ZGdpbnM=?= Windows 64bit 4 07-23-2006 11:52 PM
x86 Mac Laptop and x86 iMac now available Daniel NZ Computing 11 01-17-2006 12:11 PM
Is there a way with Linux x86 to report a way the current stack trace for a thread? kevin.hall@motioneng.com C++ 4 10-20-2005 09:43 PM



Advertisments