Re: calling convention stdcalll and cdecl call
* Liviu:
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:-b6dnbW47NIf3hzVnZ2dnUVZ_sninZ2d@posted.comnet...
* Liviu:
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:FrydnQFAxMIkhhzVnZ2dnUVZ_hadnZ2d@posted.comnet...
* Liviu:
"Alf P. Steinbach" <alfps@start.no> wrote
It had to do with a minor point that Igor may or may not have been
trying to make, who knows since evidently he doesn't mean what he
writes.
I'll go on a limb here ;-) but I trust Igor meant exactly what he
wrote:
| With stdcall, the function is responsible for removing its
| parameters from the stack. To do this, it must know how many
| parameters there are, and thus cannot take variable number of
| parameters.
I requoted his claim above. What part exactly do you find
incorrect?
"and thus cannot take variable number of parameters" is incorrect.
Please explain how a function which "must know how many
parameters there are" does, in fact, know that when called with a
variable number thereof.
An example would suffice...
You have already C++
You don't decide who's calling you, unless you target a very narrow,
closed, self contained world.
and assembly language example, from earlier in this thread.
I pasted it under "_printf:" below and it failed miserably ;-)
You were unable to ride a bicycle and blame the bicycle. It doesn't work. Well,
that's not the only possible conclusion, you know.
If you have one which actually works in the quoted context, then by all
means please surprise all the rest of us and post it.
; this is the caller code ........................................
; calling a variadic _stdcall function with 1 argument
push eax
call _printf
; calling the same variadic _stdcall function with 2 arguments
push eax
push eax
call _printf
; ...etc
; please fill in this part ........................................
_printf:
; --- your code here ---
; --- doesn't have to do anything except _not_ corrupt the stack ---
Please note that once you impose additional requirements on the
caller
code (pass number of params, set some sort of stack frame, etc) then
that's no longer the __stdcall convention.
Also, you must appreciate that __stdcall is just the C name for a
well
defined binary contract between caller and callee. It is not a matter
of
compiler, and there doesn't even have to be one involved. I could
technically write assembler, or direct machine bytecodes in debug.exe
if
I so wished, and the same calling convention would still apply.
You asked this earlier and had it explained to you, in message
<mOednStCze1YgxzVnZ2dnUVZ_jSdnZ2d@posted.comnet>. Quoting from that
message:
[Quoted from earlier message:]
Imagine for a moment that you'd be writing the __stdcall
printf(const
char *, ...) code. At the point your code would be entered,
everything
would look _exactly_ the same when the function is called in either
of
these ways:
printf("%d", 1);
printf("%d", 1, 2);
How would you code your printf so that it pops the right number of
bytes off the stack in each case?
No problem. The number of bytes is passed in any way the compiler
deems suitable
Let me see if I get this right... Your example is then proposing a
calling convention which current compilers don't support,
No, it is not.
It demonstrated that an existing calling convention could support varargs,
actually that it supports it better than the conventional one for that, and that
tool support for that was lacking.
Not that we need that tool support, as far as I can see.
but could
conceivably be made to work with cooperation from next generation
compilers when issuing function calls between themselves. OK, fine, no
argument there. But then (a) why refer to it as __stdcall, which is a
different, established calling convention,
It is not.
Do you know what __stdcall is?
It's a combination of a few things.
At the machine code level, that arguments are pushed right to left, and that the
callee adjusts the stack pointer back when returning. This my example did.
At a higher level, that a certain name mangling, defined by the specific
compiler (it varies between compilers), is used. Note that at this level the
meaning of stdcall varies between compilers. My example did employ this name
mangling, but by doing so also indicated that that particular compiler's name
mangling scheme failed to accomodate variable number of arguments -- at least
as far as I could see, and I'm not going to investigate that further.
At no point did my example violate the contract of stdcall, which was evidenced
by Visual C++ being able to call the function correctly.
and (b) what bearing has it
at all on Igor's statement you pretend to dispute.
I'm now wondering whether I'm actually talking to Igor under an alias: two just
about equally thickheaded persons with the same blind zone.
But anyway, Igor maintained that stdcall could not accomodate variable number of
arguments.
That was incorrect.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?