ft(interpreter): impl push/pop

This commit is contained in:
2025-06-17 10:39:49 +09:00
parent d1ea96edd8
commit 5fab099cd8
5 changed files with 116 additions and 15 deletions

View File

@@ -1,6 +1,8 @@
use crate::operands::{Byte, ImmediateOperand, Word};
use core::fmt;
use super::interpreter::InterpreterError;
#[derive(Debug, Clone, Copy)]
pub struct Register {
pub ax: AX,
@@ -20,13 +22,33 @@ impl Register {
bx: BX::new(),
cx: CX::new(),
dx: DX::new(),
sp: 0,
sp: 0xffff,
bp: 0,
si: 0,
di: 0,
}
}
/// Decrement stack pointer
pub fn push(&mut self) -> Result<(), InterpreterError> {
if self.sp <= 2 {
return Err(InterpreterError::InvalidRegisterState(*self));
} else {
self.sp -= 2;
Ok(())
}
}
/// Increment stack pointer
pub fn pop(&mut self) -> Result<(), InterpreterError> {
if self.sp >= 0xffff - 2 {
return Err(InterpreterError::InvalidRegisterState(*self));
} else {
self.sp += 2;
Ok(())
}
}
/// Read value from a [`crate::register::Register`].
pub fn read(&self, reg: crate::register::Register) -> ImmediateOperand {
match reg {