diff --git a/src/interpreter/computer.rs b/src/interpreter/computer.rs index aeafeeb..1d7f3a5 100644 --- a/src/interpreter/computer.rs +++ b/src/interpreter/computer.rs @@ -39,9 +39,11 @@ impl Computer { flags: Flags::new(), memory: Memory::new(), }; + log::info!("Initializing stack..."); computer.init_stack().unwrap(); // Copy static data to 0x0000; + log::info!("Initializing static data..."); for (addr, b) in data.iter().enumerate() { let val = ImmediateOperand::Byte(*b); computer.write(val, (addr as Word).into()).unwrap(); @@ -80,7 +82,6 @@ impl Computer { for env in envs.iter() { log::debug!("Adding env {env} to stack"); for b in env.chars().rev() { - log::debug!("{}", b); self.push_stack(ImmediateOperand::Byte(b as u8))?; } } @@ -88,11 +89,10 @@ impl Computer { // write 0-terminated argv's let mut argv_ptrs = Vec::new(); - for arg in argv.iter() { + for arg in argv.iter().rev() { log::debug!("Adding arg {arg} to stack"); self.push_stack(ImmediateOperand::Byte(0))?; for b in arg.chars().rev() { - log::debug!("{}", b); self.push_stack(ImmediateOperand::Byte(b as u8))?; } argv_ptrs.push(self.regs.sp); @@ -127,7 +127,25 @@ impl Computer { ImmediateOperand::Byte(_) => 1, ImmediateOperand::Word(_) => 2, }); - log::debug!("writing at sp: {:#04x}", self.regs.sp); + match val { + ImmediateOperand::Byte(b) => { + log::debug!( + "writing byte {b:#04x} ({}) at sp: {:#04x}", + b as char, + self.regs.sp + ) + } + ImmediateOperand::Word(v) => { + let [low, high] = v.to_le_bytes(); + log::debug!( + "writing word {v:#04x} ({}) ({}) at sp: {:#04x} and {:#04x}", + low as char, + high as char, + self.regs.sp, + self.regs.sp + 1 + ) + } + } self.memory.write_raw( self.mem_addr( ImmediateOperand::from(self.regs.sp).into(), @@ -430,7 +448,6 @@ impl Computer { target: ModRmTarget, val: ImmediateOperand, ) -> Result<(), InterpreterError> { - log::debug!("Writing {val} into {target}"); match target { ModRmTarget::Memory(idx) => self.write(val.into(), idx)?, ModRmTarget::Register(reg) => self.regs.write(reg, val), diff --git a/src/interpreter/interpreter.rs b/src/interpreter/interpreter.rs index 52d4c3c..cec6ad0 100644 --- a/src/interpreter/interpreter.rs +++ b/src/interpreter/interpreter.rs @@ -77,6 +77,7 @@ impl Interpreter { pub fn interpret(&mut self) -> Result<(), InterpreterError> { while self.ip < self.text.len() { + log::debug!("=============== Fetching next instruction... ==============="); self.disassembler.offset = self.ip; self.disassembler .decode_instruction() diff --git a/src/interpreter/memory.rs b/src/interpreter/memory.rs index ac7e90b..4bc4f1f 100644 --- a/src/interpreter/memory.rs +++ b/src/interpreter/memory.rs @@ -26,6 +26,7 @@ impl Memory { return Err(InterpreterError::MemoryOutOfBound(addr)); } else { self.raw[addr as usize] = b; + log::debug!("Wrote raw byte {b:#04x} at addr {addr:#04x}"); } } ImmediateOperand::Word(w) => { @@ -35,6 +36,9 @@ impl Memory { let [low, high] = w.to_le_bytes(); self.raw[addr as usize] = low; self.raw[(addr + 1) as usize] = high; + log::debug!( + "Wrote raw byte {low:#04x} and {high:#04x}, starting at addr {addr:#04x}" + ); } } } @@ -54,6 +58,7 @@ impl Memory { .get((addr + 1) as usize) .ok_or(InterpreterError::MemoryOutOfBound(addr))? .to_owned(); + log::debug!("Read raw byte {b1:#04x} and {b2:#04x}, starting at addr {addr:#04x}"); Ok(Word::from_be_bytes([b2, b1])) } }