-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Henrik Boeving
committed
Nov 28, 2020
1 parent
111ca93
commit 2348f78
Showing
13 changed files
with
478 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
LDA #0 | ||
ADD: ADD #1 | ||
JMP ADD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// comment | ||
LDA #1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
START: LDA #1 | ||
// Jumps to the label start | ||
JMP START |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)* } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.