(program, labels) = assemble(""" :init SET 0 <- 0 SET 1 <- 1 SET 2 <- 2 SET 3 <- 1 :start MUL 2 3 -> 4 5 6 SUB 4 1 -> 4 ADD 5 1 -> 5 DIV 4 5 -> 11 DIV 11 2 -> 11 SUB 13 11 -> 13 SUB 3 1 -> 10 BRZ finish ADD 2 7 -> 7 DIV 6 7 -> 11 MUL DATA 11 -> 12 ADD 12 13 -> 13 SUB 10 1 -> 10 BRZ finish :loop SUB 6 1 -> 6 ADD 1 7 -> 7 DIV 6 7 -> 8 MUL 8 11 -> 11 SUB 6 1 -> 6 ADD 1 7 -> 7 DIV 6 7 -> 9 MUL 9 11 -> 11 MUL DATA 11 -> 12 ADD 12 13 -> 13 SUB 10 1 -> 10 BRN loop :finish SUB 0 13 PRINT ADD 1 3 -> 3 SET 7 <- 0 SET 13 <- 0 HALT """)
# https://enigmaticcode.wordpress.com/2015/10/21/running-the-first-program-part-3/ # Program 6 - factorial3.py from analytical_engine import AnalyticalEngine, Column, assemble import sys n = (40 if len(sys.argv) < 2 else int(sys.argv[1])) (program, _) = assemble(""" :init SET 0 <- {n} SET 1 <- 1 SET 2 <- 1 :loop # operation 1: v[2] *= v[0] MUL 0 2 -> 2 # operation 2: v[2] -= 1 SUB 0 1 -> 0 # branch if non-zero to operation 1 BRN loop # end HALT """.format(n=n)) # initialise the engine p = AnalyticalEngine(vars=3, number=Column(digits=50), trace=1) # load the program to compute factorial(n) p.load_program(program) # run the program