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 """) # load the program ae.load_program(program) # indices B[k] k = 1 # input data, initially empty, but each result is added after computation data = [] # instruction to start execution at, initially 0, but subsequently 4 start = labels['init'] # run the program while True: # load the data and run the program ae.load_data(data) ae.run(start) # get the computed result from the output transcript r = (ae.output[-1] if ae.output else None)
ae.load_program([ # initialisation ['SET', 0, 0], ['SET', 1, 1], ['SET', 2, 2], ['SET', 3, 1], # operation 1 ['MUL'], ['LOAD', 2], ['LOAD', 3], ['STORE', 4], ['STORE', 5], ['STORE', 6], # operation 2 ['SUB'], ['LOADZ', 4], ['LOAD', 1], ['STORE', 4], # operation 3 ['ADD'], ['LOADZ', 5], ['LOAD', 1], ['STORE', 5], # operation 4 ['DIV'], ['LOADZ', 4], ['LOADZ', 5], ['STORE', 11], # operation 5 ['DIV'], ['LOADZ', 11], ['LOAD', 2], ['STORE', 11], # operation 6 ['SUB'], ['LOADZ', 13], ['LOADZ', 11], ['STORE', 13], # operation 7 ['SUB'], ['LOAD', 3], ['LOAD', 1], ['STORE', 10], # branch if zero to operation 24 ['BRZ', +66], # operation 8 ['ADD'], ['LOAD', 2], ['LOADZ', 7], ['STORE', 7], # operation 9 ['DIV'], ['LOAD', 6], ['LOAD', 7], ['STORE', 11], # operation 10 ['MUL'], ['LOAD_DATA'], ['LOAD', 11], ['STORE', 12], # operation 11 ['ADD'], ['LOADZ', 12], ['LOADZ', 13], ['STORE', 13], # operation 12 ['SUB'], ['LOADZ', 10], ['LOAD', 1], ['STORE', 10], # branch if zero to operation 24 ['BRZ', +45], # operation 13 ['SUB'], ['LOADZ', 6], ['LOAD', 1], ['STORE', 6], # operation 14 ['ADD'], ['LOAD', 1], ['LOADZ', 7], ['STORE', 7], # operation 15 ['DIV'], ['LOAD', 6], ['LOAD', 7], ['STORE', 8], # operation 16 ['MUL'], ['LOADZ', 8], ['LOADZ', 11], ['STORE', 11], # operation 17 ['SUB'], ['LOADZ', 6], ['LOAD', 1], ['STORE', 6], # operation 18 ['ADD'], ['LOAD', 1], ['LOADZ', 7], ['STORE', 7], # operation 19 ['DIV'], ['LOAD', 6], ['LOAD', 7], ['STORE', 9], # operation 20 ['MUL'], ['LOADZ', 9], ['LOADZ', 11], ['STORE', 11], # operation 21 ['MUL'], ['LOAD_DATA'], ['LOAD', 11], ['STORE', 12], # operation 22 ['ADD'], ['LOADZ', 12], ['LOADZ', 13], ['STORE', 13], # operation 23 ['SUB'], ['LOADZ', 10], ['LOAD', 1], ['STORE', 10], # branch if non-zero to operation 13 ['BRN', -45], # operation 24 ['SUB'], ['LOAD', 0], ['LOADZ', 13], # print ['PRINT'], # operation 25 ['ADD'], ['LOAD', 1], ['LOADZ', 3], ['STORE', 3], # reset working variables ['SET', 6, 0], ['SET', 7, 0], ['SET', 11, 0], # stops overwrite warnings # end ['HALT'], ])
ae.load_program([ # initialisation ['SET', 0, 0], ['SET', 1, 1], ['SET', 2, 2], ['SET', 3, 1], # operation 1 ['MUL'], ['LOAD', 2], ['LOAD', 3], ['STORE', 4], ['STORE', 5], ['STORE', 6], # operation 2 ['SUB'], ['LOAD', 4], ['LOAD', 1], ['STORE', 4], # operation 3 ['ADD'], ['LOAD', 5], ['LOAD', 1], ['STORE', 5], # operation 4 ['DIV'], ['LOAD', 4], ['LOAD', 5], ['STORE', 11], # operation 5 ['DIV'], ['LOAD', 11], ['LOAD', 2], ['STORE', 11], # operation 6 ['SUB'], ['LOAD', 13], ['LOAD', 11], ['STORE', 13], # operation 7 ['SUB'], ['LOAD', 3], ['LOAD', 1], ['STORE', 10], # branch if zero to operation 24 ['BRZ', +66], # operation 8 ['ADD'], ['LOAD', 2], ['LOAD', 7], ['STORE', 7], # operation 9 ['DIV'], ['LOAD', 6], ['LOAD', 7], ['STORE', 11], # operation 10 ['MUL'], ['LOAD_DATA'], ['LOAD', 11], ['STORE', 12], # operation 11 ['ADD'], ['LOAD', 12], ['LOAD', 13], ['STORE', 13], # operation 12 ['SUB'], ['LOAD', 10], ['LOAD', 1], ['STORE', 10], # branch if zero to operation 24 ['BRZ', +45], # operation 13 ['SUB'], ['LOAD', 6], ['LOAD', 1], ['STORE', 6], # operation 14 ['ADD'], ['LOAD', 1], ['LOAD', 7], ['STORE', 7], # operation 15 ['DIV'], ['LOAD', 6], ['LOAD', 7], ['STORE', 8], # operation 16 ['MUL'], ['LOAD', 8], ['LOAD', 11], ['STORE', 11], # operation 17 ['SUB'], ['LOAD', 6], ['LOAD', 1], ['STORE', 6], # operation 18 ['ADD'], ['LOAD', 1], ['LOAD', 7], ['STORE', 7], # operation 19 ['DIV'], ['LOAD', 6], ['LOAD', 7], ['STORE', 9], # operation 20 ['MUL'], ['LOAD', 9], ['LOAD', 11], ['STORE', 11], # operation 21 ['MUL'], ['LOAD_DATA'], ['LOAD', 11], ['STORE', 12], # operation 22 ['ADD'], ['LOAD', 12], ['LOAD', 13], ['STORE', 13], # operation 23 ['SUB'], ['LOAD', 10], ['LOAD', 1], ['STORE', 10], # branch if non-zero to operation 13 ['BRN', -45], # operation 24 ['SUB'], ['LOAD', 0], ['LOAD', 13], # print ['PRINT'], # operation 25 ['ADD'], ['LOAD', 1], ['LOAD', 3], ['STORE', 3], # reset working variables ['SET', 7, 0], ['SET', 13, 0], # end ['HALT'], ])
MUL DATA v11 -> v12 ADD v12. v13. -> v13 SUB v10. 1 -> v10 BRN loop :finish SUB 0 v13. PRINT ADD 1 v3. -> v3 SET v6 <- 0 SET v7 <- 0 SET v11 <- 0 HALT """) # load the program ae.load_program(program) # indices B[k] k = 1 # input data, initially empty, but each result is added after computation data = [] # instruction to start execution at, initially 0, but subsequently 4 start = labels['init'] # run the program while True: # load the data and run the program ae.load_data(data) ae.run(start) # get the computed result from the output transcript r = (ae.output[-1] if ae.output else None)
SUB 10 1 -> 10 BRN loop :finish SUB 0 13 PRINT ADD 1 3 -> 3 SET 7 <- 0 SET 13 <- 0 HALT """) # initialise the engine p = AnalyticalEngine(vars=14, number=Column(digits=10, dp=40), trace=0) # load the program p.load_program(program) # indices B[k] k = 1 # input data, initially empty, but each result is added after computation data = [] # instruction to start execution at, initially 0, but subsequently 4 start = labels['init'] # run the program while True: # load the data and run the program p.load_data(data) p.run(start) # get the computed result from the output transcript r = (p.output[-1] if p.output else None)
n = (12 if len(argv) < 2 else int(argv[1])) # initialise the engine ae = AnalyticalEngine(vars=3, number=int) # load the program ae.load_program([ # initialisation ['SET', 0, n], ['SET', 1, 1], ['SET', 2, 1], # operation 1: v2 = v0 * v2 ['MUL'], ['LOAD', 0], ['LOAD', 2], ['STORE', 2], # operation 2: v0 = v0 - 1 ['SUB'], ['LOAD', 0], ['LOAD', 1], ['STORE', 0], # branch if non-zero to operation 1 ['BRN', -9], # end ['HALT'], ]) # run the program ae.run() # the result is in v2 print("factorial({n}) = {r}".format(n=n, r=ae.v[2]))
n = (40 if len(argv) < 2 else int(argv[1])) # initialise the engine ae = AnalyticalEngine(vars=3, number=Column(digits=50)) # load the program ae.load_program([ # initialisation ['SET', 0, n], ['SET', 1, 1], ['SET', 2, 1], # operation 1: v2 = v0 * v2 ['MUL'], ['LOAD', 0], ['LOAD', 2], ['STORE', 2], # operation 2: v0 = v0 - 1 ['SUB'], ['LOAD', 0], ['LOAD', 1], ['STORE', 0], # branch if non-zero to operation 1 ['BRN', -9], # end ['HALT'], ]) # run the program ae.run() # the result is in v2 print("factorial({n}) = {r}".format(n=n, r=ae.v[2]))
# initialise the engine p = AnalyticalEngine(vars=3, number=Column(digits=50)) # load the program to compute factorial(40) n = 40 p.load_program( [ # initialisation ["SET", 0, n], ["SET", 1, 1], ["SET", 2, 1], # operation 1: v[2] *= v[0] ["MUL"], ["LOAD", 0], ["LOAD", 2], ["STORE", 2], # operation 2: v[2] -= 1 ["SUB"], ["LOAD", 0], ["LOAD", 1], ["STORE", 0], # branch if non-zero to operation 1 ["BRN", -9], # end ["HALT"], ] ) # run the program p.run() # the result is in v[2]