<?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; x86</title>
	<atom:link href="http://www.coding.com.br/tag/x86/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>Opcode stats</title>
		<link>http://www.coding.com.br/embarcado/opcode-stats/</link>
		<comments>http://www.coding.com.br/embarcado/opcode-stats/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 00:09:50 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[embarcado]]></category>
		<category><![CDATA[arm]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=1128</guid>
		<description><![CDATA[I was trying some GCC options and decided see how they affect assembly code generated. I&#8217;ve created a simple Python script that parses .s output and put in a human readable way. Suppose the classical Hello World program. The output will be something like:
$ python stats_opcodes.py file
For Cortex-m3 (check previous post):

At my laptop (x86):

 

Download the script here.
]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I was trying some GCC options and decided see how they affect assembly code generated. I&#8217;ve created a simple Python script that parses <em>.s</em> output and put in a human readable way. Suppose the classical <em>Hello World </em>program. The output will be something like:</p>
<p style="padding-left: 30px;">$ python stats_opcodes.py file</p>
<p>For Cortex-m3 (check <a href="http://www.coding.com.br/embarcado/cortex-m3-and-qemu/" target="_blank">previous</a> post):</p>
<p style="padding-left: 30px;"><img class="aligncenter size-full wp-image-1136" title="opcode-arm" src="http://www.coding.com.br/wp-content/uploads/2010/06/opcode-arm.png" alt="" width="304" height="194" /></p>
<p>At my laptop (x86):</p>
<p style="padding-left: 30px;"><img class="aligncenter size-full wp-image-1133" title="output" src="http://www.coding.com.br/wp-content/uploads/2010/06/opcodes1.png" alt="" width="306" height="194" /></p>
<p><span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: small;"><span style="line-height: 18px; white-space: pre;"><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: small;"><span style="line-height: 19px; white-space: normal;"> </span></span></span></span></p>
<p style="text-align: center;">
<p>Download the script <a href="http://gist.github.com/raw/452170/db097f735c52ffd733d2060603c203982d53b79f/stats_opcode.py" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/embarcado/opcode-stats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Otimização</title>
		<link>http://www.coding.com.br/programacao/otimizacao/</link>
		<comments>http://www.coding.com.br/programacao/otimizacao/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 03:00:47 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=399</guid>
		<description><![CDATA[Dentre as diversas apresentações no Linux Kongress 2009 a palestra do Felix von Leitner&#8217;s me chamou atenção. Com título &#8220;Compiler Optimization Survey&#8221; ele discutiu os aspectos relacionados a programação nos compiladores modermos e as possibilidades de otimização (slides).  A idéia básica é que:

Otimização é importante
Mas frequentemente: Código legível é mais importante
Aprenda o que seu compilador [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Dentre as diversas <a href="http://www.linux-kongress.org/2009/program.html" target="_blank">apresentações</a> no <em>Linux Kongress 2009 </em>a palestra do Felix von Leitner&#8217;s me chamou atenção. Com título &#8220;<em>Compiler Optimization Survey</em>&#8221; ele discutiu os aspectos relacionados a programação nos compiladores modermos e as possibilidades de otimização (<a href="http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf" target="_blank">slides</a>).  A idéia básica é que:</p>
<ul>
<li>Otimização é importante</li>
<li>Mas frequentemente: Código legível é mais importante</li>
<li>Aprenda o que seu compilador faz<br />
E então deixe-o fazê-lo.</li>
</ul>
<p style="text-align: justify;">Exemplo. As quatro instruções abaixo fazem a mesma coisa, zeram um registrador da CPU x86, qual é a melhor forma?</p>
<p><img class="aligncenter size-full wp-image-400" title="Zerando registros" src="http://www.coding.com.br/wp-content/uploads/2009/11/a.png" alt="Zerando registros" width="153" height="88" /></p>
<p>Analisando no GDB (x/FMT ADDRESS) temos:</p>
<pre>movl  0xb8    0x00    0x00    0x00    0x00
andl  0x83    0xe0    0x00
subl  0x29    <span style="color: #ff0000;">0xc0 </span> 
xorl  0x31    <span style="color: #ff0000;">0xc0
</span><span style="color: #000000;"> </span></pre>
<p>E agora? <strong>sub</strong> ou <strong>xor</strong>, veja que ambos produzem uma <a href="http://en.wikipedia.org/wiki/Register_renaming" target="_blank">falsa dependência</a> em %eax, mas a CPU sabe como ignorar no caso do xor.</p>
<p style="text-align: center;">Você sabia?</p>
<p style="text-align: center;">O compilador sim.</p>
<p style="text-align: left;">Se você usar o sub na prova seu professor provavelmente irá descontar alguns pontos ou te dar zero&#8230;</p>
<ul>
<li>Se você fizer uma otimizacão, teste em dados reais.</li>
<li>Se tornar o código ilegível não tornar o programa <span style="text-decoration: underline;">drasticamente</span> rápido, não faça.</li>
</ul>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 476px; width: 1px; height: 1px;">movl  0xb8    0&#215;00    0&#215;00    0&#215;00    0&#215;00<br />
andl  0&#215;83    0xe0    0&#215;00<br />
subl  0&#215;29    0xc0<br />
xorl  0&#215;31    0xc0</div>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/otimizacao/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Depurando programas em assembly no GNU/Linux (parte 1)</title>
		<link>http://www.coding.com.br/programacao/depurando-programas-em-assembly-no-gnulinux-parte-1/</link>
		<comments>http://www.coding.com.br/programacao/depurando-programas-em-assembly-no-gnulinux-parte-1/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 02:41:03 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[nasm]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=276</guid>
		<description><![CDATA[Embora eu particularmente prefira a sintaxe AT&#38;T à Intel. Estou tendo que aprender a usá-la (e bem). Para os estudos optei pelo NASM, GNU ld e o GDB. Um processo simples, descrito abaixo:
#nasm -g -f elf programa.asm 
#ld programa.o
#gdb -q a.out
Vamos fazer um pequeno programa teste que carrega no registro ecx o valor da variável [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Embora eu particularmente <a href="http://www.ibm.com/developerworks/linux/library/l-gas-nasm.html">prefira</a> a sintaxe AT&amp;T à Intel. Estou <a href="http://www.unifei.edu.br">tendo</a> que aprender a usá-la (e bem). Para os estudos optei pelo <a href="http://www.nasm.us/">NASM</a>, <a href="http://www.gnu.org/software/binutils/">GNU ld</a> e o <a href="http://www.gnu.org/software/gdb/">GDB</a>. Um processo simples, descrito abaixo:</p>
<p style="padding-left: 30px;">#<strong>nasm</strong> -g -f elf <span style="color: #000000;">programa.asm </span><br />
#<strong>ld</strong> <span style="color: #000000;">programa.o</span><br />
#<strong>gdb</strong> -q <span style="color: #000000;">a.out</span></p>
<p>Vamos fazer um pequeno programa teste que carrega no registro <em>ecx</em> o valor da variável (var1) e em <em>edx</em> seu endereço:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">section <span style="color: #000000; font-weight: bold;">.data</span>
&nbsp;
   var1 <span style="color: #000000; font-weight: bold;">dd</span> <span style="color: #0000ff;">40</span>
&nbsp;
section <span style="color: #339933;">.</span>text
&nbsp;
   global _start
&nbsp;
   _start<span style="color: #339933;">:</span>
      <span style="color: #00007f; font-weight: bold;">nop</span>
      <span style="color: #00007f; font-weight: bold;">nop</span>
      <span style="color: #00007f; font-weight: bold;">mov</span>   <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>var1<span style="color: #009900; font-weight: bold;">&#93;</span>
      <span style="color: #00007f; font-weight: bold;">lea</span>   <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>var1<span style="color: #009900; font-weight: bold;">&#93;</span>
&nbsp;
   _exit<span style="color: #339933;">:</span>
      <span style="color: #00007f; font-weight: bold;">mov</span>   <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">1</span>
      <span style="color: #00007f; font-weight: bold;">int</span>   <span style="color: #0000ff;">80h</span></pre></div></div>

<p>Dentro do gdb:</p>
<pre>(gdb) break 11
Breakpoint 1 at 0x80480a1: file teste.asm, line 11.
(gdb) r
Starting program: /home/maluta/coding/a.out
Breakpoint 1, _start () at teste.asm:11
11            nop
(gdb) print var1
$1 = 40
(gdb) info registers ecx edx
ecx            0x0      0
edx            0x0      0</pre>
<p>Adicionamos um <a href="http://sourceware.org/gdb/current/onlinedocs/gdb_6.html#SEC34">breakpoint</a> (eu precisei inserir duas instruções <em>nop</em> para o gdb realmente para no ponto) e verificamos os valores de <strong>var1</strong>, <strong>ecx</strong> e <strong>edx</strong>.</p>
<pre>(gdb) si
_start () at teste.asm:12
12            mov   ecx, [var1]
(gdb) si
_start () at teste.asm:13
13            lea   edx, [var1]
(gdb) info registers ecx
ecx            0x28     40
(gdb) si
_exit () at teste.asm:16
16            mov   eax, 1
(gdb) info registers edx
edx            0x80490b8        134516920</pre>
<p>E por fim, verificamos o conteúdo no endereço definido em edx.</p>
<pre>(gdb) print *0x80490b8
$2 = 40</pre>
<p style="text-align: justify;">Algumas considerações:</p>
<ul style="text-align: justify;">
<li style="text-align: justify;">O <a href="http://www.gnu.org/software/ddd/">Data Diplay Debugger</a> (DDD) é um <em>front-end</em> muito bom para o GDB que pode ser utilizado ao invés da interface de linha de comando.</li>
</ul>
<ul style="text-align: justify;">
<li style="text-align: justify;">O após <em>label</em> <strong>_exit </strong>há uma chamada a uma <em>syscall</em> (adivinha qual?) do <a href="http://www.kernel.org">kernel</a>. Passa-se o valor (no registro eax) e chama uma interrupção (80h).  A verdadeira &#8220;diversão&#8221; está em associar essas syscalls (write, fork, execve, &#8230;) mas isto é para outro <em>post</em>&#8230; Por enquanto vou lidar com os diferentes modos de endereçamento e operações lógicas e aritméticas.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/depurando-programas-em-assembly-no-gnulinux-parte-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
