diff options
| Commit | 1e75ec0d31f05fb8a87939db0415a63a8a021b18 (patch) | |
|---|---|---|
| Author | 2025-02-14 17:59:01 +0200 | |
| Committer | 2025-02-14 17:59:01 +0200 | |
| Files | 16528f9bdafcbbb9dc3072b626f485a5370068a4 | |
| tonkobali-1e75ec0d31f05fb8a87939db0415a63a8a021b18.tar tonkobali-1e75ec0d31f05fb8a87939db0415a63a8a021b18.tar.gz tonkobali-1e75ec0d31f05fb8a87939db0415a63a8a021b18.zip | ||
| Parent | 1a6b9be6c585f7cfc3351b7554179123f7543174 (diff) | |
feat: Execute fib, add dec instruction, fix constants in memory creation
6 files changed, 48 insertions, 51 deletions
| -rw-r--r-- | assembler/src/main.rs | 5 | ||||
| -rw-r--r-- | assembler/src/parser/code_gen.rs | 3 | ||||
| -rw-r--r-- | assembler/src/parser/instruction.rs | 19 | ||||
| -rw-r--r-- | example.s | 54 | ||||
| -rw-r--r-- | vm/src/vm/memory.rs | 13 | ||||
| -rw-r--r-- | vm/src/vm/primitive.rs | 5 |
diff --git a/assembler/src/main.rs b/assembler/src/main.rs index 630f1a1..3a6dd4a 100644 --- a/assembler/src/main.rs +++ b/assembler/src/main.rs @@ -7,7 +7,8 @@ use std::io::{Write, BufRead, BufReader}; use parser::Program; pub(crate) type Word = i64; -static PROGRAM_START: usize = 2; +static PROGRAM_START: usize = 10; +static PROGRAM_COUNTER: usize = 3; fn main() { let file_path = "./example.s"; @@ -43,7 +44,7 @@ fn main() { /* Machine code gen */ prog.instructions_to_constants(PROGRAM_START); println!("{}", prog); - let binary = prog.to_bytes(PROGRAM_START); + let binary = prog.to_bytes(PROGRAM_START, PROGRAM_COUNTER); 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 1c5b95a..ec4a2d2 100644 --- a/assembler/src/parser/code_gen.rs +++ b/assembler/src/parser/code_gen.rs @@ -50,9 +50,10 @@ impl Program { self.instructions.clear(); } - pub(crate) fn to_bytes(self, absolute_start: usize) -> Vec<u8> { + pub(crate) fn to_bytes(self, absolute_start: usize, pc: usize) -> Vec<u8> { let mut res = Vec::new(); res.resize(absolute_start, Default::default()); + res[pc] = self.constants[0]; res.extend(self.constants); res .into_iter() diff --git a/assembler/src/parser/instruction.rs b/assembler/src/parser/instruction.rs index cc0523f..ed3051d 100644 --- a/assembler/src/parser/instruction.rs +++ b/assembler/src/parser/instruction.rs @@ -1,5 +1,9 @@ use super::{ Instruction, Address }; +static Z: Address = Address::Memory(0); +static E: Address = Address::Memory(1); +static N: Address = Address::Memory(2); + #[derive(Debug)] pub(crate) enum ArgumentError { UnknownInstruction, @@ -25,7 +29,7 @@ impl Instruction { if self.args.len() < expected { return Err(ArgumentError::TooFew); } - else if self.args.len() > 3 { + else if self.args.len() > expected { return Err(ArgumentError::TooMany); } Ok(()) @@ -49,15 +53,14 @@ impl Instruction { return Err(ArgumentError::NotLocation); } }, + "dec" => { + self.check_args_len(1)?; + }, "add" => { self.check_args_len(2)?; }, "mov" => { self.check_args_len(2)?; - - if ! self.args[1].is_location() { - return Err(ArgumentError::NotLocation); - } }, "beq" => { self.check_args_len(2)?; @@ -73,13 +76,15 @@ impl 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" => vec![self.clone()], "jmp" => vec![ Instruction::new_subleq(&Z, &Z, &self.args[0]), ], + "dec" => vec![ + Instruction::new_subleq(&E, &self.args[0], &Address::Instruction(next + 1)), + ], "add" => vec![ Instruction::new_subleq(&self.args[0], &Z, &Address::Instruction(next + 1)), Instruction::new_subleq(&Z, &self.args[1], &Address::Instruction(next + 2)), @@ -103,7 +108,7 @@ impl Instruction { pub(crate) fn collapse_len(&self) -> usize { match &*self.name { - "subleq" | "jmp" + "subleq" | "jmp" | "dec" => 1, "add" => 3, @@ -1,43 +1,21 @@ -beq condition, if_true -jmp if_false +f0 = 0 +f1 = 1 +fN = 0 +n = 5 -if_true: - mov 517, %1 - jmp endif +fib: + beq n, fib_result ; while n > 0 -if_false: - mov 1952, %1 + mov f0, fN ; fN = f0 + f1 + add f1, fN + mov f1, f0 ; f0 = f1 + mov fN, f1 ; f1 = fN -endif: - jmp %0 + dec n ; n-- -condition = %30 + jmp fib -; 0 beq Memory(0), Instruction(2), -; 0: subleq Memory(0), Memory(0), Instruction(2), -; 1: subleq Memory(0), Memory(0), Instruction(4), -; 2: subleq Memory(0), Memory(0), Instruction(3), -; 3: subleq Memory(0), Memory(0), Instruction( 5 ), -; -; 1 jmp Instruction(4), -; 4: subleq Memory(0), Memory(0), Instruction( 10 ), -; -;if_true: -; 2 mov Constants(0), Memory(1), -; 5: subleq Memory(1), Memory(1), Instruction(6), -; 6: subleq Constants(0), Memory(0), Instruction(7), -; 7: subleq Memory(0), Memory(1), Instruction(8), -; 8: subleq Memory(0), Memory(0), Instruction(9), -; -; 3 jmp Instruction(5), -; 9: subleq Memory(0), Memory(0), Instruction( 14 ), -; -;if_false: -; 4 mov Constants(1), Memory(4), -; 10: subleq Memory(1), Memory(1), Instruction(11), -; 11: subleq Constants(1), Memory(0), Instruction(12), -; 12: subleq Memory(0), Memory(1), Instruction(13), -; 13: subleq Memory(0), Memory(0), Instruction(14), -; -; 5 jmp Memory(0), -; 14: subleq Memory(0), Memory(0), Memory(0), +fib_result: + mov f0, %1000 ; return f0 + mov %0, f0 ; Reset f0 + mov %1, f1 ; Reset f1 diff --git a/vm/src/vm/memory.rs b/vm/src/vm/memory.rs index 33a6de6..319b647 100644 --- a/vm/src/vm/memory.rs +++ b/vm/src/vm/memory.rs @@ -3,6 +3,8 @@ use std::{ ops::Index, }; +use super::primitive::spa; + pub(crate) type Word = i64; // Size of each memory block in words @@ -40,9 +42,13 @@ impl Memory { } fn new() -> Memory { - Memory { + let mut out = Memory { sparse_pages: HashMap::from([(0, Default::default())]), - } + }; + *out.get_mut(spa::Z) = 0; + *out.get_mut(spa::E) = 1; + *out.get_mut(spa::N) = -1; + out } pub(crate) fn new_from(mem: &Vec<Word>) -> Memory { @@ -50,6 +56,9 @@ impl Memory { for (i, chunk) in mem.chunks(PAGE_SIZE).enumerate() { memobj.sparse_pages.insert(i, Page::new(chunk)); } + *memobj.get_mut(spa::Z) = 0; + *memobj.get_mut(spa::E) = 1; + *memobj.get_mut(spa::N) = -1; memobj } diff --git a/vm/src/vm/primitive.rs b/vm/src/vm/primitive.rs index 43f217f..3e41bd0 100644 --- a/vm/src/vm/primitive.rs +++ b/vm/src/vm/primitive.rs @@ -2,7 +2,10 @@ use crate::vm::memory::{ Memory, Word }; // Special addrs pub mod spa { - pub(crate) const PROGRAM_COUNTER: usize = 2; + pub(crate) const Z: usize = 0; + pub(crate) const E: usize = 1; + pub(crate) const N: usize = 2; + pub(crate) const PROGRAM_COUNTER: usize = 3; } #[derive(Debug, PartialEq, Eq)] |
