From 7e7e648fa8a3a345e3f8ad7d375b381b5d62e270 Mon Sep 17 00:00:00 2001 From: Marco Thomas Date: Tue, 17 Jun 2025 10:54:42 +0900 Subject: [PATCH] fix(interpreter): wrong le byte order on pop --- src/interpreter/computer.rs | 15 ++++++++++++++- src/interpreter/memory.rs | 2 +- src/interpreter/register.rs | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/interpreter/computer.rs b/src/interpreter/computer.rs index 28af901..cff82e6 100644 --- a/src/interpreter/computer.rs +++ b/src/interpreter/computer.rs @@ -257,5 +257,18 @@ impl fmt::Display for Computer { #[cfg(test)] mod tests { - // use super::*; + + use super::*; + + #[test] + fn test_push() { + let mut c = Computer::new(); + let val = ImmediateOperand::Word(0x1234); + c.push_stack(val).unwrap(); + + let target = PopTarget::Register(crate::register::Register::AX); + c.pop_stack(target).unwrap(); + + assert_eq!(val, c.regs.read(crate::register::Register::AX)) + } } diff --git a/src/interpreter/memory.rs b/src/interpreter/memory.rs index 2406a5a..460d68f 100644 --- a/src/interpreter/memory.rs +++ b/src/interpreter/memory.rs @@ -38,7 +38,7 @@ impl Memory { .get((idx + 1) as usize) .ok_or(InterpreterError::MemoryOutOfBound(idx))? .to_owned(); - Ok(Word::from_be_bytes([b1, b2])) + Ok(Word::from_be_bytes([b2, b1])) } /// Write an [`ImmediateOperand`] to a memory location indexed by a [`MemoryIndex`]. diff --git a/src/interpreter/register.rs b/src/interpreter/register.rs index 025fe6e..857fd33 100644 --- a/src/interpreter/register.rs +++ b/src/interpreter/register.rs @@ -31,7 +31,7 @@ impl Register { /// Decrement stack pointer pub fn push(&mut self) -> Result<(), InterpreterError> { - if self.sp <= 2 { + if self.sp < 2 { return Err(InterpreterError::InvalidRegisterState(*self)); } else { self.sp -= 2; @@ -41,7 +41,7 @@ impl Register { /// Increment stack pointer pub fn pop(&mut self) -> Result<(), InterpreterError> { - if self.sp >= 0xffff - 2 { + if self.sp > 0xffff - 2 { return Err(InterpreterError::InvalidRegisterState(*self)); } else { self.sp += 2;