Giter Club home page Giter Club logo

mixal's Introduction

MIXAL

Unit Tests Deploy Build Status Coverage Status Version Downloads License

[中文|English]

A MIX assembly language simulator.

Install

Make sure you have a C++ 11 compiler and execute:

pip install mixal

For MacOS users, you may need to use:

MACOSX_DEPLOYMENT_TARGET=10.9 pip install mixal

Sample

Following is a piece of sample code that finds the maximum value. There are upper-case letters in the names of functions and attributes as the codes were written with 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 Devices

There are several pre-defined IO devices in the environment. You can set the initial values of the input devices before the execution. Following is the code that reads one word from the input device and writes the same one to the output device.

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())

mixal's People

Contributors

cyberzhg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mixal's Issues

program(python) quit when loading

用的提供的示例代码,卡了一会直接退了,调试发现问题在computer.load里。
The python program (usage example) provided in this project failed to run and exits silently when doing computer.load.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.