Giter VIP home page Giter VIP logo

hack-vm-translator's Introduction

Hack VM Translator

A Hack Virtual Machine (VM) translator written in Rust. It translates VM code into Hack assembly code, which can then be assembled and run on the Hack computer as defined in the Nand2Tetris course.

Features

  • Translates VM commands into Hack assembly code.
  • Supports arithmetic operations, memory access, branching, and function calls.
  • Efficiently handles static and dynamic content in the generated assembly code.
  • Command-line interface for specifying the input VM file.

Prerequisites

  • Rust (make sure you have the latest version installed)

Installation

  1. Clone the repository:

    git clone https://github.com/tairasu/hack-vm-translator.git
    cd hack-vm-translator
  2. Build the project using Cargo:

    cargo build --release

Usage

To use the translator, run the binary (compiled in /target/release) with the path to your .vm file as an argument:

./hack-vm path/to/your/file.vm

For example:

./hack-vm 7/StackArithmetic/StackTest/StackTest.vm

This will generate a .asm file with the same name in the same directory.

Example

Given a VM file StackTest.vm with the following content:

push constant 7
push constant 8
add

Running the translator:

./hack-vm 7/StackArithmetic/StackTest/StackTest.vm

Will produce a StackTest.asm file with the translated Hack assembly code:

@7
D=A
@SP
A=M
M=D
@SP
M=M+1
@8
D=A
@SP
A=M
M=D
@SP
M=M+1
@SP
M=M-1
A=M
D=M
@SP
M=M-1
A=M
M=D+M
@SP
M=M+1

Project Structure

  • main.rs: The entry point of the application. Handles command-line arguments and file operations.
  • lib.rs: Contains the core logic for parsing and translating VM commands.

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

This project is inspired by the Nand2Tetris course.

hack-vm-translator's People

Contributors

tairasu 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.