コード例 #1
0
(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
  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
""")
コード例 #2
0
(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
  DIV v6 v7 -> v11
  MUL DATA v11 -> v12
  ADD v12. v13. -> v13
  SUB v10. 1 -> v10
  BRZ finish
  :loop
  SUB v6. 1 -> v6
  ADD 1 v7. -> v7
  DIV v6 v7 -> v8
  MUL v8. v11. -> v11
  SUB v6. 1 -> v6
  ADD 1 v7. -> v7
  DIV v6 v7 -> v9
  MUL v9. v11. -> v11
  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
""")
コード例 #3
0
(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
  DIV v6 v7 -> v11
  MUL DATA v11 -> v12
  ADD v12. v13. -> v13
  SUB v10. 1 -> v10
  BRZ finish
  :loop
  SUB v6. 1 -> v6
  ADD 1 v7. -> v7
  DIV v6 v7 -> v8
  MUL v8. v11. -> v11
  SUB v6. 1 -> v6
  ADD 1 v7. -> v7
  DIV v6 v7 -> v9
  MUL v9. v11. -> v11
  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
""")
コード例 #4
0
# 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
  # test against previous value
  SUB v3 v4.
  BRN loop
  HALT
""".format(n=n))

# load the program
ae.load_program(program)

# run the program
ae.run()
コード例 #5
0
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))

# load the program to compute factorial(n)
ae.load_program(program)

# run the program
ae.run()

# the result is in v2
コード例 #6
0
# 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
""".format(n=n))

# load the program to compute factorial(n)
ae.load_program(program)

# run the program
ae.run()

# the result is in v2
コード例 #7
0
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?
  BRZ exit
  # increment a and b
  ADD v2. v4 -> v2
  ADD v3. v5 -> v3
  BRA repeat
  :exit
  HALT
""")

# load the program
ae.load_program(program)

# run the program
コード例 #8
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)
  ADD v3 1 -> v10
  DIV 4 v10. -> v9
  # - 2/(8k + 4)
  ADD v3 4 -> v10
  DIV 2 v10. -> v10
  SUB v9. v10. -> v9
  # - 1/(8k + 5)
  ADD v3 5 -> v10
  DIV 1 v10. -> v10
  SUB v9. v10. -> v9
  # - 1/(8k + 6)
  ADD v3 6 -> v10
  DIV 1 v10. -> v10
  SUB v9. v10. -> v9
  # * (1/16)^k
  MUL v9. v7 -> v9
  BRZ exit
  # add in the term
  ADD v0. v9. -> v0
  # increase k
  ADD v3. 8 -> v3
  MUL v7. 16 -> v7
  BRA repeat
  :exit
  HALT
""")
コード例 #9
0
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))

# load the program to compute factorial(n)
ae.load_program(program)

# run the program
ae.run()

# the result is in v2
コード例 #10
0
(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
  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
""")