#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # alternative implementation of ada5.py using the LOADZ opcode where appropriate from analytical_engine import AnalyticalEngine from fractions import Fraction from enigma import raw_input, printf # initialise the engine ae = AnalyticalEngine(vars=14, number=Fraction, trace=0, warn=1) # load the program 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],
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/14/running-the-first-program-part-2/ # Program 4 - ada5.py from analytical_engine import AnalyticalEngine from fractions import Fraction from enigma import raw_input, printf # initialise the engine ae = AnalyticalEngine(vars=14, number=Fraction, trace=0) # load the program 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'],
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/21/running-the-first-program-part-3/ # Program 6 - alternative implementation of factorial3.py using destructive reads from analytical_engine import AnalyticalEngine, Column from sys import argv n = (40 if len(argv) < 2 else int(argv[1])) # initialise the engine ae = AnalyticalEngine(vars=3, number=Column(digits=50), warn=1, trace=0) (program, _) = ae.assemble(""" :init SET v0 <- {n} SET v1 <- 1 SET v2 <- 1 :loop # operation 1: v2 = v0 * v2 MUL v0 v2. -> v2 # operation 2: v0 = v0 - 1 SUB v0. 1 -> v0 # branch if non-zero to operation 1 BRN loop # end HALT """.format(n=n))
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/21/running-the-first-program-part-3/ # Program 7 - ada6z.py from analytical_engine import AnalyticalEngine from enigma import raw_input, printf # initialise the engine ae = AnalyticalEngine(vars=14, warn=1, trace=0) # assemble the program (program, labels) = ae.assemble(""" :init SET v0 <- 0 SET v1 <- 1 SET v2 <- 2 SET v3 <- 1 :start MUL v2 v3 -> v4 v5 v6 SUB v4. 1 -> v4 ADD v5. 1 -> v5 DIV v4. v5. -> v11 DIV v11. 2 -> v11 SUB v13. v11. -> v13 SUB v3 1 -> v10 BRZ finish ADD v2 v7. -> v7
from analytical_engine import AnalyticalEngine from sys import argv n = (2 if len(argv) < 2 else argv[1]) # we use 6 variables in the AE: # v0 = 0 (constant) # v1 = 0.5 (constant, we multiply by 0.5 instead of dividing by 2) # v2 = n (input parameter) # v3 = x (output parameter, approximation to square root) # v4 = x' (previous value of x) # v5 = t (temporary variable) # initialise the engine ae = AnalyticalEngine(vars=6, warn=1, trace=0) (program, _) = ae.assemble(""" :init SET v0 <- 0 SET v1 <- 1/2 SET v2 <- {n} # initial guess: x = n / 2 MUL v1 v2 -> v3 :loop # save current guess ADD v3 0 -> v4 # x = (n / x + x) / 2 DIV v2 v3 -> v5 ADD v5. v3. -> v5 MUL v5. v1 -> v3
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 """) # 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)
# # pi/2 = 1 + 1/3 + (1/3)(2/5) + (1/3)(2/5)(3/7) + ... # # we use 6 variables in the AE: # # v0 = x (approximation to pi) # v1 = t (incremental term) # v2 = a (numerator) # v3 = b (denominator) # v4 = 1 (constant, numerator increment) # v5 = 2 (constant, denominator increment) from analytical_engine import AnalyticalEngine # initialise the engine ae = AnalyticalEngine(vars=6, warn=1, trace=0) (program, _) = ae.assemble(""" :init SET v1 <- 2 SET v2 <- 1 SET v3 <- 3 SET v4 <- 1 SET v5 <- 2 :repeat # add in the current term ADD v0. v1 -> v0 # calculate the next term: t = t * a / b MUL v1. v2 -> v1 DIV v1. v3 -> v1 # have we run out of accuracy?
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 p.run() # the result is in v[2] print("factorial({n}) = {f}".format(n=n, f=p.v[2]))
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/14/running-the-first-program-part-2/ # Program 5 - factorial2.py from analytical_engine import AnalyticalEngine, Column # compute factorial(n) from sys import argv 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],
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/14/running-the-first-program-part-2/ # Program 3 - factoria1.py from analytical_engine import AnalyticalEngine # compute factorial(n) from sys import argv 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],
# v4 = 4 # v5 = 5 # v6 = 6 # v8 = 8 # v16 = 1/16 (* 1/16 is faster than / 16) # # v0 = x (approximation to pi) # v3 = 8k # v7 = (1/16)^k # v9 = t (incremental term) # v10 = (temporary variable) from analytical_engine import AnalyticalEngine # initialise the engine ae = AnalyticalEngine(vars=17, warn=1, trace=0) (program, _) = ae.assemble(""" :init # constants SET v1 <- 1 SET v2 <- 2 SET v4 <- 4 SET v5 <- 5 SET v6 <- 6 SET v8 <- 8 SET v16 <- 1/16 SET v3 <- 0 SET v7 <- 1 :repeat # 4/(8k + 1)
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/21/running-the-first-program-part-3/ # Program 6 - alternative implementation of factorial3.py using destructive reads from analytical_engine import AnalyticalEngine, Column from sys import argv n = (40 if len(argv) < 2 else int(argv[1])) # initialise the engine ae = AnalyticalEngine(vars=3, number=Column(digits=50), warn=1, trace=0) (program, _) = ae.assemble(""" :init SET v0 <- {n} SET v1 <- 1 SET v2 <- 1 :loop # operation 1: v2 = v0 * v2 MUL v0 v2. -> v2 # operation 2: v0 = v0 - 1 SUB v0. 1 -> v0 # branch if non-zero to operation 1 BRN loop # end HALT
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/21/running-the-first-program-part-3/ # Program 7 - ada6.py from analytical_engine import AnalyticalEngine from enigma import raw_input, printf # initialise the engine ae = AnalyticalEngine(vars=14, trace=0) # assemble the program (program, labels) = ae.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
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/21/running-the-first-program-part-3/ # Program 6 - factorial3.py from analytical_engine import AnalyticalEngine, Column # compute factorial(n) from sys import argv n = (40 if len(argv) < 2 else int(argv[1])) # initialise the engine ae = AnalyticalEngine(vars=3, number=Column(digits=50), trace=1) (program, _) = ae.assemble(""" :init SET 0 <- {n} SET 1 <- 1 SET 2 <- 1 :loop # operation 1: v2 = v0 * v2 MUL 0 2 -> 2 # operation 2: v0 = v0 - 1 SUB 0 1 -> 0 # branch if non-zero to operation 1 BRN loop # end HALT
#!/usr/bin/env python -t # -*- mode: Python; py-indent-offset: 2; -*- from __future__ import print_function # https://enigmaticcode.wordpress.com/2015/10/14/running-the-first-program-part-2/ # Program 5 - factorial2.py from analytical_engine import AnalyticalEngine, Column # 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],