Skip to content

KorieDrakeChaney/rust-masm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust MASM

A simple Rust library for generating MASM code

Usage

use rust_masm::MidenProgram;

fn main() {
    let mut program = MidenProgram::new();

    program.push(5);
    program.push(1);
    program.add();
}

This will generate the following MASM code:

begin
    push 5
    push 1
    add
end

You can do more complex things like:

use rust_masm::{MidenProgram, EmptyProgram};

fn main() {
    let mut add_program = EmptyProgram::new();

    add_program.add_n(5);

    let mut if_program = EmptyProgram::new();

    let mut else_program = EmptyProgram::new();
    else_program.push(1);
    else_program.add_n(2);

    if_program.if_else_block(&mut add_program, &mut else_program);

    let mut rand_program = EmptyProgram::new();

    rand_program.push(5);

    rand_program.push(1);

    rand_program.mem_store();

    rand_program.mem_load_n(1);

    rand_program.mul_n(2);

    rand_program.eq_n(10);

    let mut program = MidenProgram::new();

    program.add_program(&mut rand_program);

    program.add_program(&mut if_program);

    let mut repeat_program = EmptyProgram::new();

    repeat_program.exp_n(2);

    program.repeat(5, &mut repeat_program);
}

This will generate the following MASM code:

begin
	push.5
	push.1
	mem_store
	mem_load.1
	mul.2
	eq.10
	if.true
		add.5
	else
		push.1
		add.2
	end
	repeat.5
		exp.2
	end
end

#stack output : [3273349785757477523, 0, 0, 0, 0, 0, 0, 0]

Seeing your stack transform

You can see how your stack transforms by using the print method:

use rust_masm::{MidenProgram};

fn main() {
    let mut program = MidenProgram::new();

    program.push(5);

    program.print("push 5");

    program.push(1);

    program.print("push 1");

    program.add();

    program.print("add");
}

See errors in your MASM code

use rust_masm::{MidenProgram};

fn main() {
    let mut program = MidenProgram::new();

    program.push(1);
    program.push(2);

    program.and();

    program.print_masm();
}

This will generate the following MASM code:

begin
        push.1
        push.2
        #ERROR: NotBinaryValue(2), 2 is not binary
        #and
end

Parse from MASM to Rust

You can also parse MASM code into Rust code:

use rust_masm::{MidenProgram};

fn main() {
    let mut program = MidenProgram::parse("
	begin
        	push.5
        	push.1
        	add
    	end
    "
    ).unwrap();

    program.print("add 5 and 1");
}

CLI

Also try using this simple CLI tool to see how your stack transforms:

cargo run

About

Allows you to create miden programs with rust

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages