ft(interpreter): impl push/pop
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user