<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>redstack &#187; The Basics</title>
	<atom:link href="http://redstack.net/blog/category/basics/feed/" rel="self" type="application/rss+xml" />
	<link>http://redstack.net/blog</link>
	<description>Pirates are way cooler than Ninjas, but not as much as Samuraïs</description>
	<lastBuildDate>Tue, 14 Dec 2010 17:14:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>x86 calling conventions</title>
		<link>http://redstack.net/blog/2008/01/16/x86-calling-conventions/</link>
		<comments>http://redstack.net/blog/2008/01/16/x86-calling-conventions/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 03:45:37 +0000</pubDate>
		<dc:creator>xipe</dc:creator>
				<category><![CDATA[The Basics]]></category>

		<guid isPermaLink="false">http://redstack.net/blog/index.php/2008/01/16/x86-calling-conventions.html</guid>
		<description><![CDATA[This is the first article of a (I hope) long series of articles about &#8216;The Basics: What everyone should know about&#8217; The calling convention defines the way a function or a piece of code should arrange data before calling a function, and what to do after. It responds to questions like &#8220;In which order should [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first article of a (I hope) long series of articles about &#8216;The Basics: What everyone should know about&#8217; <img src='http://redstack.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The calling convention defines the way a function or a piece of code should arrange data before calling a function, and what to do after. It responds to questions like <em>&#8220;In which order should I pass the arguments ?&#8221;</em>, <em>&#8220;Should I clean something ?&#8221;</em>, <em>&#8220;Where is the result ?&#8221;</em>, &#8230;</p>
<p>There is a lot of different calling conventions. Here are the 3 I see the most of the time:</p>
<ul>
<li>cdecl</li>
<li>stdcall</li>
<li>fastcall</li>
</ul>
<p><strong>cdecl convention</strong></p>
<p>The <strong>cdecl</strong> convention is the default one used when working with a C compiler like GCC or MSVC. To use the <strong>cdecl</strong> scheme for a function, you can use this syntax (GCC):<span id="more-8"></span></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">__attribute__<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>cdecl<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #993333;">int</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> arg1<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> arg2<span style="color: #339933;">,</span> ...<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>GCC will produce the following code when calling a <strong>cdecl</strong> function with 4 arguments :</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x4</span> <span style="color: #666666; font-style: italic;">; arg4</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x3</span> <span style="color: #666666; font-style: italic;">; arg3</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x2</span> <span style="color: #666666; font-style: italic;">; arg2</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x1</span> <span style="color: #666666; font-style: italic;">; arg1</span>
<span style="color: #00007f; font-weight: bold;">call</span>   _cdecl_fct
<span style="color: #00007f; font-weight: bold;">add</span>    <span style="color: #00007f;">esp</span><span style="color: #339933;">,</span><span style="color: #0000ff;">0x10</span>
<span style="color: #00007f; font-weight: bold;">mov</span>    <span style="color: #000000; font-weight: bold;">DWORD</span> <span style="color: #000000; font-weight: bold;">PTR</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">-</span><span style="color: #0000ff;">0x4</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span><span style="color: #00007f;">eax</span></pre></div></div>

<p>As you can see, arguments are pushed into the stack in right to left order, and it&#8217;s up to the caller to remove the arguments from the stack (Here this is done by <code>add esp, 0x10</code>). The result of the function is stored in the EAX register.</p>
<p><strong>stdcall convention</strong></p>
<p>The <strong>stdcall</strong> convention is the one used by Win32 APIs. It&#8217;s also the easyest to use when writing ASM code, in my opinion. A function can be declared as a <strong>stdcall</strong> function in C with this syntax (GCC):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">__attribute__<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>stdcall<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #993333;">int</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> arg1<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> arg2<span style="color: #339933;">,</span> ...<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>GCC will produce the following code when calling a <strong>stdcall</strong> function with 4 arguments :</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x4</span> <span style="color: #666666; font-style: italic;">; arg4</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x3</span> <span style="color: #666666; font-style: italic;">; arg3</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x2</span> <span style="color: #666666; font-style: italic;">; arg2</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x1</span> <span style="color: #666666; font-style: italic;">; arg1</span>
<span style="color: #00007f; font-weight: bold;">call</span>   _stdcall_fct@<span style="color: #0000ff;">16</span>
<span style="color: #00007f; font-weight: bold;">mov</span>    <span style="color: #000000; font-weight: bold;">DWORD</span> <span style="color: #000000; font-weight: bold;">PTR</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">-</span><span style="color: #0000ff;">0x4</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span><span style="color: #00007f;">eax</span></pre></div></div>

<p>As for the <strong>cdecl</strong> calling style, arguments are pushed from right to left, but in <strong>stdcall</strong> mode, the caller doesn&#8217;t have to clean the arguments from the stack after calling the function. A <strong>stdcall</strong> function removes arguments from the stack before returning. This is done by using the <code>ret n</code> instruction most of the time.<br />
Like for <strong>cdecl</strong>, result is in EAX.</p>
<p><strong>fastcall convention</strong></p>
<p>The <strong>fastcall</strong> convention is not standardized, but we will watch the way GCC and MSVC handle it. A function can be declared as a <strong>fastcall</strong> function in C with this syntax (GCC):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">__attribute__<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>fastcall<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #993333;">int</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> arg1<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> arg2<span style="color: #339933;">,</span> ...<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>GCC will produce the following code when calling a <strong>stdcall</strong> function with 4 arguments :</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x4</span> <span style="color: #666666; font-style: italic;">; arg4</span>
<span style="color: #00007f; font-weight: bold;">push</span>   <span style="color: #0000ff;">0x3</span> <span style="color: #666666; font-style: italic;">; arg3</span>
<span style="color: #00007f; font-weight: bold;">mov</span>    <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">0x2</span> <span style="color: #666666; font-style: italic;">; arg2</span>
<span style="color: #00007f; font-weight: bold;">mov</span>    <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">0x1</span> <span style="color: #666666; font-style: italic;">; arg1</span>
<span style="color: #00007f; font-weight: bold;">call</span>   @fastcall_fct@<span style="color: #0000ff;">16</span>
<span style="color: #00007f; font-weight: bold;">mov</span>    <span style="color: #000000; font-weight: bold;">DWORD</span> <span style="color: #000000; font-weight: bold;">PTR</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">-</span><span style="color: #0000ff;">0x4</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span><span style="color: #00007f;">eax</span></pre></div></div>

<p>As you can see, not all the arguments are pushed into the stack. The first two arguments are passed via the ECX, for the first argument, and EDX, for the second argument. The remaining arguments are pushed into the stack from right to left. The called function has to pop the arguments from the stack before returning, like for <strong>stdcall</strong>.<br />
The result is, as usual, in EAX <img src='http://redstack.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://redstack.net/blog/2008/01/16/x86-calling-conventions/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

