Added notes for day 7
authorNeil Smith <neil.git@njae.me.uk>
Tue, 18 Jul 2017 23:57:41 +0000 (00:57 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Tue, 18 Jul 2017 23:57:41 +0000 (00:57 +0100)
07-interpreter/machine-code.ipynb
problem-ideas.ipynb

index ceee75c4f8937e76d1a48f8518ab994374543cf5..c6b62b09b7c78ee0c2711e08796ec6639a839e65 100644 (file)
@@ -23,7 +23,7 @@
     "| `inc r`     | increment contents of register `r` |\n",
     "| `dec r`     | decrement contents of register `r` |\n",
     "| `set r i`   | set contents of register `r` to literal value `i` |\n",
-    "| `cpy r s`   | set contents of register `r` into register `s` | \n",
+    "| `cpy r s`   | copy contents of register `r` into register `s` | \n",
     "| `sto r m`   | store contents of register `r` into memory location `m` |\n",
     "| `ld r m`    | load contents of memory location `m` into register `r` |\n",
     "| `jmp i`     | jump to instruction `i` places forward |\n",
     {
      "data": {
       "text/plain": [
-       "{1: 10,\n",
-       " 'a': 0,\n",
-       " 'c': 20,\n",
-       " 'pc': 6,\n",
-       " 'b': 10,\n",
+       "{'pc': 6,\n",
        " 'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
        "  (<function __main__.dec>, ['a']),\n",
        "  (<function __main__.inc>, ['b']),\n",
        "  (<function __main__.sto>, ['b', 1]),\n",
        "  (<function __main__.jpz>, ['a', 2]),\n",
-       "  (<function __main__.jmp>, [-4])]}"
+       "  (<function __main__.jmp>, [-4])],\n",
+       " 1: 10,\n",
+       " 'c': 20,\n",
+       " 'a': 0,\n",
+       " 'b': 10}"
       ]
      },
      "execution_count": 22,
     {
      "data": {
       "text/plain": [
-       "{1: 10,\n",
-       " 'a': 0,\n",
-       " 'c': 20,\n",
-       " 'pc': 6,\n",
-       " 'b': 10,\n",
+       "{'pc': 6,\n",
        " 'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
        "  (<function __main__.dec>, ['a']),\n",
        "  (<function __main__.inc>, ['b']),\n",
        "  (<function __main__.sto>, ['b', 1]),\n",
        "  (<function __main__.jpz>, ['a', 2]),\n",
-       "  (<function __main__.jmp>, [-4])]}"
+       "  (<function __main__.jmp>, [-4])],\n",
+       " 1: 10,\n",
+       " 'c': 20,\n",
+       " 'a': 0,\n",
+       " 'b': 10}"
       ]
      },
      "execution_count": 23,
   },
   {
    "cell_type": "code",
-   "execution_count": 48,
+   "execution_count": 28,
    "metadata": {},
    "outputs": [
     {
        "'1: 0, a: 0, b: 0, c: 27, pc: 11'"
       ]
      },
-     "execution_count": 48,
+     "execution_count": 28,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# c holds a * b\n",
+    "program = \"\"\"\n",
+    "      set c 0\n",
+    "      sto a 1\n",
+    "loop: jpz b end      \n",
+    "      dec b\n",
+    "      ld a 1\n",
+    "smul: jpz a emul\n",
+    "      inc c\n",
+    "      dec a\n",
+    "      jmp smul\n",
+    "emul: jmp loop \n",
+    "      \n",
+    "end:  sto a 1\n",
+    "\"\"\"\n",
+    "m = new_machine()\n",
+    "program_from_listing(program, m)\n",
+    "run(m)\n",
+    "show_machine(execute(program, initial_state={'a': 9, 'b': 3}))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 29,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'1: 0, a: 0, b: 0, c: 10, pc: 13'"
+      ]
+     },
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
    "source": [
     "# c holds a * b\n",
     "program = \"\"\"\n",
+    "      set a 2\n",
+    "      set b 5\n",
     "      set c 0\n",
     "      sto a 1\n",
     "loop: jpz b end      \n",
index 77cecd0b48be2325d3395ef41570eb778d0ce68b..66012c785aa37bdd8d1afe596ce3f08e474f8636 100644 (file)
     "4. [Ghost leg, follow and pack](04-08-amidakuji): Clojure\n",
     "5. [Display board](05-display-board): JavaScript/Node.js\n",
     "6. [Tour shapes](06-tour-shapes): Lua\n",
-    "7. [Virtual machine](07-interpreter): Haskell\n",
+    "7. [Virtual machine](07-interpreter): Haskell (Advent 16 day 23)\n",
     "8. [Word chains](08-word-chains): Prolog\n",
     "9. [Resolving the bill](09-resolving-the-bill): Sense\n",
     "10. [Word search](10-word-search): Python x\n",