<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Things that were not immediately obvious to me</title>
	
	<link>http://www.mlsite.net/blog</link>
	<description>Making mistakes so you don't have to</description>
	<lastBuildDate>Sun, 14 Apr 2013 20:37:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ThingsThatWereNotImmediatelyObviousToMe" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="thingsthatwerenotimmediatelyobvioustome" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Reelviews #3: Seven Days in May</title>
		<link>http://www.mlsite.net/blog/?p=3382</link>
		<comments>http://www.mlsite.net/blog/?p=3382#comments</comments>
		<pubDate>Wed, 08 Aug 2012 06:37:36 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Reelviews]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3382</guid>
		<description><![CDATA[&#8220;Seven Days in May&#8221;, of 1964, (dir. John Frankenheimer, from Rod Serling&#8217;s screen adaptation of the novel) tells a story of inter-service rivalry in the middle years of the Cold War. Burt Lancaster plays USAF Gen. James Mattoon Scott, appointed &#8230; <a href="http://www.mlsite.net/blog/?p=3382">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_3383" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.mlsite.net/blog/wp-content/uploads/2012/08/7days.png"><img src="http://www.mlsite.net/blog/wp-content/uploads/2012/08/7days-300x185.png" alt="VADM Barnswell" title="7days" width="300" height="185" class="size-medium wp-image-3383" /></a><p class="wp-caption-text">This man is not about to yield to the junior service.</p></div>
<p>&#8220;Seven Days in May&#8221;, of 1964, (dir. John Frankenheimer, from Rod Serling&#8217;s screen adaptation of the novel) tells a story of inter-service rivalry in the middle years of the Cold War.  Burt Lancaster plays USAF Gen. James Mattoon Scott, appointed to the Chairmanship of the Joint Chiefs of Staff by deeply unpopular President Jordan Lyman (Frederick March) in a calculated affront to the Navy Department.  The Navy is able to maneuver Lancaster into attempting an ill-conceived coup by playing to his vanity, before undermining him (and, as a bonus, the other service chiefs) with a suspiciously well-timed confession delivered by VADM Barnswell (John Houseman).  A misguided romantic subplot does not detract from this subtle tale of political intrigue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3382</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CANVAS Gotcha (Precision)</title>
		<link>http://www.mlsite.net/blog/?p=3375</link>
		<comments>http://www.mlsite.net/blog/?p=3375#comments</comments>
		<pubDate>Mon, 02 Jul 2012 05:21:32 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Web stuff]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3375</guid>
		<description><![CDATA[Been playing around with the &#60;CANVAS&#62; tag. Overall, it works pretty well. However, there was this little bit o&#8217; joy: The transforms are all based on 32-bit floats. (See the WebKit code, e.g. translate, transform, &#38;c.) Since 32-bit floats only &#8230; <a href="http://www.mlsite.net/blog/?p=3375">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Been playing around with the &lt;CANVAS&gt; tag.  Overall, it works pretty well.  However, there was this little bit o&#8217; joy:  The transforms are all based on 32-bit floats.  (See the <a href="http://trac.webkit.org/browser/trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp">WebKit code</a>, e.g. translate, transform, &amp;c.)  Since 32-bit floats only give you <a href="http://en.wikipedia.org/wiki/Floating_point#Internal_representation">23 bits of mantissa</a>, you have to be careful when working with very large domains.</p>
<p>Converting all your coordinates to be relative to your viewport origin (in 64-bit JS) seems to work OK, although it is a bit hacky.</p>
<p>I&#8217;m not sure why someone would decide to use 32-bit floats in this context &#8212; &#8220;this context&#8221; being a browser that must also support JavaScript and its 64-bit floats.  Maybe some mobile platforms don&#8217;t support double-precision FP, and so this was written to the lowest common denominator.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3375</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scott’s Space Wars</title>
		<link>http://www.mlsite.net/blog/?p=3358</link>
		<comments>http://www.mlsite.net/blog/?p=3358#comments</comments>
		<pubDate>Sun, 01 Apr 2012 09:10:22 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3358</guid>
		<description><![CDATA[I recently stumbled across this page, which describes a very early computer game (in fact, a heavily-qualified &#8220;first&#8221;). The page included a link to scans of the original machine code. The page also included this portentous sentence: It has been &#8230; <a href="http://www.mlsite.net/blog/?p=3358">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently stumbled across <a href="http://exoticsciences.com/">this page</a>, which describes a very early computer game (in fact, a heavily-qualified &#8220;first&#8221;).  The page included a link to scans of the original <a href="http://exoticsciences.com/swgmanu.htm">machine code</a>.  The page also included this portentous sentence:</p>
<blockquote><p>It has been suggested that the intact program in the manuscript be replicated and made into an emulation of the game to run on modern hardware.</p></blockquote>
<p>So, being highly suggestible, and apparently having no other demands on my time, that&#8217;s exactly <a href="http://www.mlsite.net/ssw/">what I did</a>.  (To be fair, I was also sort of curious about just how fast modern computers and JS engines had gotten.  Was it feasible to do instruction-level emulation of early hardware in a browser?  Yes.  Yes, it <a href="http://bellard.org/jslinux/tech.html">was</a>.)</p>
<p>I hope you <a href="http://www.mlsite.net/ssw/">enjoy the game</a>.  Thanks to Richard Adams for answering some of my questions, and to Scott Adams for writing the game and (hopefully) not suing me.  Since this sucked up a disproportionate amount of my time, you can expect some followup posts milking this little exercise for all it&#8217;s worth.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3358</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reelviews #2: The Fountainhead</title>
		<link>http://www.mlsite.net/blog/?p=3353</link>
		<comments>http://www.mlsite.net/blog/?p=3353#comments</comments>
		<pubDate>Sun, 18 Mar 2012 20:51:52 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Reelviews]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3353</guid>
		<description><![CDATA[In 1949&#8242;s &#8220;The Fountainhead&#8221;, Gary Cooper plays Howard Roark, a brilliant engineer and terrible architect. Roark offers a Faustian bargain to the city of New York; he will solve their most pressing civil engineering problems, but only on condition that &#8230; <a href="http://www.mlsite.net/blog/?p=3353">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_3354" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.mlsite.net/blog/wp-content/uploads/2012/03/fhead.png"><img src="http://www.mlsite.net/blog/wp-content/uploads/2012/03/fhead-300x165.png" alt="Roark&#039;s model" title="Roark&#039;s model" width="300" height="165" class="size-medium wp-image-3354" /></a><p class="wp-caption-text">Brilliant, but hideous</p></div>
<p>In 1949&#8242;s &#8220;The Fountainhead&#8221;, Gary Cooper plays Howard Roark, a brilliant engineer and terrible architect.  Roark offers a Faustian bargain to the city of New York; he will solve their most pressing civil engineering problems, but only on condition that they permit him to ruin their skyline.  When the city fathers attempt to renege on the deal, Roark&#8217;s revenge extracts a terrible toll on both the city at large and those closest to him.  From a screenplay by Ayn Rand, adapting her own novel.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3353</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reelviews #1: The Caine Mutiny</title>
		<link>http://www.mlsite.net/blog/?p=3346</link>
		<comments>http://www.mlsite.net/blog/?p=3346#comments</comments>
		<pubDate>Sun, 18 Mar 2012 01:41:31 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Reelviews]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3346</guid>
		<description><![CDATA[Possibly the first in a continuing series. 1954&#8242;s &#8220;The Caine Mutiny&#8221; (directed by Edward Dmytryk) tells the story of aging nightclub &#8220;singer&#8221; May Wynn&#8217;s (May Wynn, nee Donna Lee Hickey) attempts to secure a marriage to a young, handsome, but &#8230; <a href="http://www.mlsite.net/blog/?p=3346">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>Possibly the first in a continuing series.</em></p>
<div id="attachment_3347" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.mlsite.net/blog/wp-content/uploads/2012/03/caine.png"><img class="size-medium wp-image-3347" title="Mae Wynn" src="http://www.mlsite.net/blog/wp-content/uploads/2012/03/caine-300x166.png" alt="Mae Wynn" width="300" height="166" /></a><p class="wp-caption-text">The protagonist of &quot;The Caine Mutiny&quot;</p></div>
<p>1954&#8242;s &#8220;The Caine Mutiny&#8221; (directed by Edward Dmytryk) tells the story of aging nightclub &#8220;singer&#8221; May Wynn&#8217;s (May Wynn, nee Donna Lee Hickey) attempts to secure a marriage to a young, handsome, but somewhat dim scion of a prominent family.  Her plans are consistently frustrated by the fecklessness and weakness of her chosen target, Ens. Willie Keith (Robert Francis).  Trouble begins almost immediately, when Keith&#8217;s commander is able to maneuver him into turning down a beneficial promotion (arranged by his father) to the Admiral&#8217;s staff, and grows worse when he is unable to confront his family&#8217;s disapproval of Mae.  Finally, he finds himself a pawn in a mutinous plot launched by one of his fellow officers, which threatens to destroy Mae&#8217;s last chance at escaping her disreputable past.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3346</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Of Frogs and Boiling</title>
		<link>http://www.mlsite.net/blog/?p=3343</link>
		<comments>http://www.mlsite.net/blog/?p=3343#comments</comments>
		<pubDate>Wed, 21 Dec 2011 05:15:24 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Jack Handy]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3343</guid>
		<description><![CDATA[I was just looking over some old records, and discovered something that, at the time, escaped my attention. Between early 2002 and mid-2007, my cable company (AT&#38;T) raised my rates for the same service by about $35/mo, or nearly 50%. &#8230; <a href="http://www.mlsite.net/blog/?p=3343">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was just looking over some old records, and discovered something that, at the time, escaped my attention.</p>
<p>Between early 2002 and mid-2007, my cable company (AT&amp;T) raised my rates <em>for the same service</em> by about $35/mo, or nearly 50%.  Naturally this was done bit by bit &#8212; a dollar this month, 5 dollars that month, &amp;c.  Discovering this filled me with rage, and made me regret not canceling the service years earlier than I did.</p>
<p>That&#8217;s the last money you&#8217;ll get from me, you sneaky SOBs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3343</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ranking</title>
		<link>http://www.mlsite.net/blog/?p=3341</link>
		<comments>http://www.mlsite.net/blog/?p=3341#comments</comments>
		<pubDate>Sun, 18 Dec 2011 01:12:14 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Jack Handy]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3341</guid>
		<description><![CDATA[Better the rule of law than the rule of men. But better the rule of men than the rule of lawyers.]]></description>
			<content:encoded><![CDATA[<p>Better the rule of law than the rule of men.<br />
But better the rule of men than the rule of lawyers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3341</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Want a Job?</title>
		<link>http://www.mlsite.net/blog/?p=3337</link>
		<comments>http://www.mlsite.net/blog/?p=3337#comments</comments>
		<pubDate>Wed, 14 Dec 2011 07:08:54 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3337</guid>
		<description><![CDATA[I can&#8217;t recall if I mentioned it or not, but I spent a good bit of time looking for work when I was in Colombia … not there, but here. Between the Internet and Skype you can do 80% of &#8230; <a href="http://www.mlsite.net/blog/?p=3337">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I can&#8217;t recall if I mentioned it or not, but I spent a good bit of time looking for work when I was in <a href="http://www.mlsite.net/blog/?cat=28">Colombia</a> … not there, but here.  Between the Internet and Skype you can do 80% of your job search without even setting foot in the US.  It worked out, and since mid-May I&#8217;ve been working at Motorola on the <a href="http://www.mymotocast.com">MotoCast</a> team.</p>
<p>(Do I really need to mention that everything I say here is on my own account, and not on behalf of Motorola Mobility, Inc.?  Yes?  Well, consider it said.)</p>
<h3>Req</h3>
<p>Now I&#8217;m on the other side of the table, and looking to hire.  So if you&#8217;re a sharp engineer, I&#8217;d like to <a href="http://www.mlsite.net/mail.xhtml">hear from you</a>.  Nice-to-haves include Java, mobile, multimedia (GStreamer, transcoding, &#038;c.), and server-side, but the real requirement is great coding chops.</p>
<h3>What&#8217;s In It For You?</h3>
<p>Relative to some of the <a href="http://www.mlsite.net/blog/?p=2839">criteria</a> I&#8217;d laid out for what makes a good job, I can tell you that you&#8217;ll be working with &#8220;hot&#8221; technologies (mobile, &#8220;cloud&#8221;), writing a marketable language (mostly Java, some Ruby, and as much Python as I can sneak in), and solving important problems on an important product.</p>
<p>Compensation is good, work arrangements are flexible, and the people are agreeable.  If you live and work on the San Francisco peninsula, please <a href="http://www.mlsite.net/mail.xhtml">send me your resume</a>.  (Helpful resume writing tips <a href="http://www.mlsite.net/blog/?p=2247">here</a>.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3337</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GCHQ VM</title>
		<link>http://www.mlsite.net/blog/?p=3319</link>
		<comments>http://www.mlsite.net/blog/?p=3319#comments</comments>
		<pubDate>Sun, 04 Dec 2011 08:41:18 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Reverse Engineering]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3319</guid>
		<description><![CDATA[So, Britain&#8217;s GCHQ (the UK&#8217;s equivalent of the NSA) has put up a programming contest to attract coders. (The punchline is that the gig only pays $39,000, so good luck with that, chaps.) There are several parts, and the only &#8230; <a href="http://www.mlsite.net/blog/?p=3319">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So, Britain&#8217;s <a href="http://en.wikipedia.org/wiki/GCHQ">GCHQ</a> (the UK&#8217;s equivalent of the NSA) has <a href="http://abcnews.go.com/blogs/technology/2011/12/can-you-crack-it-uk-agencys-website-seeks-new-spies/">put up</a> a <a href="http://canyoucrackit.co.uk/">programming contest</a> to attract coders.  (The punchline is that the gig only pays <a href="http://www.telegraph.co.uk/news/uknews/defence/8929232/Would-be-spies-who-crack-GCHQ-code-directed-to-25000-job-vacancy.html">$39,000</a>, so good luck with that, chaps.)  There are several parts, and the only one that appealed to me required the user to implement a little VM.  Since I&#8217;m a sucker for <a href="http://www.mlsite.net/8086/">simple machines</a>, I thought I&#8217;d give it a go.  (The original puzzle is <a href="http://www.canyoucrackit.co.uk/15b436de1f9107f3778aad525e5d0b20.js">here</a>.)</p>
<p><span id="more-3319"></span></p>
<h3>Problem Statement</h3>
<p>Cutting to the chase, here&#8217;s the description of the VM:</p>
<pre class="code"><code>    // virtual machine architecture
    // ++++++++++++++++++++++++++++
    //
    // segmented memory model with 16-byte segment size (notation seg:offset)
    //
    // 4 general-purpose registers (r0-r3)
    // 2 segment registers (cs, ds equiv. to r4, r5)
    // 1 flags register (fl)
    //
    // instruction encoding
    // ++++++++++++++++++++
    //
    //           byte 1               byte 2 (optional)
    // bits      [ 7 6 5 4 3 2 1 0 ]  [ 7 6 5 4 3 2 1 0 ]
    // opcode      - - -
    // mod               -
    // operand1            - - - -
    // operand2                         - - - - - - - -
    //
    // operand1 is always a register index
    // operand2 is optional, depending upon the instruction set specified below
    // the value of mod alters the meaning of any operand2
    //   0: operand2 = reg ix
    //   1: operand2 = fixed immediate value or target segment (depending on instruction)
    //
    // instruction set
    // +++++++++++++++
    //
    // Notes:
    //   * r1, r2 =&gt; operand 1 is register 1, operand 2 is register 2
    //   * movr r1, r2 =&gt; move contents of register r2 into register r1
    //
    // opcode | instruction | operands (mod 0) | operands (mod 1)
    // -------+-------------+------------------+-----------------
    // 0x00   | jmp         | r1               | r2:r1
    // 0x01   | movr        | r1, r2           | rx,   imm
    // 0x02   | movm        | r1, [ds:r2]      | [ds:r1], r2
    // 0x03   | add         | r1, r2           | r1,   imm
    // 0x04   | xor         | r1, r2           | r1,   imm
    // 0x05   | cmp         | r1, r2           | r1,   imm
    // 0x06   | jmpe        | r1               | r2:r1
    // 0x07   | hlt         | N/A              | N/A
    //
    // flags
    // +++++
    //
    // cmp r1, r2 instruction results in:
    //   r1 == r2 =&gt; fl = 0
    //   r1 &lt; r2  =&gt; fl = 0xff
    //   r1 &gt; r2  =&gt; fl = 1
    //
    // jmpe r1
    //   =&gt; if (fl == 0) jmp r1
    //      else nop</code></pre>
<p>(Note that the complete problem statement also provides a JS definition of the VM, less its <code>exec</code> method, the implementation of which is the point of the exercise.  We&#8217;ll be taking this definition as a given for the rest of this post, so please consult the <a href="http://www.canyoucrackit.co.uk/15b436de1f9107f3778aad525e5d0b20.js">original puzzle</a> for details.)</p>
<h3>Helpers</h3>
<p>Since we&#8217;ll be going between opcode and JS representations of memory locations and registers, let&#8217;s build some util fxns to make that easier.  (I also threw in some convenience <code>vars</code> and a fxn to grab a byte from and increment the IP.)</p>
<pre class="code"><code>  exec: function()
  {
    // Convenience
    var cpu = this.cpu;
    var mem = this.mem;

    // Helpers
    function read_memory(s, o)
    {
        return mem[s*16 + o];
    }
    function write_memory(s, o, v)
    {
        mem[s*16 + o] = v;
    }
    function read_register(n)
    {
        switch(n)
        {
            case 0:
                return cpu.r0;
            case 1:
                return cpu.r1;
            case 2:
                return cpu.r2;
            case 3:
                return cpu.r3;
            case 4:
                // Equivalence from "docs"
                return cpu.cs;
            case 5:
                // Equivalence from "docs"
                return cpu.ds;
            default:
                throw "Unknown register index: " + n;
                break;
        }
    }
    function write_register(n, v)
    {
        switch(n)
        {
            case 0:
                cpu.r0 = v;
                break;
            case 1:
                cpu.r1 = v;
                break;
            case 2:
                cpu.r2 = v;
                break;
            case 3:
                cpu.r3 = v;
                break;
            case 4:
                // Equivalence from "docs"
                cpu.cs = v;
                break;
            case 5:
                // Equivalence from "docs"
                cpu.ds = v;
                break;
            default:
                throw "Unknown register index: " + n;
                break;
        }
    }
    function fetch_from_ip()
    {
        var r = read_memory(cpu.cs, cpu.ip);
        cpu.ip += 1;
        return r;
    }

    // ... more stuff ...
  }</code></pre>
<h3>Op Implementations</h3>
<p>With that done, we can write some implementation code for the JMP/JMPE, ADD, XOR, and CMP operations.  (MOV and HLT ops don&#8217;t require any logic beyond the helpers we just defined, since we&#8217;re not emitting any diagnostic information.)</p>
<pre class="code"><code>  exec: function()
  {
    // ... helpers ...

    // Op implementations
    function jmp(ix, seg, go)
    {
        if (!go) return;
        cpu.cs = seg;
        cpu.ip = read_register(ix);
    }
    function add(ix, v)
    {
        write_register(ix, read_register(ix)+v);
    }
    function xor(ix, v)
    {
        write_register(ix, read_register(ix)^v);
    }
    function cmp(ix, v1)
    {
        var v0 = read_register(ix);
        if (v0 == v1)
            cpu.fl = 0;
        else if (v0 &lt; v1)
            cpu.fl = 0xff;
        else
            cpu.fl = 1;
    }

    // ... more stuff ...
  }</code></pre>
<h3>Single-Step Interpreter and Loop</h3>
<p>We can now implement the core VM logic, and a simple loop to invoke it until we hit a HLT.  The only thing to note here is how much ambiguity there is in the VM specification:  Are JMPs absolute, or relative?  Does the 2nd argument to a FAR JMP evaluate to an immediate CS value, or a register index that should be dereferenced?  Where is the result of an ADD or XOR operation stored?  As it happened, my first guesses at the right answers to all such questions appeared to have been correct, but experiment was the only way to determine that.</p>
<pre class="code"><code>  exec: function()
  {
    // ... helpers ...

    // ... op implementations ...

    // Single-stepper
    function run_next_op()
    {
        var b       = fetch_from_ip();
        var opcode  = (b &amp; 0xe0) &gt;&gt; 5;
        var mod     = (b &amp; 0x10) &gt;&gt; 4;
        var ix      = (b &amp; 0x0f);

        switch (opcode)
        {
            case 0:
                jmp(ix, mod==0?cpu.cs:fetch_from_ip(), true);
                return true;
            case 1:
                write_register(ix, mod==0?read_register(fetch_from_ip()):fetch_from_ip());
                return true;
            case 2:
                if (mod == 0)
                    write_register(ix, read_memory(cpu.ds, read_register(fetch_from_ip())));
                else
                    write_memory(cpu.ds, read_register(ix), read_register(fetch_from_ip()));
                return true;
            case 3:
                add(ix, mod==0?read_register(fetch_from_ip()):fetch_from_ip());
                return true;
            case 4:
                xor(ix, mod==0?read_register(fetch_from_ip()):fetch_from_ip());
                return true;
            case 5:
                cmp(ix, mod==0?read_register(fetch_from_ip()):fetch_from_ip());
                return true;
            case 6:
                jmp(ix, mod==0?cpu.cs:fetch_from_ip(), cpu.fl==0);
                return true;
            case 7:
                return false;
        }
    }

    // Run until HLT
    while (run_next_op());
  }</code></pre>
<h3>The Answer</h3>
<p>After the VM halts, it&#8217;s not immediately clear what the answer to the puzzle is.  However, if you dump (the 768 bytes of) memory, interpret it as ASCII coded text, and  eyeball it for meaningful strings, an HTTP request pops out at you.</p>
<p>I tweaked the JS code to run in <a href="http://www.mozilla.org/rhino/">Rhino</a>, and added a dump statement to the bottom of <code>VM.exec()</code>:</p>
<pre class="code"><code>print(mem);</code></pre>
<p>I captured this dump into a file (the <code>-jar js.jar</code> business is a Rhino-ism):</p>
<pre class="code"><code>java -jar js.jar ~/Desktop/ukvm/src.js > mem.txt</code></pre>
<p>Next, a little Python (bolding added) revealed the answer:</p>
<pre class="code"><code>&gt;&gt;&gt; ''.join(chr(int(e)) for e in file('mem.txt', 'rb').read().split(','))
'1\x043\xaa@\x02\x80\x03R\x00r\x01s\x01\xb2P0\x14\xc0\x01\x80\x00\x10\x10
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x002\x00u
\x0c1\x0832@\x02\x80\x03R\x00r\x01s\x03\xb2\x00\xc3\xb0\x000\x1b\xc0\x01
\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00u\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\xcc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}\x1f\x15
`MMR}\x0e\'m\x10mZ\x06VG\x14B\x0e\xb6\xb2\xb2\xe6\xeb\xb4\x83\x8e\xd7\xe5
\xd4\xd9\xc3\xf0\x80\x95\xf1\x82\x82\x9a\xbd\x95\xa4\x8d\x9a+0iJieU\x1c{i
\x1cn\x04t5!&amp;/`\x03N7\x1e3T9\xe6\xba\xb4\xa2\xad\xa4\xc5\x95\xc8\xc1\xe4
\x8a\xec\xe7\x92\x8b\xe8\x81\xf0\xad\x98\xa4\xd0\xc0\x8d\xac"Re~\'+Z\x12a
\n\x01zk\x1dg<strong>GET /da75370fe15c4148bd4ceec861fbdaa5.exe HTTP/1.0</strong>\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x007z\x07\x11\x1f\x1dh%2w
\x1eb#[GUS0\x11B\xf6\xf1\xb1\xe6\xc3\xcc\xf8\xc5\xe4\xcc\xc0\xd3\x85\xfd
\x9a\xe3\xe6\x81\xb5\xbb\xd7\xcd\x87\xa3\xd3k6oofU0\x16E^\tt\\?)+f=\r\x02
0(5\x15\t\x15\xdd\xec\xb8\xe2\xfb\xd8\xcb\xd8\xd1\x8b\xd5\x82\xd9\x9a\xf1
\x92\xab\xe8\xa6\xd6\xd0\x8c\xaa\xd2\x94\xcfEFg }D\x14kEmT\x03\x17`bUZJfa
\x11Whu\x05b6}\x02\x10K\x08"B2\xba\xe2\xb9\xe2\xd6\xb9\xff\xc3\xe9\x8a
\x8f\xc1\x8f\xe1\xb8\xa4\x96\xf1\x8f\x81\xb1\x8d\x89\xcc\xd4xvar&gt;7#Vsqyc|
\x08\x11 iz\x14h\x05!\x1e2\'Y\xb7\xcf\xab\xdd\xd5\xcc\x97\x93\xf2\xe7\xc0
\xeb\xff\xe9\xa3\xbf\xa1\xab\x8b\xbb\x9e\x9e\x8c\xa0\xc1\x9bZ//NN\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00'</code></pre>
<h3>Conclusion</h3>
<p>This code is somewhat over-terse; in particular, there&#8217;s no good place to add comprehensive logging or diagnostics to it as written.  I&#8217;d first written a more verbose implementation with plenty of places for those things, but didn&#8217;t end up needing them.  You can download my actual JS solution <a href="http://www.mlsite.net/blog/wp-content/uploads/2011/12/src.js">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3319</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alfonso Cano is Dead</title>
		<link>http://www.mlsite.net/blog/?p=3315</link>
		<comments>http://www.mlsite.net/blog/?p=3315#comments</comments>
		<pubDate>Sat, 05 Nov 2011 09:01:20 +0000</pubDate>
		<dc:creator>MXH</dc:creator>
				<category><![CDATA[Jack Handy]]></category>

		<guid isPermaLink="false">http://www.mlsite.net/blog/?p=3315</guid>
		<description><![CDATA[The FARCs top dog just got killed. Couldn&#8217;t happen to a nicer guy. Peace The funniest thing about this story is the following comment from the link: Five Decades and the fighting&#8217;s still on? With all the killings and maimings &#8230; <a href="http://www.mlsite.net/blog/?p=3315">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The FARCs top dog <a href="http://news.yahoo.com/colombian-rebel-commander-cano-killed-government-024424330.html">just got killed</a>.  Couldn&#8217;t happen to a nicer guy.</p>
<h3>Peace</h3>
<p>The funniest thing about this story is the following comment from the link:</p>
<blockquote><p>Five Decades and the fighting&#8217;s still on? With all the killings and maimings and bombings on all sides aside, can&#8217;t someone or group or neutrals come up with some form of negotiated settlement and permanent solution to this &#8220;cancer&#8221; in the innards of Colombia?</p></blockquote>
<p>You gotta love fools like this.  The solution is victory, and today it is one step closer.</p>
<h3>Prosperity</h3>
<p>Incidentally, it is my view that in 50 years Colombia will be the preeminent country in Latin America.  &#8220;What about Brazil?&#8221; you ask.  Well, in Brazil they elect leftist guerrillas, and in Colombia &#8230; well, they deal with them differently.  This difference will tell over time.</p>
<p>In that connection, I&#8217;d like to mention in passing how delighted I was to see the Colombian FTA finally signed.  Long overdue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlsite.net/blog/?feed=rss2&amp;p=3315</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
