X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=07-interpreter%2Fmachine-code.ipynb;h=c6b62b09b7c78ee0c2711e08796ec6639a839e65;hb=08e0c316beac066037ece028bf687379819fc409;hp=48da543feccf20468c7c036c640544966007074b;hpb=787b6246ddfd5d1eaa228cdd11c537096362eb2f;p=ou-summer-of-code-2017.git diff --git a/07-interpreter/machine-code.ipynb b/07-interpreter/machine-code.ipynb index 48da543..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", @@ -241,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 15, "metadata": { "collapsed": true }, @@ -257,7 +257,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 16, "metadata": { "collapsed": true }, @@ -310,7 +310,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -416,7 +416,8 @@ { "data": { "text/plain": [ - "{'instructions': [(, ['a', 10]),\n", + "{'pc': 6,\n", + " 'instructions': [(, ['a', 10]),\n", " (, ['a']),\n", " (, ['b']),\n", " (, ['b', 1]),\n", @@ -425,7 +426,6 @@ " 1: 10,\n", " 'c': 20,\n", " 'a': 0,\n", - " 'pc': 6,\n", " 'b': 10}" ] }, @@ -457,7 +457,8 @@ { "data": { "text/plain": [ - "{'instructions': [(, ['a', 10]),\n", + "{'pc': 6,\n", + " 'instructions': [(, ['a', 10]),\n", " (, ['a']),\n", " (, ['b']),\n", " (, ['b', 1]),\n", @@ -466,7 +467,6 @@ " 1: 10,\n", " 'c': 20,\n", " 'a': 0,\n", - " 'pc': 6,\n", " 'b': 10}" ] }, @@ -582,7 +582,7 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -591,7 +591,7 @@ "'1: 8, a: 0, b: 1, c: 8, pc: 11'" ] }, - "execution_count": 85, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -619,7 +619,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -628,7 +628,7 @@ "'a: 4, b: 0, c: 12, pc: 9'" ] }, - "execution_count": 43, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -658,47 +658,125 @@ }, { "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": [ + { + "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", + " 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": 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": 82, + "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" } @@ -768,7 +846,9 @@ " \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", @@ -778,7 +858,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -787,7 +867,7 @@ "40" ] }, - "execution_count": 56, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -798,7 +878,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 31, "metadata": { "collapsed": true }, @@ -819,7 +899,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -828,7 +908,7 @@ "52" ] }, - "execution_count": 64, + "execution_count": 32, "metadata": {}, "output_type": "execute_result" } @@ -839,7 +919,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 33, "metadata": {}, "outputs": [ { @@ -848,7 +928,7 @@ "(250504, 937)" ] }, - "execution_count": 80, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -859,7 +939,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -868,7 +948,7 @@ "[]" ] }, - "execution_count": 73, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -885,16 +965,16 @@ }, { "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" } @@ -903,6 +983,85 @@ "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,