GithubHelp home page GithubHelp logo

pokorj54 / register-machine Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 20 KB

Implementation of a model of computation - Register machine

C++ 32.96% Makefile 0.73% Python 54.58% R 11.26% Shell 0.47%
computability computability-theory computer-science

register-machine's Introduction

Register Machine

Semestral work for NI-VYC at FIT CTU.

The register machine

The register machine is a machine composed of integer registers and a program made of instructions.

Four instructions are supported:

  • inc r: increments the value stored in register r by 1.
  • dec r: decrements the value stored in register r by 1. If the current value is 0 then it increments the program counter by 2 instead of 1.
  • print r: prints the value stored in register r.
  • jmp x: changes the program counter by x.

The program can be made only from these instructions, one per line, everything else is undefined behaviur. Programs in folder programs has to be preprocessed before running.

The computation start with the first instruction, after execution of an instruction the program counter is incremented by 1. The program terminates when program counter points out of the program. For reusability of programs the program should end with the program counter pointing last line +1.

Preprocessor

The preprocessor is made to make it easier to write more complicated code and it allows to reuse existing programs and to labels to jumps.

Reusing programs

Only programs that are in programs can be included. To include the program use $ before it's name and then follow it with the register mapping. Be sure that right amount of registers is mapped, sometimes program needs also to have temporary register allocated.

This is file programs/clear.r.

# sets register 0 to 0
dec 0
jmp -1

And this is how to use it in program that sets the register 0 to 3.

# set register 0 to 3
$clear 0
inc 1
inc 1
inc 1

Jump labels

Jump labels start with the symbol #, which is followed by a name. Every term that starts with the symbol >, which is followed by a name of a label, is replaced by the distance (number of instruction from label down to the jump) from the label of specified name. Since the labels are removed after preprocessing, they can be also used as comments.

Example of using labels - adding register 0 to the register 1.

# R1 = R0 + R1; R2 is temporary
$move 0 2
# decrby1
dec 2
jmp >add1both
jmp >end
# add1both
inc 0
inc 1
jmp >decrby1
# end

How to run

To compile the register machine run make, to run it call ./r.out

To run the preprocessor run python3 preprocessor.py input_program.t output_program.r.

Preprocessing and running a program is combined in single command ./r.sh program.r.

register-machine's People

Contributors

pokorj54 avatar

Watchers

 avatar

register-machine's Issues

Preprocessor errors

The preprocessor should tell what is the problem with the code, not just throw an exception or worse do something even if it isn't correct.

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.