From d7db6acc4c4d5f065da2e95a313e376310a990e0 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 19 Jul 2017 00:57:41 +0100 Subject: [PATCH] Added notes for day 7 --- 07-interpreter/machine-code.ipynb | 70 ++++++++++++++++++++++++------- problem-ideas.ipynb | 2 +- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/07-interpreter/machine-code.ipynb b/07-interpreter/machine-code.ipynb index ceee75c..c6b62b0 100644 --- a/07-interpreter/machine-code.ipynb +++ b/07-interpreter/machine-code.ipynb @@ -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", @@ -416,17 +416,17 @@ { "data": { "text/plain": [ - "{1: 10,\n", - " 'a': 0,\n", - " 'c': 20,\n", - " 'pc': 6,\n", - " 'b': 10,\n", + "{'pc': 6,\n", " 'instructions': [(, ['a', 10]),\n", " (, ['a']),\n", " (, ['b']),\n", " (, ['b', 1]),\n", " (, ['a', 2]),\n", - " (, [-4])]}" + " (, [-4])],\n", + " 1: 10,\n", + " 'c': 20,\n", + " 'a': 0,\n", + " 'b': 10}" ] }, "execution_count": 22, @@ -457,17 +457,17 @@ { "data": { "text/plain": [ - "{1: 10,\n", - " 'a': 0,\n", - " 'c': 20,\n", - " 'pc': 6,\n", - " 'b': 10,\n", + "{'pc': 6,\n", " 'instructions': [(, ['a', 10]),\n", " (, ['a']),\n", " (, ['b']),\n", " (, ['b', 1]),\n", " (, ['a', 2]),\n", - " (, [-4])]}" + " (, [-4])],\n", + " 1: 10,\n", + " 'c': 20,\n", + " 'a': 0,\n", + " 'b': 10}" ] }, "execution_count": 23, @@ -658,7 +658,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -667,7 +667,45 @@ "'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" } @@ -675,6 +713,8 @@ "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", diff --git a/problem-ideas.ipynb b/problem-ideas.ipynb index 77cecd0..66012c7 100644 --- a/problem-ideas.ipynb +++ b/problem-ideas.ipynb @@ -313,7 +313,7 @@ "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", -- 2.34.1