<?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>Algorithm Blogs &#187; Assembly</title>
	<atom:link href="http://www.algorithm.co.il/blogs/index.php/tag/assembly/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Thu, 22 Apr 2010 21:04:32 +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>Some Assembly Required No. 1</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/some-assembly-required-no-1/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/some-assembly-required-no-1/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 20:17:05 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[vial]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/some-assembly-required-no-1/</guid>
		<description><![CDATA[I've been working on some of the instruction tests in vial, and I wanted to test the implementation of LOOP variants. My objective was to make sure the vial version is identical to the real CPU version (as discussed here). To achieve this, I had to cover all of the essential behaviors of LOOP.
Well, using [...]]]></description>
			<content:encoded><![CDATA[<p>I've been working on some of the instruction tests in vial, and I wanted to test the implementation of LOOP variants. My objective was to make sure the vial version is identical to the real CPU version (<a href="http://www.algorithm.co.il/blogs/index.php/programming/issues-in-writing-a-vm-part-1/">as discussed here</a>). To achieve this, I had to cover all of the essential behaviors of LOOP.</p>
<p>Well, using the framework <a href="http://www.ragestorm.net/blogs/?p=58">Gil and I wrote</a>, I hacked up some code that should cover the relevant cases:</p>
<div class="syntax_hilite">
<div id="python-2">
<div class="python">code_template = <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"<br />
mov edx, ecx ; control the start zf<br />
mov ecx, eax ; number of iterations<br />
mov eax, 0 ; will hold the result, also an iteration counter<br />
loop_start:</p>
<p>&nbsp; &nbsp; cmp eax, ebx&nbsp; &nbsp; ; check if we need to change zf<br />
&nbsp; &nbsp; setz dh<br />
&nbsp; &nbsp; xor dh, dl&nbsp; &nbsp; &nbsp; ; if required, invert zf<br />
&nbsp; &nbsp; inc eax&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;; count the iteration<br />
&nbsp; &nbsp; cmp dh, 0&nbsp; &nbsp; &nbsp; &nbsp;; set zf</p>
<p>&nbsp; &nbsp; loop%s loop_start<br />
"</span><span style="color: #483d8b;">""</span><br />
<span style="color: #00007f;font-weight:bold;">for</span> loop_kind <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">''</span>,<span style="color: #483d8b;">'z'</span>,<span style="color: #483d8b;">'nz'</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; code_text = code_template % loop_kind<br />
&nbsp; &nbsp; c = FuncObject<span style="color: black;">&#40;</span>code_text<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> start_zf_value <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> num_iters <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">4</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> when_zf_changes <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">15</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: black;">&#40;</span>num_iters, when_zf_changes, start_zf_value<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #000000;">check</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>Note that c(...) executes the code both on vial's VM, and on the real cpu. c.check() compares their return value (EAX) and flags after the execution. I also wanted to avoid other kinds of jumps in this test.</p>
<p>To check that the code ran the same number of times, I returned EAX as the number of iterations.<br />
All the games with edx are there to make sure that I'm testing different zf conditions.</p>
<p><strong>The challenge for today:</strong><br />
Can you write a shorter assembly snippet that tests the same thing?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/some-assembly-required-no-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
