start assembler

This commit is contained in:
coolcoder613eb 2024-03-15 17:35:53 +11:00
parent e5d33f1593
commit c78498cef7
6 changed files with 89 additions and 0 deletions

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "oxvm"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "oxvm"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

44
src/asm.rs Normal file
View File

@ -0,0 +1,44 @@
use std::u32;
pub struct Assembler {
startaddr: u32,
}
impl Assembler {
pub fn new() -> Self {
Self { startaddr: 0 }
}
pub fn assemble(&mut self, code: String) {
for line in code.lines() {
match line.trim_start().chars().next() {
Some(character) => match character {
'.' => Self::directive(self, line.trim_start()),
_ => Self::code(line.trim_start()),
},
None => {
println!("Empty line.")
}
}
}
}
fn directive(&mut self, line: &str) {
let mut tokens = line.split_whitespace();
match tokens.next() {
Some(token) => match token {
".start" => {
let straddr = tokens.next().expect("No address for .start!");
let addr = u32::from_str_radix(
match straddr.strip_prefix("0x") {
Some(stripped) => stripped,
None => straddr,
},
16,
)
.expect("Error parsing address!");
self.startaddr = addr;
}
_ => println!("Unknown directive: {}", token),
},
None => println!("Empty directive?"),
}
}
fn code(line: &str) {}
}

22
src/main.rs Normal file
View File

@ -0,0 +1,22 @@
use std::env::args;
use std::fs::read_to_string;
mod asm;
use asm::Assembler;
mod opcodes;
/*
* oxvm asm prog.asm prog.bin
* oxvm prog.bin
*/
fn main() {
let mut arguments = args();
arguments.next();
match arguments.next() {
Some(filename) => {
println!("Filename: {}", filename);
let mut asmb = Assembler::new();
asmb.assemble(read_to_string(filename).expect("Failed to read file!"));
}
None => println!("No file specified"),
}
}

3
src/opcodes.rs Normal file
View File

@ -0,0 +1,3 @@
pub enum Opcodes {
Goto,
}

5
test.asm Normal file
View File

@ -0,0 +1,5 @@
# start memory address
.start 0x0000
.label loop
goto loop