MIX 汇编语言模拟器
项目描述
混合
一个MIX汇编语言模拟器。
安装
确保你有一个 C++ 11 编译器并执行:
pip install mixal
对于 MacOS 用户,您可能需要使用:
MACOSX_DEPLOYMENT_TARGET=10.9 pip install mixal
样本
以下是一段查找最大值的示例代码。由于代码是用 C++ 编写的,因此函数和属性的名称中有大写字母。
import random
import mixal
# Initialize an environment
computer = mixal.Computer()
# The location for register J
end_point = 3500
# Load the assembly codes.
# Note that the location for register J is set to HLT
# to make sure the codes halt eventually
computer.loadCodes([
'X EQU 1000',
' ORIG 3000',
'MAXIMUM STJ EXIT',
'INIT ENT3 0,1',
' JMP CHANGEM',
'LOOP CMPA X,3',
' JGE *+3',
'CHANGEM ENT2 0,3',
' LDA X,3',
' DEC3 1',
' J3P LOOP',
'EXIT JMP *',
' ORIG {}'.format(end_point),
' HLT',
])
num_numbers, max_val = 100, 0
# Register I1 denotes the number of integers in the memory buffer
computer.rI1.set(num_numbers)
# Register J stores the returning location
computer.rJ.set(end_point)
for i in range(1001, 1001 + num_numbers):
val = random.randint(0, 100000)
# Set random values to memory
computer.memoryAt(i).set(val)
max_val = max(max_val, val)
# Execute until the HLT operation
computer.executeUntilHalt()
print('Expected:', max_val)
# Register A stores the final maximum value
print('Actual:', computer.rA.value())
# The units of time for executing the codes, exclude the halt operation.
print('Compute Cost:', computer.elapsed())
IO 设备
环境中有几个预定义的 IO 设备。您可以在执行前设置输入设备的初始值。以下是从输入设备读取一个字并将同一个字写入输出设备的代码。
import mixal
# Initialize an environment
computer = mixal.Computer()
# Pre-defined indices for IO devices
card_reader_index = 16
card_punch_index = 17
computer.loadCodes("""
ORIG 3000
IN 100(16)
LIN JBUS LIN(16)
OUT 100(17)
LOUT JBUS LOUT(17)
""")
# Set the initial value of input device
computer.getDeviceWordAt(card_reader_index, 0).set('PRIME')
computer.executeUntilHalt()
# Check the output text of the output device
print(computer.getDeviceWordAt(card_punch_index, 0).getCharacters())