Coder Social home page Coder Social logo

hvme's Introduction

Hack Virtual Machine Emulator

This is a virtual machine emulator for the Hack Virtual Machine as specified in The Elements of Computing Systems which runs directly on your machine instead of compiling to Hack assembly.

Try it out

git clone https://github.com/thass0/hvme.git
cd hvme
make run args=examples/fibonacci.vm

This computes the 16th element in the Fibonacci sequence (987).

To Do

  • Add features to the System API (In Progress)

  • Add function, call and return (completed language spec)

  • Combine multiple files and manage their static data.

System API

The Hack VM specification assumes that VM code is compiled down to binary code which runs on the physical (the computer they're talking about doesn't really exist) machine. There, I/O works by writing and reading from the screen and keyboard memory maps (which are not yet implemented TBH).

The downside of this approach is that it's very annoying to be limited to such primitive forms of I/O while actually running on a real computer all the time. This is why I decided to add a basic API to just use standard I/O.

It's defined as a set of HVM functions which are available by default to any program. While those functions are not actually implemented using HVM they look and feel just like native functions and thereby don't extend the core language.

Right now, you can use the following I/O functions. Checkout the examples/ to get an idea of how to use them.

  • Sys.print_char(ch) -> 0: prints the given character.

  • Sys.print_num(num) -> 0: prints the given number.

  • Sys.print_str(nchars, addr) -> 0: prints a given number of characters stored on the heap starting at a given address.

  • Sys.read_char() -> ch: reads a single character (getchar).

  • Sys.read_num() -> ch: reads a single number (scanf).

  • Sys.read_str(addr) -> nchars: reads a string (getline) and stores it on heap at addr. The number of stored characters is returned.

Note that none of the above functions accept different types. There are no types in HVM after all! They merely interpret the values differently.

Hence, all the names in the describes interfaces are totally symbolic. All functions must return a single value and take any number of arguments. The interfaces read like this:

 Guess     Arguments: `arg_1` is    Symbolic name for the return value
  what.     lowest on stack.       (usually 0 if it has no meaning).
   __\________   _/_    _\_       __/__
  /           \ /   \  /   \     /     \
  function_name(arg_1, arg_2) -> ret_val

hvme's People

Contributors

thass0 avatar

Stargazers

 avatar

Watchers

 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.