<?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>coding &#187; asm</title>
	<atom:link href="http://www.coding.com.br/tag/asm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coding.com.br</link>
	<description>have you coded today?</description>
	<lastBuildDate>Fri, 23 Jul 2010 02:37:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Assembly inline</title>
		<link>http://www.coding.com.br/programacao/assembly-inline/</link>
		<comments>http://www.coding.com.br/programacao/assembly-inline/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 02:20:11 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=444</guid>
		<description><![CDATA[I&#8217;ve made some code snippets about assembly inline with GCC. A quick search points to a lot of good documentation.

IBM DeveloperWorks about Inline asssembly for x86 in Linux
Linux Documentation Project HOWTO describing GCC Inline Assembly 

The syntax may be confusing, if you don&#8217;t understand read the documentation available. Each example are an C function with [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I&#8217;ve made some code snippets about assembly inline with <a href="http://gcc.gnu.org/">GCC</a>. A <a href="http://www.google.com.br/search?sourceid=chrome&amp;ie=UTF-8&amp;q=assembly+inline+gcc">quick search</a> points to a lot of good documentation.</p>
<ul>
<li>IBM DeveloperWorks about <em><a href="http://www.ibm.com/developerworks/linux/library/l-ia.html" target="_blank">Inline asssembly for x86 in Linux</a></em></li>
<li>Linux Documentation Project HOWTO describin<em>g <a href="http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html">GCC Inline Assembly</a> </em></li>
</ul>
<p style="text-align: justify;">The syntax may be confusing, if you don&#8217;t understand read the documentation available. Each example are an C function with the special construct &#8220;asm&#8221; for inline assembly code. Examples:</p>
<p>Increment an value:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> inc<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;incl %0&quot;</span>
         <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;=a&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Exchange to numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> swap<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>
        <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;=a&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;=b&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;b&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Copy an vector:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> vector_copy<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>v_src<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>v_dst<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;up: lodsl;&quot;</span>
	<span style="color: #ff0000;">&quot;    stosl;&quot;</span>
	<span style="color: #ff0000;">&quot;loop up;  &quot;</span>
	<span style="color: #339933;">:</span>
	<span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;S&quot;</span><span style="color: #009900;">&#40;</span>v_src<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;D&quot;</span><span style="color: #009900;">&#40;</span>v_dst<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;%eax&quot;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A simple copy:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> copy<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>to<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     asm <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;movl %1, %%eax;&quot;</span>
          <span style="color: #ff0000;">&quot;movl %%eax, %0;&quot;</span>
          <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;=&amp;amp;r&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>to<span style="color: #009900;">&#41;</span>
          <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>from<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A very simple implementation for strncpy:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> _strncpy<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>src<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>dst<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cld;&quot;</span>
        <span style="color: #ff0000;">&quot;rep movsb;&quot;</span>
        <span style="color: #339933;">:</span>
        <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;S&quot;</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 'S' == %esi</span>
	  <span style="color: #ff0000;">&quot;D&quot;</span><span style="color: #009900;">&#40;</span>dst<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 'D' == %edi</span>
	  <span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 'c' == %ecx</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you like to try check this <a href="http://github.com/maluta/junk/blob/master/asm-inline.c">example</a>:</p>
<pre style="padding-left: 30px;">wget http://github.com/maluta/junk/raw/master/asm-inline.c
gcc -Wall -ggdb asm-inline.c -o asm-inline
./asm-inline</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/assembly-inline/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
