ft(interpreter): generalize binary operations
This commit is contained in:
@@ -7,7 +7,7 @@ use crate::register::SegmentRegister;
|
||||
|
||||
use crate::{disasm::DisasmError, register::Register};
|
||||
use core::fmt;
|
||||
use std::ops::{Add, Sub};
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
|
||||
pub type Byte = u8; // b
|
||||
pub type IByte = i8; // used for displacements of memory access
|
||||
@@ -88,6 +88,44 @@ impl Sub for ImmediateOperand {
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for ImmediateOperand {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: Self) -> Self {
|
||||
match self {
|
||||
ImmediateOperand::Byte(lhsb) => match other {
|
||||
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_mul(rhsb)),
|
||||
_ => panic!("Cannot multiply Byte with Word"),
|
||||
},
|
||||
ImmediateOperand::Word(lhsw) => match other {
|
||||
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_mul(rhsw)),
|
||||
ImmediateOperand::Byte(rhsb) => {
|
||||
ImmediateOperand::Word(lhsw.wrapping_mul(rhsb as Word))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div for ImmediateOperand {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: Self) -> Self {
|
||||
match self {
|
||||
ImmediateOperand::Byte(lhsb) => match other {
|
||||
ImmediateOperand::Byte(rhsb) => ImmediateOperand::Byte(lhsb.wrapping_div(rhsb)),
|
||||
_ => panic!("Cannot divide Byte with Word"),
|
||||
},
|
||||
ImmediateOperand::Word(lhsw) => match other {
|
||||
ImmediateOperand::Word(rhsw) => ImmediateOperand::Word(lhsw.wrapping_div(rhsw)),
|
||||
ImmediateOperand::Byte(rhsb) => {
|
||||
ImmediateOperand::Word(lhsw.wrapping_div(rhsb as Word))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ImmediateOperand {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
|
||||
Reference in New Issue
Block a user