Skip to content

Commit

Permalink
parsing seems to be working fine
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Boeving committed Nov 28, 2020
1 parent 111ca93 commit 2348f78
Show file tree
Hide file tree
Showing 13 changed files with 478 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
180 changes: 180 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "hm-asm"
version = "0.1.0"
authors = ["Henrik Boeving <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pest = "2.0"
pest_derive = "2.0"
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# hm-asm
A small assembler I wrote for use with the microprocessor from the Book "Grundlagen der Technische Informatik"
A small assembler I wrote for use with the microprocessor from the Book "Grundlagen der Technische Informatik" by Dirk
W. Hoffmann.

## How does it work?
You run the binary with a path to an assembler file like this `cargo run path/to/file.asm`.

## Syntax?
You can find some syntax examples in `examples/`

## Limitations
Note that since this microprocessor is only a 4 bit microporcessor we cannot possibly have programs with more than 16
instructions. Furthermore since the parameters of the instructions are stored in the same part of memory as where you
can store values with the `STA` instruction it is technically possible to overwrite your program at runtime and do a
sort of self modifying programming style, this assembler does not warn you if you do this as of now.
3 changes: 3 additions & 0 deletions examples/add_endless.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LDA #0
ADD: ADD #1
JMP ADD
2 changes: 2 additions & 0 deletions examples/comments.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// comment
LDA #1
3 changes: 3 additions & 0 deletions examples/labels.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
START: LDA #1
// Jumps to the label start
JMP START
14 changes: 14 additions & 0 deletions examples/list_of_ops.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NOP
LDA #1
LDA (1)
STA (1)
ADD #1
ADD (1)
SUB #1
SUB (1)
JMP 0
BRZ #1
BRC #1
BRN #1
MYLABEL: NOP
JMP MYLABEL
30 changes: 30 additions & 0 deletions src/asm.pest
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
program = _{ SOI ~ "\n"* ~ (stmt ~ "\n"+) * ~ stmt? ~ EOI }


stmt = { ((label ~ ":")? ~ instruction)}


instruction = {
no_arg_instruction |
arg_instruction ~ argument |
jump_instruction ~ jump_argument |
memory_location_instruction ~ memory_location |
constant_arg_instruction ~ digit_literal
}

memory_location_instruction = {"STA"}
constant_arg_instruction = {"BRZ" | "BRC" | "BRN"}
jump_instruction = {"JMP"}
arg_instruction = {"LDA" | "ADD" | "SUB"}
no_arg_instruction = { "NOP" }

jump_argument = { jump_location | label}
argument = { memory_location | digit_literal }
memory_location = { "(" ~ ASCII_HEX_DIGIT ~")" }
digit_literal = {"#" ~ ASCII_HEX_DIGIT}
jump_location = { ASCII_HEX_DIGIT }

label = { ASCII_ALPHA_UPPER+ }

WHITESPACE = _{ " " | "\t" }
COMMENT = _{"//" ~ (!"\n" ~ ANY)* }
51 changes: 51 additions & 0 deletions src/asm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Argument {
MemoryLocation(u8),
Constant(u8)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Label<'a>{
pub name: &'a str,
pub location: u8
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum JumpArgument<'a> {
Location(u8),
Label(&'a str)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Instruction<'a> {
NoArgumentInstruction(NoArgumentInstruction, Option<Label<'a>>),
MemoryLocationInstruction(MemoryLocationInstruction, Option<Label<'a>>),
ConstantArgumentInstruction(ConstantArgumentInstruction, Option<Label<'a>>),
ArgumentInstruction(ArgumentInstruction, Option<Label<'a>>),
Jump(JumpArgument<'a>, Option<Label<'a>>)
}


#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum NoArgumentInstruction {
NOP
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MemoryLocationInstruction {
STA(u8)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ConstantArgumentInstruction {
BRZ(u8),
BRC(u8),
BRN(u8)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ArgumentInstruction {
LDA(Argument),
ADD(Argument),
SUB(Argument),
}
Empty file added src/generate.rs
Empty file.
32 changes: 32 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::fs;
use std::env;


#[macro_use]
extern crate pest_derive;

use pest::Parser;


mod asm;
mod parse;
mod generate;

use parse::{parse_asm, AsmParser};

fn main() {
let file_name = env::args().nth(1);

let file_content = match file_name {
Some(file_name) => {
fs::read_to_string(file_name).expect("Could not read the provided asm file")
}
None => {
println!("No input file was provided");
return;
}
};

let instructions = parse_asm(AsmParser::parse(parse::Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)));
println!("{:#?}", instructions);
}
Loading

0 comments on commit 2348f78

Please sign in to comment.