Пример #1
0
#!/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
Пример #2
0
#!/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
#!/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
Пример #4
0
#!/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],
Пример #5
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],