diff --git a/src/interpreter/memory.rs b/src/interpreter/memory.rs index e8e8eea..ac7e90b 100644 --- a/src/interpreter/memory.rs +++ b/src/interpreter/memory.rs @@ -20,14 +20,18 @@ impl Memory { /// Safely writes a [`ImmediateOperand`] into an index of memory. /// Warning: Does access at `addr`, not `DS:addr`! pub fn write_raw(&mut self, addr: Word, val: ImmediateOperand) -> Result<(), InterpreterError> { - if (addr + 1) as usize > MEMORY_SIZE { - return Err(InterpreterError::MemoryOutOfBound(addr)); - } else { - match val { - ImmediateOperand::Byte(b) => { + match val { + ImmediateOperand::Byte(b) => { + if addr as usize > MEMORY_SIZE { + return Err(InterpreterError::MemoryOutOfBound(addr)); + } else { self.raw[addr as usize] = b; } - ImmediateOperand::Word(w) => { + } + ImmediateOperand::Word(w) => { + if (addr + 1) as usize > MEMORY_SIZE { + return Err(InterpreterError::MemoryOutOfBound(addr)); + } else { let [low, high] = w.to_le_bytes(); self.raw[addr as usize] = low; self.raw[(addr + 1) as usize] = high;