From 13bc44e09c4e0cb2f4e9f90843716624e4c1cde1 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Fri, 14 Feb 2025 00:45:40 +0200 Subject: fix(assembler): Fix addressing --- assembler/src/parser/code_gen.rs | 4 +++- assembler/src/parser/instruction.rs | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'assembler/src/parser') 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 { + pub(crate) fn collapse(&mut self, next: usize) -> Vec { 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), } } -- cgit v1.3.1-23-gd82d