<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Gourav Das's Blogs - Beginners paradise</title><link>http://blogs.msdn.com/gouravdas/default.aspx</link><description>School of thought</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Objects and Properties - The C# 3.0ish way!!</title><link>http://blogs.msdn.com/gouravdas/archive/2009/03/18/objects-and-properties-the-c-3-0ish-way.aspx</link><pubDate>Wed, 18 Mar 2009 13:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9486490</guid><dc:creator>gouravdas</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/gouravdas/comments/9486490.aspx</comments><wfw:commentRss>http://blogs.msdn.com/gouravdas/commentrss.aspx?PostID=9486490</wfw:commentRss><description>These days I have been bitten by the C# bug. As most would agree, the effects are "addictive". Migrating from one to Language to another has always been like shifting homes, which you wouldn't do unless forced to. But the .NET/C# team is ensuring that...(&lt;a href="http://blogs.msdn.com/gouravdas/archive/2009/03/18/objects-and-properties-the-c-3-0ish-way.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9486490" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Auto+Property/default.aspx">Auto Property</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Immutable/default.aspx">Immutable</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Mutable/default.aspx">Mutable</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Object+Initilization/default.aspx">Object Initilization</category></item><item><title>Slang - 'C' - Lang (for beginners)</title><link>http://blogs.msdn.com/gouravdas/archive/2008/05/12/slang-c-lang.aspx</link><pubDate>Sun, 11 May 2008 22:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8488555</guid><dc:creator>gouravdas</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/gouravdas/comments/8488555.aspx</comments><wfw:commentRss>http://blogs.msdn.com/gouravdas/commentrss.aspx?PostID=8488555</wfw:commentRss><description>Hello Guys, Sorry again if the title is misleading, but how often pointers have been more like a slang to a beginner. I can bet there are more nods right now!! I am middle of studying deeper into Templates, but I just found enough time to talk about Pointers...(&lt;a href="http://blogs.msdn.com/gouravdas/archive/2008/05/12/slang-c-lang.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8488555" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/gouravdas/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Pointers/default.aspx">Pointers</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Constants/default.aspx">Constants</category></item><item><title>Function Calls and the conventions - Steps towards effective debugging</title><link>http://blogs.msdn.com/gouravdas/archive/2008/05/02/function-calls-and-the-conventions-step-towards-effective-debugging.aspx</link><pubDate>Thu, 01 May 2008 21:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8447422</guid><dc:creator>gouravdas</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/gouravdas/comments/8447422.aspx</comments><wfw:commentRss>http://blogs.msdn.com/gouravdas/commentrss.aspx?PostID=8447422</wfw:commentRss><description>&lt;P&gt;Hello Guys,&lt;/P&gt;
&lt;P&gt;How often has it&amp;nbsp;happened, that we do things without really knowing why we are doing so. I can hear you speaking to yourself... Its not a fault (or a "trap" :P ). Many a times we end up doing that, since most often, the work needs to be completed, period. Learning a certain thing (a big thing), is often like solving puzzles. Its only when you have put all the pieces together, you know the importance of a certain piece. &lt;/P&gt;
&lt;P&gt;Debugging is one such puzzle. Function calling conventions - one of those pieces. Today I would talk about function calling conventions that are available to us. Its those basics again(for veterans),&amp;nbsp;nothing new or exiting...but come on, these things never hurt. This time I am covering only Function calling conventions.&lt;/P&gt;
&lt;P&gt;N.B - The examples (if any) will reference C/C++ (C# at most). Generally (and I mean GENERALLY)&amp;nbsp;when we use functions we do not use any &lt;EM&gt;"specifier" &lt;/EM&gt;explicitly&lt;EM&gt; &lt;/EM&gt;for calling conventions.&lt;/P&gt;
&lt;P&gt;Let's first discuss&amp;nbsp;about&amp;nbsp;the&amp;nbsp;Function Calling Conventions available. We will talk about some that&amp;nbsp;are still alive (__pascal, __fortran,__syscall -&amp;gt; R.I.P).&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;__cdecl&lt;/LI&gt;
&lt;LI&gt;__stdcall&lt;/LI&gt;
&lt;LI&gt;__fastcall&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;__thiscall&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Calling conventions mainly..&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;decide the way/order things are pushed to the Stack&lt;/LI&gt;
&lt;LI&gt;who is going to clean the mess (clear the stack I meant)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The first point is important because, the parameters are accessed by EBP's, in the manner of [EBP + 8], [EBP + 12], etc..(we will talk about stacks later someday, another point to remember here is EBP is used for stack walking when optimization like FPO is not used, else ESP is used directly.)&lt;/P&gt;
&lt;P&gt;The second point is also very important because in Static functions (static in terms of number of parameters being constant), the called function (Callee) has complete idea about the number of parameters being passed to it on a particular call, because that is stagnant. Once it has the complete idea, it can do the cleaning stuff. &lt;/P&gt;
&lt;P&gt;But in case of dynamic functions (wondering about dynamic functions...hmm...printf(), If you want to write your own, you can use &amp;lt;stdarg.h&amp;gt;/&amp;lt;cstdarg&amp;gt; .)only the calling function knows about the number of parameters that is getting passed. So the onus is on the Calling function. We could have had made the called functions do the cleaning, but we have to pass much more information(infact store them) so that the Callee can use them to cleanup. Which is an obvious overhead that is not economic.&lt;/P&gt;
&lt;P&gt;Calling conventions, where the Calling functions do the cleanup, are usually less efficient than the function calls where the Callee function do the cleanup. The executables are also larger in size. Remember that, if we had to pass and store the extra information on Dynamic functions so that the Callee can cleanup, the executable would have been even larger in size.&lt;/P&gt;
&lt;P&gt;Function calling convention also decide the way function names are decorated(mangled). I won't talk here much&amp;nbsp;about name mangling because thats another topic. I could have shown some examples but in VS2008, nor in Windbg the proper mangled names are displayed. Just to remember that Name Mangling (Name Decoration as also known) is also dependent on the Calling convention. Those are quite simple in C but quite tricky in C++ (refer to &lt;A href="http://en.wikipedia.org/wiki/Name_mangling" mce_href="http://en.wikipedia.org/wiki/Name_mangling"&gt;http://en.wikipedia.org/wiki/Name_mangling&lt;/A&gt;&amp;nbsp;for the Name Mangling).&lt;/P&gt;
&lt;P&gt;Now lets explore each convention mentioned above.&lt;/P&gt;
&lt;P&gt;__cdecl&lt;BR&gt;=-=-=-=-&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Parameters are passed from right to left ( int foo (int _a, int _b, int _c), here they are pushed&amp;nbsp;to stack in the following order _c -&amp;gt; _b -&amp;gt;&amp;nbsp;_a&amp;nbsp;)&lt;/LI&gt;
&lt;LI&gt;The calling function has to do the cleanup.&lt;/LI&gt;
&lt;LI&gt;Its default calling convention for C/C++ (In case of C++ its default if we are not calling Member Functions or Member Functions have dynamic number of parameters)&lt;/LI&gt;
&lt;LI&gt;Larger executables.&lt;/LI&gt;
&lt;LI&gt;From compiler, the option is to compile using /Gd&lt;/LI&gt;&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;lt;code&amp;gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;__cdecl&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; pf(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; a,&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; b)&lt;BR&gt;{&lt;BR&gt;}&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; main()&lt;BR&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; a=5,b;&lt;BR&gt;&amp;nbsp;b=a+1;&lt;BR&gt;&amp;nbsp;pf(b,a);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 0;&lt;BR&gt;}&lt;BR&gt;&lt;/FONT&gt;&amp;lt;\code&amp;gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;For this convention the mangled name would be &lt;A href="mailto:?pf@@YAXHH@Z" mce_href="mailto:?foo@@YAXHHH@Z"&gt;?pf@@YAXHH@Z&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;If you have VS installed, search for undname.exe (utility for undecorating names), then&amp;nbsp;you can do the following..&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;D:\VS2008\VC\bin&amp;gt;undname &lt;A href="mailto:?pf@@YAXHH@Z" mce_href="mailto:?foo@@YAXHHH@Z"&gt;?pf@@YAXHH@Z&lt;/A&gt;&lt;BR&gt;Microsoft (R) C++ Name Undecorator&lt;BR&gt;Copyright (C) Microsoft Corporation. All rights reserved.&lt;/P&gt;
&lt;P&gt;Undecoration of :- "&lt;A href="mailto:?pf@@YAXHH@Z" mce_href="mailto:?foo@@YAXHHH@Z"&gt;?pf@@YAXHH@Z&lt;/A&gt;"&lt;BR&gt;is :- "void __cdecl pf(int,int)"&lt;/P&gt;
&lt;P&gt;If you view the diassembly, you will see that RET (which is the return command) is without any operands/parameters. This is because in this calling convention the callee dosent cleanup the stack. Look at the last opcode&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&amp;lt;DISASSEMBLY&amp;gt;&lt;BR&gt;&lt;FONT size=1&gt;&lt;FONT size=1&gt;void __cdecl pf(int a,int b)&lt;BR&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=1&gt;&lt;FONT color=#808080 size=1&gt;001113A0 push ebp &lt;BR&gt;001113A1 mov ebp,esp &lt;BR&gt;001113A3 sub esp,0C0h &lt;BR&gt;001113A9 push ebx &lt;BR&gt;001113AA push esi &lt;BR&gt;001113AB push edi &lt;BR&gt;001113AC lea edi,[ebp-0C0h] &lt;BR&gt;001113B2 mov ecx,30h &lt;BR&gt;001113B7 mov eax,0CCCCCCCCh &lt;BR&gt;001113BC rep stos dword ptr es:[edi] &lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;}&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=1&gt;&lt;FONT color=#808080 size=1&gt;001113BE pop edi &lt;BR&gt;001113BF pop esi &lt;BR&gt;001113C0 pop ebx &lt;BR&gt;001113C1 mov esp,ebp &lt;BR&gt;001113C3 pop ebp &lt;BR&gt;001113C4 ret &lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;lt;DISASSEMBLY&amp;gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;__stdcall&lt;BR&gt;=-=-=-=-=&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Windows programmers are so much used to this. This is used everywhere in Windows Programming. Actually the name used there is WINAPI(you can say&amp;nbsp;it as synonym for __stdcall).&lt;/LI&gt;
&lt;LI&gt;Parameters pushed into stack from right to left. The &lt;EM&gt;this&lt;/EM&gt; parameter of objects are passed to stack at last.&lt;/LI&gt;
&lt;LI&gt;Stack cleared up by Callee function.&lt;/LI&gt;
&lt;LI&gt;From compiler, the option is to compile using /Gz&lt;/LI&gt;&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;lt;code&amp;gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;__stdcall&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; pf(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; a,&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; b)&lt;BR&gt;{&lt;BR&gt;}&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; main()&lt;BR&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; a=5,b;&lt;BR&gt;&amp;nbsp;b=a+1;&lt;BR&gt;&amp;nbsp;pf(b,a);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 0;&lt;BR&gt;}&lt;BR&gt;&lt;/FONT&gt;&amp;lt;\code&amp;gt;&lt;/P&gt;
&lt;P&gt;For this convention the mangled name would be &lt;A href="mailto:?pf@@YGXHH@Z" mce_href="mailto:?foo@@YGXHHH@Z"&gt;?pf@@YGXHH@Z&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;D:\VS2008\VC\bin&amp;gt;undname &lt;A href="mailto:?pf@@YGXHH@Z" mce_href="mailto:?foo@@YGXHHH@Z"&gt;?pf@@YGXHH@Z&lt;/A&gt;&lt;BR&gt;Microsoft (R) C++ Name Undecorator&lt;BR&gt;Copyright (C) Microsoft Corporation. All rights reserved.&lt;/P&gt;
&lt;P&gt;Undecoration of :- "&lt;A href="mailto:?pf@@YGXHH@Z" mce_href="mailto:?foo@@YGXHHH@Z"&gt;?pf@@YGXHH@Z&lt;/A&gt;"&lt;BR&gt;is :- "void __stdcall pf(int,int)"&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So since in case of __stdcall the stack is cleared up by the callee, you would see in the disassembly that the RET is followed by the size of all the parameters. In this case, it would be 4 bytes of int a + 4bytes of int b. If there was a double c, you would see RET 10h (remember this 10 is in hex so its decimal equivalent would be 12)&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;lt;DISASSEMBLY&amp;gt;&lt;BR&gt;&lt;FONT size=1&gt;&lt;FONT size=1&gt;&lt;FONT size=1&gt;void __stdcall pf(int a,int b)&lt;BR&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=1&gt;&lt;FONT color=#808080 size=1&gt;003C13A0 push ebp &lt;BR&gt;003C13A1 mov ebp,esp &lt;BR&gt;003C13A3 sub esp,0C0h &lt;BR&gt;003C13A9 push ebx &lt;BR&gt;003C13AA push esi &lt;BR&gt;003C13AB push edi &lt;BR&gt;003C13AC lea edi,[ebp-0C0h] &lt;BR&gt;003C13B2 mov ecx,30h &lt;BR&gt;003C13B7 mov eax,0CCCCCCCCh &lt;BR&gt;003C13BC rep stos dword ptr es:[edi] &lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;}&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=1&gt;&lt;FONT color=#808080 size=1&gt;003C13BE pop edi &lt;BR&gt;003C13BF pop esi &lt;BR&gt;003C13C0 pop ebx &lt;BR&gt;003C13C1 mov esp,ebp &lt;BR&gt;003C13C3 pop ebp &lt;BR&gt;003C13C4 ret 8 &lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;lt;DISASSEMBLY&amp;gt;&lt;BR&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;__fastcall&lt;BR&gt;=-=-=-=-=&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The main essense is that , the first two parameters if are equal to or less than 32 bits (like int) are pushed to the ECX and EDX registers. Rest are pushed to stack. Since operations are registers are faster, hence the name __fastcall.&lt;/LI&gt;
&lt;LI&gt;Parameters which are not pushed to Registers are pushed from Right to left.&lt;/LI&gt;
&lt;LI&gt;The Called function clears the stack, which obviously means that __fastcall becomes incompatible with function with dynamic arguments.&lt;/LI&gt;
&lt;LI&gt;From compiler, the option is to compile using /Gr&lt;/LI&gt;&lt;/UL&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;lt;code&amp;gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;__fastcall&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; pf(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; a,&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; b)&lt;BR&gt;{&lt;BR&gt;}&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; main()&lt;BR&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; a=5,b;&lt;BR&gt;&amp;nbsp;b=a+1;&lt;BR&gt;&amp;nbsp;pf(b,a);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt; 0;&lt;BR&gt;}&lt;BR&gt;&lt;/FONT&gt;&amp;lt;\code&amp;gt;&lt;/P&gt;
&lt;P&gt;For this convention the mangled name would be &lt;A href="mailto:?pf@@YIXHH@Z" mce_href="mailto:?foo@@YIXHHH@Z"&gt;?pf@@YIXHH@Z&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;D:\VS2008\VC\bin&amp;gt;undname &lt;A href="mailto:?pf@@YIXHH@Z" mce_href="mailto:?foo@@YIXHHH@Z"&gt;?pf@@YIXHH@Z&lt;/A&gt;&lt;BR&gt;Microsoft (R) C++ Name Undecorator&lt;BR&gt;Copyright (C) Microsoft Corporation. All rights reserved.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Undecoration of :- "&lt;A href="mailto:?pf@@YIXHH@Z" mce_href="mailto:?foo@@YIXHHH@Z"&gt;?pf@@YIXHH@Z&lt;/A&gt;"&lt;BR&gt;is :- "void __fastcall pf(int,int,int)"&lt;BR&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;Now lets have a look at the disassembly&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;&amp;lt;DISASSEMBLY&amp;gt;&lt;BR&gt;&lt;FONT size=1&gt;void __fastcall pf(int a,int b)&lt;BR&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=1&gt;&lt;FONT color=#808080 size=1&gt;000413A0 push ebp &lt;BR&gt;000413A1 mov ebp,esp &lt;BR&gt;000413A3 sub esp,0D8h &lt;BR&gt;000413A9 push ebx &lt;BR&gt;000413AA push esi &lt;BR&gt;000413AB push edi &lt;BR&gt;000413AC push ecx &lt;BR&gt;000413AD lea edi,[ebp-0D8h] &lt;BR&gt;000413B3 mov ecx,36h &lt;BR&gt;000413B8 mov eax,0CCCCCCCCh &lt;BR&gt;000413BD rep stos dword ptr es:[edi] &lt;BR&gt;000413BF pop ecx &lt;BR&gt;000413C0 mov dword ptr [ebp-14h],edx &lt;BR&gt;000413C3 mov dword ptr [ebp-8],ecx &lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=1&gt;}&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#808080 size=1&gt;&lt;FONT color=#808080 size=1&gt;000413C6 pop edi &lt;BR&gt;000413C7 pop esi &lt;BR&gt;000413C8 pop ebx &lt;BR&gt;000413C9 mov esp,ebp &lt;BR&gt;000413CB pop ebp &lt;BR&gt;000413CC ret &lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;lt;DISASSEMBLY&amp;gt;&lt;BR&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;Ok this is strange, we see simply ret. But isint that in __fastcall, the callee is supposed to clear the stack. Yes you are correct, but lets revisit the first rule once again..&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The main essense is that , the first two parameters, if are equal to or less than 32 bits (like int), are pushed to the ECX and EDX&amp;nbsp;Registers.&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Rest&amp;nbsp;are&amp;nbsp;pushed to stack. &lt;/P&gt;
&lt;P mce_keep="true"&gt;So it means if the first two parameters (in our case int and int) are equal to or less than 32 bits each (they are as int is 4 byte), then they are pushed to ECX and EDX registers. So basically the parameters are not pushed to the stack, but to the registers itself in this case, so nothing to clear!!!&lt;/P&gt;
&lt;P mce_keep="true"&gt;So following this rule, do we expect to see ret 8, when the function is void fp (int a, int b, double c)? Yes you will see that.&lt;/P&gt;
&lt;P mce_keep="true"&gt;So what would we see when the function is void fp(double a, int b)?&lt;BR&gt;Ans: You would see ret 8, because out of the first two parameters, only second is less than or equal to 32 bits, so only that can be pushed to the register and first (8 bytes) is pushed to stack and hence ret 8.&lt;/P&gt;
&lt;P&gt;__thiscall&lt;BR&gt;=-=-=-=-=-&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Default calling convention, for calling member functions in C++. But if the member function, contains dynamic argument list, then the default calling convention falls back to the &lt;EM&gt;dirty&lt;/EM&gt; __cdecl (please don't fire me saying dirty :P)&lt;/LI&gt;
&lt;LI&gt;The parameters are passed from right to left and more importantly the &lt;EM&gt;this&lt;/EM&gt; pointer for objects&amp;nbsp;is passed to the ECX (ofcourse for non-static methods)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Things I didnt tell you - &amp;gt; There is also __clrcall for clr&amp;nbsp; :P&lt;/P&gt;
&lt;P&gt;I guess its enough for the day. I would leave you thinking about whatever you can, on this topic. Hope, I could clear some of the common doubts about calling conventions. Let me know if I left something here, i will give it a shot in part 2 under this topic. Don't foget to provide comments/feedback.&lt;/P&gt;
&lt;P&gt;__cdecl bye_bye(see you soon with a new and exiting topic)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8447422" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Function/default.aspx">Function</category><category domain="http://blogs.msdn.com/gouravdas/archive/tags/Calling+convention/default.aspx">Calling convention</category></item><item><title>My Second Blog ever!!</title><link>http://blogs.msdn.com/gouravdas/archive/2008/05/02/my-second-blog-ever.aspx</link><pubDate>Thu, 01 May 2008 21:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8447394</guid><dc:creator>gouravdas</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/gouravdas/comments/8447394.aspx</comments><wfw:commentRss>http://blogs.msdn.com/gouravdas/commentrss.aspx?PostID=8447394</wfw:commentRss><description>&lt;P&gt;Hello Guys,&lt;/P&gt;
&lt;P&gt;"Putting pen on paper" for the second time ever (as far as blogging is concerned). I will be targetting mainly on the technical learnings that I have learnt and will try to pass it over through this great medium of technet.&lt;/P&gt;
&lt;P&gt;As a beginner I have had many road blocks&amp;nbsp;understanding topics (its not that I am still not having those :) ) . I will try to put in a way where each one can understand the point, be it a beginner (or users who have not even begun).&lt;/P&gt;
&lt;P&gt;Keep pouring comments so that I am always pumped up to deliver more and more good quality blogs everytime.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8447394" width="1" height="1"&gt;</description></item></channel></rss>