diff options
| Commit | 13bc44e09c4e0cb2f4e9f90843716624e4c1cde1 (patch) | |
|---|---|---|
| Author | 2025-02-14 00:45:40 +0200 | |
| Committer | 2025-02-14 00:45:40 +0200 | |
| Files | dceff1a98062d9887b30d8586a1d6466faa6ef01 /assembler/src | |
| tonkobali-13bc44e09c4e0cb2f4e9f90843716624e4c1cde1.tar tonkobali-13bc44e09c4e0cb2f4e9f90843716624e4c1cde1.tar.gz tonkobali-13bc44e09c4e0cb2f4e9f90843716624e4c1cde1.zip | ||
| Parent | cf56360e18a05da6c55148b2011fd22f8224bfef (diff) | |
fix(assembler): Fix addressing
4 files changed, 17 insertions, 7 deletions
| -rw-r--r-- | assembler/src/lexer/mod.rs | 2 | ||||
| -rw-r--r-- | assembler/src/main.rs | 9 | ||||
| -rw-r--r-- | assembler/src/parser/code_gen.rs | 4 | ||||
| -rw-r--r-- | assembler/src/parser/instruction.rs | 9 |
diff --git a/assembler/src/lexer/mod.rs b/assembler/src/lexer/mod.rs index b662242..b0220fe 100644 --- a/assembler/src/lexer/mod.rs +++ b/assembler/src/lexer/mod.rs @@ -90,7 +90,7 @@ pub fn split(mut line: String) -> Vec<Token> { panic!("Before an = you may only have a symbol name!"); }, '\\' => skip_next = true, - '%' => address = true, + '%' => { buffer.pop(); address = true; }, '-' => if address { panic!("Addresses cannot be negative!"); }, diff --git a/assembler/src/main.rs b/assembler/src/main.rs index 7c68d1d..9df9dbd 100644 --- a/assembler/src/main.rs +++ b/assembler/src/main.rs @@ -2,12 +2,12 @@ mod lexer; mod parser; use std::fs::File; -use std::io::{BufRead, BufReader}; +use std::io::{Write, BufRead, BufReader}; use parser::Program; pub(crate) type Word = i64; -static PROGRAM_START: usize = 10; +static PROGRAM_START: usize = 2; fn main() { let file_path = "./example.s"; @@ -35,10 +35,13 @@ fn main() { println!(""); prog.collapse_instructions(); println!("{}", prog); + println!(""); /* Machine code gen */ prog.instructions_to_constants(PROGRAM_START); + println!("{}", prog); let binary = prog.to_bytes(PROGRAM_START); - + let mut out = File::create("program.bin").unwrap(); + out.write_all(&binary).unwrap(); } diff --git a/assembler/src/parser/code_gen.rs b/assembler/src/parser/code_gen.rs index 92bf1d1..7d6fc45 100644 --- a/assembler/src/parser/code_gen.rs +++ b/assembler/src/parser/code_gen.rs @@ -27,8 +27,10 @@ impl Program { /* Machine code generation */ - pub(crate) fn instructions_to_constants(&mut self, absolute_start: usize) { + pub(crate) fn instructions_to_constants(&mut self, mut absolute_start: usize) { + absolute_start += 1; let constlen = self.constants.len(); + self.constants.insert(0, (absolute_start + constlen) as Word); for instr in &mut self.instructions { for arg in &mut instr.args { diff --git a/assembler/src/parser/instruction.rs b/assembler/src/parser/instruction.rs index 8c8e8aa..f78c9a6 100644 --- a/assembler/src/parser/instruction.rs +++ b/assembler/src/parser/instruction.rs @@ -8,15 +8,20 @@ impl Instruction { } } - pub(crate) fn collapse(&mut self) -> Vec<Instruction> { + pub(crate) fn collapse(&mut self, next: usize) -> Vec<Instruction> { self.name = self.name.to_lowercase(); static Z: Address = Address::Memory(0); match &*self.name { - "subleq" | "add" | "mult" => vec![self.clone()], + "subleq" => vec![self.clone()], "jmp" => vec![ Instruction::new_subleq(&Z, &Z, &self.args[0]), ], + "add" => vec![ + Instruction::new_subleq(&self.args[0], &Z, &Address::Instruction(next + 1)), + Instruction::new_subleq(&Z, &self.args[1], &Address::Instruction(next + 2)), + Instruction::new_subleq(&Z, &Z, &Address::Instruction(next + 3)), + ], _ => panic!("Unknown instruction {}!", self.name), } } |
