Finalised day 7
authorNeil Smith <neil.git@njae.me.uk>
Mon, 26 Jun 2017 21:28:20 +0000 (22:28 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 26 Jun 2017 21:28:20 +0000 (22:28 +0100)
07-interpreter/07-program.txt [new file with mode: 0644]
07-interpreter/instructions.html [new file with mode: 0644]
07-interpreter/instructions.md [new file with mode: 0644]
07-interpreter/machine-code.ipynb

diff --git a/07-interpreter/07-program.txt b/07-interpreter/07-program.txt
new file mode 100644 (file)
index 0000000..f12d425
--- /dev/null
@@ -0,0 +1,48 @@
+sto a 1
+set b 0
+dec a
+jpz a 42
+inc a
+cpy a c
+set b 0
+dec c
+jpz b 3
+dec b
+jmp 2
+inc b
+jpz c 2
+jmp -6
+jpz b 11
+cpy a b
+jpz b 6
+dec b
+inc c
+inc c
+inc c
+jmp -5
+inc c
+cpy c a
+jmp 12
+set c 0
+set b 0
+dec a
+jpz b 4
+dec b
+inc c
+jmp 2
+inc b
+jpz a 2
+jmp -7
+cpy c a
+cpy a b
+ld c 1
+jpz c 5
+jpz b 5
+dec b
+dec c
+jmp -4
+sto a 1
+jmp -42
+ld a 1
+set c 0
+sto c 1
\ No newline at end of file
diff --git a/07-interpreter/instructions.html b/07-interpreter/instructions.html
new file mode 100644 (file)
index 0000000..5abe593
--- /dev/null
@@ -0,0 +1,46 @@
+<table style="width:39%;">
+<colgroup>
+<col width="19%" />
+<col width="19%" />
+</colgroup>
+<thead>
+<tr class="header">
+<th align="left">Instruction</th>
+<th align="left">Description</th>
+</tr>
+</thead>
+<tbody>
+<tr class="odd">
+<td align="left"><code>inc r</code></td>
+<td align="left">increment contents of register <code>r</code></td>
+</tr>
+<tr class="even">
+<td align="left"><code>dec r</code></td>
+<td align="left">decrement contents of register <code>r</code></td>
+</tr>
+<tr class="odd">
+<td align="left"><code>set r i</code></td>
+<td align="left">set contents of register <code>r</code> to literal value <code>i</code></td>
+</tr>
+<tr class="even">
+<td align="left"><code>cpy r s</code></td>
+<td align="left">set contents of register <code>r</code> into register <code>s</code></td>
+</tr>
+<tr class="odd">
+<td align="left"><code>sto r m</code></td>
+<td align="left">store contents of register <code>r</code> into memory location <code>m</code></td>
+</tr>
+<tr class="even">
+<td align="left"><code>ld r m</code></td>
+<td align="left">load contents of memory location <code>m</code> into register <code>r</code></td>
+</tr>
+<tr class="odd">
+<td align="left"><code>jmp i</code></td>
+<td align="left">jump to instruction <code>i</code> places forward</td>
+</tr>
+<tr class="even">
+<td align="left"><code>jpz r i</code></td>
+<td align="left">jump to instruction <code>i</code> places forward if<br>register <code>r</code> contains zero, otherwise continue to next instruction</td>
+</tr>
+</tbody>
+</table>
diff --git a/07-interpreter/instructions.md b/07-interpreter/instructions.md
new file mode 100644 (file)
index 0000000..a2464ca
--- /dev/null
@@ -0,0 +1,11 @@
+| Instruction | Description |
+|:------------|:------------|
+| `inc r`     | increment contents of register `r` |
+| `dec r`     | decrement contents of register `r` |
+| `set r i`   | set contents of register `r` to literal value `i` |
+| `cpy r s`   | set contents of register `r` into register `s` | 
+| `sto r m`   | store contents of register `r` into memory location `m` |
+| `ld r m`    | load contents of memory location `m` into register `r` |
+| `jmp i`     | jump to instruction `i` places forward |
+| `jpz r i`   | jump to instruction `i` places forward if<br>register `r` contains zero, otherwise continue to next instruction |
+
index 48da543feccf20468c7c036c640544966007074b..ceee75c4f8937e76d1a48f8518ab994374543cf5 100644 (file)
   },
   {
    "cell_type": "code",
-   "execution_count": 42,
+   "execution_count": 15,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 39,
+   "execution_count": 16,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 40,
+   "execution_count": 18,
    "metadata": {},
    "outputs": [
     {
     {
      "data": {
       "text/plain": [
-       "{'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
+       "{1: 10,\n",
+       " 'a': 0,\n",
+       " 'c': 20,\n",
+       " 'pc': 6,\n",
+       " 'b': 10,\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])],\n",
-       " 1: 10,\n",
-       " 'c': 20,\n",
-       " 'a': 0,\n",
-       " 'pc': 6,\n",
-       " 'b': 10}"
+       "  (<function __main__.jmp>, [-4])]}"
       ]
      },
      "execution_count": 22,
     {
      "data": {
       "text/plain": [
-       "{'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
+       "{1: 10,\n",
+       " 'a': 0,\n",
+       " 'c': 20,\n",
+       " 'pc': 6,\n",
+       " 'b': 10,\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])],\n",
-       " 1: 10,\n",
-       " 'c': 20,\n",
-       " 'a': 0,\n",
-       " 'pc': 6,\n",
-       " 'b': 10}"
+       "  (<function __main__.jmp>, [-4])]}"
       ]
      },
      "execution_count": 23,
   },
   {
    "cell_type": "code",
-   "execution_count": 85,
+   "execution_count": 26,
    "metadata": {},
    "outputs": [
     {
        "'1: 8, a: 0, b: 1, c: 8, pc: 11'"
       ]
      },
-     "execution_count": 85,
+     "execution_count": 26,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 43,
+   "execution_count": 27,
    "metadata": {},
    "outputs": [
     {
        "'a: 4, b: 0, c: 12, pc: 9'"
       ]
      },
-     "execution_count": 43,
+     "execution_count": 27,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": null,
-   "metadata": {
-    "collapsed": true
-   },
-   "outputs": [],
+   "execution_count": 48,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'1: 0, a: 0, b: 0, c: 27, pc: 11'"
+      ]
+     },
+     "execution_count": 48,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
    "source": [
-    "# c holds a!\n",
+    "# c holds a * b\n",
     "program = \"\"\"\n",
-    "??????????????\n",
     "      set c 0\n",
-    "      cpy a b\n",
-    "      # start of main loop\n",
-    "loop: jpz b end\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",
-    "      inc c\n",
-    "      inc c\n",
-    "      jmp loop\n",
+    "      dec a\n",
+    "      jmp smul\n",
+    "emul: jmp loop \n",
     "      \n",
-    "      # end of program \n",
-    "      \n",
-    "end:  jmp 1\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': 4}))"
+    "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": 82,
+   "execution_count": 49,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "set c 0\n",
+      "sto a 1\n",
+      "jpz b 8\n",
+      "dec b\n",
+      "ld a 1\n",
+      "jpz a 4\n",
+      "inc c\n",
+      "dec a\n",
+      "jmp -3\n",
+      "jmp -7\n",
+      "sto a 1\n"
+     ]
+    }
+   ],
+   "source": [
+    "labelled_instructions = [i.strip() for i in program.split('\\n') \n",
+    "                             if i.strip() \n",
+    "                             if not i.strip().startswith('#')]\n",
+    "instructions = replace_labels(labelled_instructions)\n",
+    "print('\\n'.join(instructions))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 51,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'1: 52, a: 0, b: 0, c: 0, pc: 46'"
+       "'1: 0, a: 52, b: 0, c: 0, pc: 48'"
       ]
      },
-     "execution_count": 82,
+     "execution_count": 51,
      "metadata": {},
      "output_type": "execute_result"
     }
     "      \n",
     "      # end of program \n",
     "      \n",
-    "end:  set c 0\n",
+    "end:  ld a 1\n",
+    "      set c 0\n",
+    "      sto c 1\n",
     "\"\"\"\n",
     "# m = new_machine()\n",
     "# program_from_listing(program, m)\n",
   },
   {
    "cell_type": "code",
-   "execution_count": 56,
+   "execution_count": 30,
    "metadata": {},
    "outputs": [
     {
        "40"
       ]
      },
-     "execution_count": 56,
+     "execution_count": 30,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 63,
+   "execution_count": 31,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 64,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [
     {
        "52"
       ]
      },
-     "execution_count": 64,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 80,
+   "execution_count": 33,
    "metadata": {},
    "outputs": [
     {
        "(250504, 937)"
       ]
      },
-     "execution_count": 80,
+     "execution_count": 33,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 73,
+   "execution_count": 34,
    "metadata": {},
    "outputs": [
     {
        "[]"
       ]
      },
-     "execution_count": 73,
+     "execution_count": 34,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 83,
+   "execution_count": 52,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'1: 250504, a: 0, b: 0, c: 0, pc: 46'"
+       "'1: 0, a: 250504, b: 0, c: 0, pc: 48'"
       ]
      },
-     "execution_count": 83,
+     "execution_count": 52,
      "metadata": {},
      "output_type": "execute_result"
     }
     "show_machine(execute(program, initial_state={'a': 937}))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 53,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "sto a 1\n",
+      "set b 0\n",
+      "dec a\n",
+      "jpz a 42\n",
+      "inc a\n",
+      "cpy a c\n",
+      "set b 0\n",
+      "dec c\n",
+      "jpz b 3\n",
+      "dec b\n",
+      "jmp 2\n",
+      "inc b\n",
+      "jpz c 2\n",
+      "jmp -6\n",
+      "jpz b 11\n",
+      "cpy a b\n",
+      "jpz b 6\n",
+      "dec b\n",
+      "inc c\n",
+      "inc c\n",
+      "inc c\n",
+      "jmp -5\n",
+      "inc c\n",
+      "cpy c a\n",
+      "jmp 12\n",
+      "set c 0\n",
+      "set b 0\n",
+      "dec a\n",
+      "jpz b 4\n",
+      "dec b\n",
+      "inc c\n",
+      "jmp 2\n",
+      "inc b\n",
+      "jpz a 2\n",
+      "jmp -7\n",
+      "cpy c a\n",
+      "cpy a b\n",
+      "ld c 1\n",
+      "jpz c 5\n",
+      "jpz b 5\n",
+      "dec b\n",
+      "dec c\n",
+      "jmp -4\n",
+      "sto a 1\n",
+      "jmp -42\n",
+      "ld a 1\n",
+      "set c 0\n",
+      "sto c 1\n"
+     ]
+    },
+    {
+     "data": {
+      "text/plain": [
+       "342"
+      ]
+     },
+     "execution_count": 53,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "labelled_instructions = [i.strip() for i in program.split('\\n') \n",
+    "                             if i.strip() \n",
+    "                             if not i.strip().startswith('#')]\n",
+    "instructions = replace_labels(labelled_instructions)\n",
+    "print('\\n'.join(instructions))\n",
+    "open('07-program.txt', 'w').write('\\n'.join(instructions))"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,