Coder Social home page Coder Social logo

eregs's Introduction

eREGS Stack machine & Interpreter

This is just an old, simple toy project and consists of a simple stack language and an interpreter for integer arithmetic.

The instruction set consists of the following instructions:

Instruction Description
push Pushes element to the top of the stack
pop Pops (retrieves) top element from stack
add Pops 2 top most elements from stack, performs an addition and pushed result back to the stack
sub Same as for "add", but performs subtraction
mul Same as for "add", but performs multiplication
div Same as for "add", but performs division (checks for division by 0)
cmpl Checks 2 top most stack elements (true, if Arg1 < Arg2, 0 otherwise)
cmpg Checks 2 top most stack elements (true, if Arg1 > Arg2, 0 otherwise)
cmple Checks 2 top most stack elements (true, if Arg1 <= Arg2, 0 otherwise)
cmpge Checks 2 top most stack elements (true, if Arg1 >= Arg2, 0 otherwise)
cmpeq Checks 2 top most stack elements (true, if Arg1 == Arg2, 0 otherwise)
cmpne Checks 2 top most stack elements (true, if Arg1 != Arg2, 0 otherwise)
print Pops top element from stack and prints its value
jmp Unconditional jump to label
jfalse Conditional jump to label
L Label (used for go to markers)
r Register definition (for variables)
shl Binary left shift
shr Binary right shift
rol Binary rotate left
ror Binary rotate right
not Binary NOT
and Binary AND
xor Binary Exclusive Or
mod Modulo

For a detailed description on eREGS you may check the documentation located in the doc directory (german only).


Compilation

Just run make all to compile the interpreter.

๐Ÿ›‘ (This is just a spontaneously uploaded git repository and the Makefile may not work properly as of now).

Requirements

  • Make
  • Yacc
  • Flex

Running the interpreter

Just run ./bin/eregs <eREGS SOURCEFILE> to run the interpreter.


Example Code

A "eREGS" source code may use up to 32 registers and 999 labels (jump markers).

C commentary style // for single line comments and /* for multiline comments */ are also supported.

The following example shows how to compute the average of the two numbers 22 and 48 in a not so obvious way ๐Ÿ˜ˆ.

push $22
push $48
xor 
pop r1
push $1
push r1
shr
pop r1
push $22
push $48
and
push r1
add
print

See the examples directory for more examples on how to use the eREGS stack language.

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.