+ " sto c 1\n",
+ "\"\"\"\n",
+ "# m = new_machine()\n",
+ "# program_from_listing(program, m)\n",
+ "# run(m)\n",
+ "show_machine(execute(program, initial_state={'a': 17}))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'a: 4, b: 0, c: 12, pc: 9'"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# c holds a * 3\n",
+ "program = \"\"\"\n",
+ " set c 0\n",
+ " cpy a b\n",
+ " # start of main loop\n",
+ "loop: jpz b end\n",
+ " dec b\n",
+ " inc c\n",
+ " inc c\n",
+ " inc c\n",
+ " jmp loop\n",
+ " \n",
+ " # end of program \n",
+ " \n",
+ "end: jmp 1\n",
+ "\"\"\"\n",
+ "# m = new_machine()\n",
+ "# program_from_listing(program, m)\n",
+ "# run(m)\n",
+ "show_machine(execute(program, initial_state={'a': 4}))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'1: 0, a: 0, b: 0, c: 27, pc: 11'"
+ ]
+ },
+ "execution_count": 39,
+ "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": 40,
+ "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": 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: 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": 30,
+ "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": 31,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'1: 0, a: 52, b: 0, c: 0, pc: 48'"
+ ]
+ },
+ "execution_count": 31,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Collatz. a initially holds value, but location 1 is used to store the current value as we're going along.\n",
+ "# Location 2 holds number of steps taken\n",
+ "# Location 3 holds max value reached\n",
+ "program = \"\"\"\n",
+ " sto a 1\n",
+ " \n",
+ " set b 0\n",
+ " \n",
+ " # if a is one, finish, \n",
+ "main: dec a\n",
+ " jpz a end\n",
+ " inc a\n",
+ " \n",
+ " # find parity of a\n",
+ " cpy a c\n",
+ " set b 0\n",
+ "prty: dec c\n",
+ " jpz b odd\n",
+ " dec b\n",
+ " jmp prte\n",
+ "odd: inc b\n",
+ "prte: jpz c 2\n",
+ " jmp prty\n",
+ " \n",
+ " # b == 0 means a even; b == 1 means a odd\n",
+ " jpz b isev\n",
+ "\n",
+ " # c holds a * 3 + 1\n",
+ " cpy a b\n",
+ "mul: jpz b emul\n",
+ " dec b\n",
+ " inc c\n",
+ " inc c\n",
+ " inc c\n",
+ " jmp mul\n",
+ "emul: inc c\n",
+ " cpy c a\n",
+ " jmp fin\n",
+ " \n",
+ " \n",
+ "isev: set c 0\n",
+ " set b 0\n",
+ "hlvl: dec a\n",
+ " jpz b oddh\n",
+ " dec b\n",
+ " inc c\n",
+ " jmp endh\n",
+ "oddh: inc b\n",
+ "endh: jpz a 2\n",
+ " jmp hlvl\n",
+ " cpy c a\n",
+ "\n",
+ "fin: cpy a b\n",
+ " ld c 1\n",
+ "maxc: jpz c this\n",
+ " jpz b othr\n",
+ " dec b\n",
+ " dec c\n",
+ " jmp maxc\n",
+ "this: sto a 1\n",
+ "othr: jmp main\n",
+ " \n",
+ " # end of program \n",
+ " \n",
+ "end: ld a 1\n",
+ " set c 0\n",
+ " sto c 1\n",