+ "source": [
+ "# Machine interpreter\n",
+ "\n",
+ "## Instructions\n",
+ "\n",
+ "Three registers, `a`, `b`, and `c`. \n",
+ "Program counter, `pc`.\n",
+ "Unlimited memory in numbered addresses, completely separate from program storage.\n",
+ "\n",
+ "Each register and memory location can hold 8-byte integers (Java `long`s).\n",
+ "\n",
+ "Machine carries out the instruction at location `pc`. After it's executed, `pc` increments by 1.\n",
+ "\n",
+ "`jmp` and `jpz` override this normal change in `pc`.\n",
+ "\n",
+ "| Instruction | Description |\n",
+ "|:------------|:------------|\n",
+ "| `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",
+ "| `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",
+ "| `jpz r i` | jump to instruction `i` places forward if<br>register `r` contains zero, otherwise continue to next instruction |\n",
+ "\n",
+ "For `jmp` and `jpz`, `i` is relative to the current instruction. `i` can be negative to jump to earlier places in the program. `i`=1 is a no-op, `i`=0 causes an infinite loop."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "collapsed": true
+ },