Removing files from data analysis directory
[ou-summer-of-code-2017.git] / 07-interpreter / machine-code.ipynb
index 48da543feccf20468c7c036c640544966007074b..2bb22230c55db64aa3cb6e4069836942bb8a00ee 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",
   },
   {
    "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",
+       " '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}"
+       " 'pc': 6}"
       ]
      },
      "execution_count": 22,
     {
      "data": {
       "text/plain": [
-       "{'instructions': [(<function __main__.set_literal>, ['a', 10]),\n",
+       "{1: 10,\n",
+       " 'a': 0,\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}"
+       " 'pc': 6}"
       ]
      },
      "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": 28,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'1: 0, a: 0, b: 0, c: 27, pc: 11'"
+      ]
+     },
+     "execution_count": 28,
+     "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",
+    "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": [
+    {
+     "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') if i.strip() if not i.strip().startswith('#')]\n",
+    "instructions = replace_labels(labelled_instructions)\n",
+    "print('\\n'.join(instructions))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 30,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'1: 0, a: 0, b: 0, c: 10, pc: 13'"
+      ]
+     },
+     "execution_count": 30,
+     "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",
+    "      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:  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": 31,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "set a 2\n",
+      "set b 5\n",
+      "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": 32,
    "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": 32,
      "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": 33,
    "metadata": {},
    "outputs": [
     {
        "40"
       ]
      },
-     "execution_count": 56,
+     "execution_count": 33,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 63,
+   "execution_count": 34,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 64,
+   "execution_count": 35,
    "metadata": {},
    "outputs": [
     {
        "52"
       ]
      },
-     "execution_count": 64,
+     "execution_count": 35,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 80,
+   "execution_count": 36,
    "metadata": {},
    "outputs": [
     {
        "(250504, 937)"
       ]
      },
-     "execution_count": 80,
+     "execution_count": 36,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 73,
+   "execution_count": 41,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "[]"
+       "(52, 9)"
+      ]
+     },
+     "execution_count": 41,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "max([(max_collatz(i), i) for i in range(1, 10)])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 37,
+   "metadata": {
+    "scrolled": true
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(1, 0, 1),\n",
+       " (2, 0, 2),\n",
+       " (3, 0, 16),\n",
+       " (4, 0, 4),\n",
+       " (5, 0, 16),\n",
+       " (6, 0, 16),\n",
+       " (7, 0, 52),\n",
+       " (8, 0, 8),\n",
+       " (9, 0, 52),\n",
+       " (10, 0, 16),\n",
+       " (11, 0, 52),\n",
+       " (12, 0, 16),\n",
+       " (13, 0, 40),\n",
+       " (14, 0, 52),\n",
+       " (15, 0, 160),\n",
+       " (16, 0, 16),\n",
+       " (17, 0, 52),\n",
+       " (18, 0, 52),\n",
+       " (19, 0, 88),\n",
+       " (20, 0, 20),\n",
+       " (21, 0, 64),\n",
+       " (22, 0, 52),\n",
+       " (23, 0, 160),\n",
+       " (24, 0, 24),\n",
+       " (25, 0, 88),\n",
+       " (26, 0, 40),\n",
+       " (27, 0, 9232),\n",
+       " (28, 0, 52),\n",
+       " (29, 0, 88),\n",
+       " (30, 0, 160),\n",
+       " (31, 0, 9232),\n",
+       " (32, 0, 32),\n",
+       " (33, 0, 100),\n",
+       " (34, 0, 52),\n",
+       " (35, 0, 160),\n",
+       " (36, 0, 52),\n",
+       " (37, 0, 112),\n",
+       " (38, 0, 88),\n",
+       " (39, 0, 304),\n",
+       " (40, 0, 40),\n",
+       " (41, 0, 9232),\n",
+       " (42, 0, 64),\n",
+       " (43, 0, 196),\n",
+       " (44, 0, 52),\n",
+       " (45, 0, 136),\n",
+       " (46, 0, 160),\n",
+       " (47, 0, 9232),\n",
+       " (48, 0, 48),\n",
+       " (49, 0, 148),\n",
+       " (50, 0, 88),\n",
+       " (51, 0, 232),\n",
+       " (52, 0, 52),\n",
+       " (53, 0, 160),\n",
+       " (54, 0, 9232),\n",
+       " (55, 0, 9232),\n",
+       " (56, 0, 56),\n",
+       " (57, 0, 196),\n",
+       " (58, 0, 88),\n",
+       " (59, 0, 304),\n",
+       " (60, 0, 160),\n",
+       " (61, 0, 184),\n",
+       " (62, 0, 9232),\n",
+       " (63, 0, 9232),\n",
+       " (64, 0, 64),\n",
+       " (65, 0, 196),\n",
+       " (66, 0, 100),\n",
+       " (67, 0, 304),\n",
+       " (68, 0, 68),\n",
+       " (69, 0, 208),\n",
+       " (70, 0, 160),\n",
+       " (71, 0, 9232),\n",
+       " (72, 0, 72),\n",
+       " (73, 0, 9232),\n",
+       " (74, 0, 112),\n",
+       " (75, 0, 340),\n",
+       " (76, 0, 88),\n",
+       " (77, 0, 232),\n",
+       " (78, 0, 304),\n",
+       " (79, 0, 808),\n",
+       " (80, 0, 80),\n",
+       " (81, 0, 244),\n",
+       " (82, 0, 9232),\n",
+       " (83, 0, 9232),\n",
+       " (84, 0, 84),\n",
+       " (85, 0, 256),\n",
+       " (86, 0, 196),\n",
+       " (87, 0, 592),\n",
+       " (88, 0, 88),\n",
+       " (89, 0, 304),\n",
+       " (90, 0, 136),\n",
+       " (91, 0, 9232),\n",
+       " (92, 0, 160),\n",
+       " (93, 0, 280),\n",
+       " (94, 0, 9232),\n",
+       " (95, 0, 9232),\n",
+       " (96, 0, 96),\n",
+       " (97, 0, 9232),\n",
+       " (98, 0, 148),\n",
+       " (99, 0, 448)]"
       ]
      },
-     "execution_count": 73,
+     "execution_count": 37,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 83,
+   "execution_count": 38,
    "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": 38,
      "metadata": {},
      "output_type": "execute_result"
     }
     "show_machine(execute(program, initial_state={'a': 937}))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 40,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "1 loop, best of 3: 38.6 s per loop\n"
+     ]
+    }
+   ],
+   "source": [
+    "%%timeit\n",
+    "show_machine(execute(program, initial_state={'a': 937}))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 39,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "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,