Risc-V assembly interpreter built with pure Rust

Overview

Logo

Risc-V Interpreter

Interpreter for Risc-V assembly built with Rust
Report bug · Request feature

Table of contents

Quick start

The program will take a file path as an arg in the command line

  • Install rust toolchain
  • Within project root, on cli, run:
    • cargo run "file.extension"
  • Now you should see the result if your assembly code is correct :)

Exemple Program

.data
DIVIDEND: .word 20
SPACE:.asciz " "
DIVISOR: .word 7
ARRAY: .word -118, 8, 7, 99
RESULT: .word -1
REMAINDER: .word -1

.text

.main:
  # euclidean division algorithm

  la a0, DIVIDEND
  la a1, DIVISOR

  lw a0, 0(a0)
  lw a1, 0(a1)

  jal floorDiv

  la t0, RESULT
  sw a0, 0(t0)

  la t0, REMAINDER
  sw a1, 0(t0)

  li a7, 10
  ecall


floorDiv:
    blt a0, zero, normUp
    li t5, 0

afterNormUp:
    blt a1, zero, normBottom
    li t6, 0

afterNormBottom:
    li t0, 0

    j while

normUp:
    sub a0, zero, a0
    li t5, 1

    j afterNormUp

normBottom:
    sub a1, zero, a1
    li t6, 1

    j afterNormBottom

while:
    blt a0, a1, endWhile
    sub a0, a0, a1

    addi t0, t0, 1

    j while

endWhile:
    xor t1, t5, t6
    bgt t1, zero, changeSign
    j endFloorDiv

changeSign:
    sub t0, zero, t0

endFloorDiv:
    mv a1, a0
    mv a0, t0

    ret

Instructions Supported

// R-type
Add,
Sub,
Mul,
Div,
Rem,
Sll,
Xor,
Or,
And,

// I-type
Addi,
Slli,
Xori,
Ori,
Andi,
Lw,
La,

// B-type
Blt,
Beq,
Bgt,

// S-type
Sw,

// J-type
J,
Jal,
Jalr,

// Pseudo Instructions
Ret,
Li,
Mv,

Ecalls Supported

10 => finish program

Not Yet Supported Features

  • Most of .data, actually only two are supported
    • .word
    • .asciz

What's included

Basic RI32I and RI32M is covered. Stack is also supported but only using sp reg to access it

interpreter/
└── src/
    ├── dot_data/
    │   ├── data.rs
    │   └── mod.rs
    ├── file_reader/
    │    ├── mod.rs
    │    └── reader.rs
    ├── instructions/
    │    ├── B_type/
    │    │    ├── B_type.rs
    │    │    ├── mod.rs
    │    │    ├── pub_utils.rs
    │    │    └── utils.rs
    │    ├── I_type/
    │    │    ├── I_type.rs
    │    │    ├── load_utils.rs
    │    │    ├── mod.rs  
    │    │    ├── pub_utils.rs
    │    │    └── utils.rs
    │    ├── J_type/
    │    │    ├── J_type.rs
    │    │    ├── mod.rs
    │    │    ├── pub_utils.rs
    │    │    └── utils.rs
    │    ├── R_type/
    │    │    ├── mod.rs
    │    │    ├── pub_utils.rs
    │    │    ├── R_type.rs
    │    │    └── utils.rs
    │    ├── S_type/
    │    │    ├── pub_utils.rs
    │    │    ├── S_type.rs
    │    │    └── utils.rs
    │    ├── instruction.rs
    │    └── mod.rs    
    ├── registers/
    │   ├── mod.rs
    │   └── registers.rs
    ├── stack/
    │   ├── mod.rs
    │   ├── pub_utils.rs
    │   └── stack.rs
    ├── run/
    │    ├── mod.rs
    │    └── run.rs
    ├── .gitignore
    ├── Cargo.lock
    ├── Cargo.toml
    ├── LICENSE.md
    └── README.md

Bugs and feature requests

Have a bug or a feature request? Please first search for existing and closed issues. If your problem or idea is not addressed yet, please open a new issue.

Contributing

If you wish to contribute to the project, you can submit your pull request, which will be validated and merged as soon as possible, or open an issue.

Creators

Ismael Bortoluzzi

Thanks

Thank you for taking a look at my project!

Copyright and license

Code released under the MIT License.

Enjoy

You might also like...
Regorus - Rego interpreter, analyzer and validator written in Rust

regorus THIS REPOSITORY IS IN ACTIVE DEVELOPMENT AND NOT INTENDED FOR PRODUCTION USE. Regorus is a Rego interpreter, analyzer and checker written in R

This crate defines a single macro that is a brainfunct compile-time interpreter.

Compile Protection This crate defines a single macro that is a brainfunct compile-time interpreter. One example is as follows #![recursion_limit = "18

Compiler & Interpreter for the (rather new and very experimental) Y programming language.

Y Lang Why would anyone build a new (and rather experimental) language with no real world use case. Design Y (pronounced as why) is based on the idea

PICNIC Is Config Notation Interpreter/Converter
PICNIC Is Config Notation Interpreter/Converter

PICNIC Is Config Notation Interpreter/Converter 🦀 PICNIC PICNIC's name is powered by AI, which immediately makes it worth your time: Human: Please co

dustls, a pure-rust DTLS implementation

dustls, a pure-rust DTLS implementation A DTLSv1.2 implementation in Rust, reusing rustls for cryptographic primitives and most message payload format

WIP demo project for pure rust playing nicely with MCUBoot/Infinitime OTA

pinetime-rust-mcuboot WIP demo project for pure rust playing nicely with MCUBoot/Infinitime OTA Firmware Behavior This is an example project so I just

A pure-Rust serverless discord chatbot hosted on Cloudflare Workers.

About A pure-Rust serverless discord chatbot hosted on Cloudflare Workers. With a free account you have up to 100k requests per day. For storing state

A fast llama2 decoder in pure Rust.
A fast llama2 decoder in pure Rust.

llama2.rs 🤗 This is a Rust implementation of Llama2 inference on CPU The goal is to be as fast as possible. It has the following features: Support fo

🦜 A hassle-free, highly performant, host it yourself Discord music bot built with Serenity in Rust. Powered by youtube-dl and Genius.

🦜 A hassle-free, highly performant and fast evolving Discord music bot built with Serenity in Rust. Deployment Usage Just create a bot account, copy

Owner
null
Rust library for emulating RISC-V rv32imac

This library can execute instructions against any memory and register file that implements the required primitives in the traits lib_rv32::traits::{Memory, RegisterFile}. This is to encourage usage with whatever frontend you desire.

Trevor McKay 14 Dec 7, 2022
RISC-V instruction decoder written in Rust.

raki RISC-V instruction decoder written in Rust. Both 32/64bit support. Support rv32/64imac, Zicsr, Zifencei extensions. Implement Display trait for f

Norimasa Takana 5 Oct 18, 2023
Brainfuck interpreter written in rust

Brainfuck Interpreter Written in Rust Simple Interpreter that runs bare Brainfuck and extends it with debug command # which prints content of first te

Was 1 Nov 28, 2021
The brainfuck language interpreter written in Rust

The brainfuck language interpreter written in Rust

Jan Štaffa 1 Feb 21, 2022
rlox-interpreter is an AST-walking implementation of Bob Nystrom's Lox language in Rust.

rlox-interpreter rlox-interpreter is an AST-walking implementation of Bob Nystrom's Lox language in Rust. Disclaimer: This is my first Rust project, d

Paul Fedorow 3 Oct 5, 2022
A Brainf** interpreter written in rust 🦀

Brainf Interpreter An interpreter for the esoteric programming language Brainfuck written in rust ?? Compilation NOTE: To compile the project, you nee

Chitram Dasgupta 2 Sep 12, 2022
Brainf - A brainfuck interpreter written in Rust 🦀

brainf A brainfuck interpreter written in Rust ?? . Do not I wrote this in my spare time... but still its good! Run Locally Clone the project git cl

Nav 3 Oct 8, 2022
Brainfu*k interpreter and REPL written in Rust🦀

brainfuck interpreter: a simple brainfuck interpreter and REPL writen in rust ?? Read this in other languages. AR Arabic-العربية Features Run brainfuc

Anas Elgarhy 7 Feb 1, 2023
A fast, powerful and configurable interpreter written in Rust

brainfuck-interpreter Overview A fast, powerful and configurable interpreter written in Rust, which allows various options to meet different demends,

Justin Chen 4 Feb 12, 2023
A simple interpreter written in Rust programming language.

Interpreter in Rust A simple interpreter written in Rust programming language. Getting Started These instructions will get you a copy of the project u

Ismail Mosleh 5 Feb 17, 2023