"| `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",
{
"data": {
"text/plain": [
- "{1: 10,\n",
- " 'a': 0,\n",
- " 'c': 20,\n",
- " 'pc': 6,\n",
- " 'b': 10,\n",
+ "{'pc': 6,\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])]}"
+ " (<function __main__.jmp>, [-4])],\n",
+ " 1: 10,\n",
+ " 'c': 20,\n",
+ " 'a': 0,\n",
+ " 'b': 10}"
]
},
"execution_count": 22,
{
"data": {
"text/plain": [
- "{1: 10,\n",
- " 'a': 0,\n",
- " 'c': 20,\n",
- " 'pc': 6,\n",
- " 'b': 10,\n",
+ "{'pc': 6,\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])]}"
+ " (<function __main__.jmp>, [-4])],\n",
+ " 1: 10,\n",
+ " 'c': 20,\n",
+ " 'a': 0,\n",
+ " 'b': 10}"
]
},
"execution_count": 23,
},
{
"cell_type": "code",
- "execution_count": 48,
+ "execution_count": 28,
"metadata": {},
"outputs": [
{
"'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"
}
"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",
"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",