GithubHelp home page GithubHelp logo

moritzgoeckel / assembly-ish-interpreter Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 29 KB

Interpreter for an assembly like language

JavaScript 100.00%
interpreter custom-language programming-language nodejs assembly language virtual-machine

assembly-ish-interpreter's Introduction

Assembly-ish interpreter

This is an Interpreter for an assembly like language in Node.js. It's Massembly, the M stands for Moritz :)

List of commands

  • mov reg|mem num|reg|mem
  • add reg num|reg
  • sub reg num|reg
  • mul reg num|reg
  • div reg num|reg
  • mod reg num|reg
  • out num|reg
  • end
  • push num|reg
  • pop reg
  • label:
  • and reg num|reg
  • or reg num|reg
  • xor reg num|reg
  • not reg
  • cmp num|reg num|reg
  • je label
  • jne label
  • jg label
  • jge label
  • jl label
  • jle label
  • jmp label
  • check num|reg op num|reg
  • dbg
  • ; comment
  • nop

Example program: Finding primes

;Finding prime numbers
;Sieve of Eratosthenes

;Heighest number
mov rm 200

;Start number
mov rs 2

;Create a list with numbers
mov ry rs
start:
mov [ry] ry
add ry 1
cmp ry rm
jle start

;Set start adress
mov ry rs
out rs

conductSearch:

mov rx [ry]
mov rb ry
deletingNonPrimes:
add rb rx
mov [rb] 0
cmp rb rm
jle deletingNonPrimes

findNext:
add ry 1
cmp ry rm
jg shutdown
mov ro [ry]
cmp ro 0
je findNext

out ry
jmp conductSearch

shutdown:
halt

This will output

2
3
5
7
11
13
17
...

Running the interpreter

Command line

Just execute the mexec.js with node.js and provide the source file path as parameter

node mexec.js <file>

To run the example program use

node mexec.js Examples\primesTest.m

Javascript

An example on how to run the interpreter from Javascript

//Including the Interpreter
const execMassembly = require("./Massembly.js");
const fs = require('fs')

//Load the source code from a file
fs.readFile('./Examples/primesTest.m', 'utf8', function (err,data) {
    execMassembly(data); // <-- Execute the code
});

Extending the interpreter

Just extend the commands object in the Commands.js. Key is the command and value is a function receiving the entire line string, the arguments array and the context object as parameters.

The function can change the context, end the software and decide which line is the next to execute.

Implementing commands

// Ending the software
"end":function(line, args, context){
    context.__end = true;
},
// Doing an output
"out":function(line, args, context){
    console.log(getRightHandValue(args[0], context));
    context.nextLine++;
},
// Multiplication
"mul":function(line, args, context){
    setRegister(
        args[0], 
        getRightHandValue(args[0], context) * getRightHandValue(args[1], context), 
        context
    );
    context.nextLine++;  
}

assembly-ish-interpreter's People

Contributors

moritzgoeckel avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.