<?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>porges &#187; asm</title>
	<atom:link href="http://porg.es/blog/tag/asm/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description>... master of none</description>
	<lastBuildDate>Sun, 22 Aug 2010 21:36:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Generating x86 assembly with Haskell</title>
		<link>http://porg.es/blog/generating-x86-assembly-with-haskell</link>
		<comments>http://porg.es/blog/generating-x86-assembly-with-haskell#comments</comments>
		<pubDate>Mon, 03 Nov 2008 12:51:14 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=236</guid>
		<description><![CDATA[A quick and dirty example: data Exp = Lit Int -- literal value &#124; Call String Exp -- calling a function &#124; Bin BinOp Exp Exp -- binary operations &#124; Param -- the parameter deriving &#40;Show,Eq&#41; &#160; data Function = Func String Exp -- a function has a name and expression data BinOp = Add [...]]]></description>
			<content:encoded><![CDATA[<p>A quick and dirty example:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Exp <span style="color: #339933; font-weight: bold;">=</span> Lit <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #5d478b; font-style: italic;">-- literal value</span>
	<span style="color: #339933; font-weight: bold;">|</span> Call <span style="color: #cccc00; font-weight: bold;">String</span> Exp <span style="color: #5d478b; font-style: italic;">-- calling a function</span>
	<span style="color: #339933; font-weight: bold;">|</span> Bin BinOp Exp Exp <span style="color: #5d478b; font-style: italic;">-- binary operations</span>
	<span style="color: #339933; font-weight: bold;">|</span> Param <span style="color: #5d478b; font-style: italic;">-- the parameter</span>
	<span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: #cccc00; font-weight: bold;">Eq</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Function <span style="color: #339933; font-weight: bold;">=</span> Func <span style="color: #cccc00; font-weight: bold;">String</span> Exp <span style="color: #5d478b; font-style: italic;">-- a function has a name and expression</span>
<span style="color: #06c; font-weight: bold;">data</span> BinOp <span style="color: #339933; font-weight: bold;">=</span> Add <span style="color: #339933; font-weight: bold;">|</span> Subtract <span style="color: #339933; font-weight: bold;">|</span> Multiply <span style="color: #339933; font-weight: bold;">|</span> Divide <span style="color: #06c; font-weight: bold;">deriving</span><span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: #cccc00; font-weight: bold;">Eq</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- an instance to allow us to write nicer definitions</span>
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: #cccc00; font-weight: bold;">Num</span> Exp <span style="color: #06c; font-weight: bold;">where</span>
	x <span style="color: #339933; font-weight: bold;">*</span> y <span style="color: #339933; font-weight: bold;">=</span> Bin Multiply x y
	x <span style="color: #339933; font-weight: bold;">-</span> y <span style="color: #339933; font-weight: bold;">=</span> Bin Subtract x y
	x <span style="color: #339933; font-weight: bold;">+</span> y <span style="color: #339933; font-weight: bold;">=</span> Bin Add x y
	<span style="font-weight: bold;">fromInteger</span> x <span style="color: #339933; font-weight: bold;">=</span> Lit <span style="color: green;">&#40;</span><span style="font-weight: bold;">fromInteger</span> x<span style="color: green;">&#41;</span>
&nbsp;
outFunc <span style="color: green;">&#40;</span>Func name it<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> name <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- output function name</span>
	<span style="background-color: #3cb371;">&quot;:<span style="background-color: #3cb371; font-weight: bold;">\n</span>push ebp<span style="background-color: #3cb371; font-weight: bold;">\n</span>mov ebp,esp<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- save old stack frame</span>
	output it <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- output function</span>
	<span style="background-color: #3cb371;">&quot;pop eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>pop ebp<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: #5d478b; font-style: italic;">-- restore old stack frame</span>
	<span style="background-color: #3cb371;">&quot;ret 4<span style="background-color: #3cb371; font-weight: bold;">\n</span><span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- remove argument from stack and return</span>
&nbsp;
output <span style="color: green;">&#40;</span>Bin op l r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> outBin op l r <span style="color: #5d478b; font-style: italic;">-- binary operations</span>
output <span style="color: green;">&#40;</span>Lit x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;push dword &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> x <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- push literal</span>
output <span style="color: green;">&#40;</span>Param<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;push dword [ebp+8]<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- push parameter</span>
output <span style="color: green;">&#40;</span>Call name with<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> output with <span style="color: #339933; font-weight: bold;">++</span>
	<span style="background-color: #3cb371;">&quot;call &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> name <span style="color: #339933; font-weight: bold;">++</span>
	<span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\n</span>push eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #5d478b; font-style: italic;">-- put return value onto stack</span>
&nbsp;
outBin Add <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;add&quot;</span>
outBin Subtract <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;sub&quot;</span>
outBin Multiply <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;imul&quot;</span>
outBin Divide <span style="color: #339933; font-weight: bold;">=</span> outBinGen <span style="background-color: #3cb371;">&quot;idiv&quot;</span>
&nbsp;
outBinGen op l r <span style="color: #339933; font-weight: bold;">=</span> output l <span style="color: #339933; font-weight: bold;">++</span> output r <span style="color: #339933; font-weight: bold;">++</span>
	<span style="background-color: #3cb371;">&quot;pop ebx<span style="background-color: #3cb371; font-weight: bold;">\n</span>pop eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> op <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot; eax,ebx<span style="background-color: #3cb371; font-weight: bold;">\n</span>push eax<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span></pre></div></div>

<p>And an example of using it:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">putStr</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">concatMap</span> outFunc <span style="color: green;">&#91;</span>double<span style="color: #339933; font-weight: bold;">,</span>quad<span style="color: green;">&#93;</span>
double <span style="color: #339933; font-weight: bold;">=</span> Func <span style="background-color: #3cb371;">&quot;double&quot;</span> <span style="color: green;">&#40;</span>Param <span style="color: #339933; font-weight: bold;">*</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span>
quad <span style="color: #339933; font-weight: bold;">=</span> Func <span style="background-color: #3cb371;">&quot;quad&quot;</span> <span style="color: green;">&#40;</span>Call <span style="background-color: #3cb371;">&quot;double&quot;</span> <span style="color: green;">&#40;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">*</span>Param<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></div></div>

<p>This example outputs:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">double<span style="color: #339933;">:</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span><span style="color: #00007f;">esp</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</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;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #0000ff;">2</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">imul</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">ret</span> <span style="color: #0000ff;">4</span>
&nbsp;
quad<span style="color: #339933;">:</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span><span style="color: #00007f;">esp</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #0000ff;">2</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #000000; font-weight: bold;">dword</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;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">imul</span> <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #00007f;">ebx</span>
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">call</span> double
	<span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">eax</span>
	<span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ebp</span>
	<span style="color: #00007f; font-weight: bold;">ret</span> <span style="color: #0000ff;">4</span></pre></div></div>

<p>Not the most efficient code in the world <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_wink.gif" alt="" /> The calling convention is somewhat ad-hoc; the callee must remove its arguments from the stack and returns its value in <code>eax</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/generating-x86-assembly-with-haskell/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
