Files
8086-rs/src/operands.rs
Marco Thomas c396d33f76 fix: align pointer parsing with spec
Previously pointer parsing was completely wrong.
Now split into Pointer32 for immediates with
segment;offset and Pointer16 for short jumps, which
use DS or ES as segment and the Pointer16 value
as offset.
2025-05-28 13:31:14 +09:00

183 lines
5.6 KiB
Rust

//! All types which a Mnemonic can have as some kind of operand.
//! This includes things such as immediates, ModRM byte targets, etc. etc.
use crate::{disasm::DisasmError, register::Register};
use core::fmt;
pub type Byte = u8; // b
pub type IByte = i8; // used for displacements of memory access
pub type Word = u16; // w or v
pub type IWord = i16; // used for displacement of memory access
pub type DWord = u32;
#[derive(Debug, Clone)]
/// Encodes either Byte- or Word-sized operands.
/// Also sometimes used to decide if an instruction is Byte- or Word-sized,
/// which is usually indicated by using a value of 0 and the disregarding
/// the value when read.
pub enum Operand {
Byte(Byte),
Word(Word),
}
impl fmt::Display for Operand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Byte(byte) => write!(f, "{}", byte),
Self::Word(word) => write!(f, "{}", word),
}
}
}
impl fmt::LowerHex for Operand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Byte(b) => fmt::LowerHex::fmt(b, f),
Self::Word(v) => fmt::LowerHex::fmt(v, f),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// ModRM byte can either target a memory location or some register.
pub enum ModRmTarget {
Memory(MemoryIndex),
Register(Register),
}
impl std::fmt::Display for ModRmTarget {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Memory(idx) => write!(f, "{}", idx),
Self::Register(reg) => write!(f, "{}", reg),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Memory displacements are signed versions of Byte and Word operands.
/// Encodes either Byte- or Word-sized operands.
pub enum Displacement {
IByte(IByte),
IWord(IWord),
}
impl fmt::LowerHex for Displacement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IByte(b) => fmt::LowerHex::fmt(b, f),
Self::IWord(v) => fmt::LowerHex::fmt(v, f),
}
}
}
impl std::fmt::Display for Displacement {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::IByte(b) => {
if *b > 0 {
write!(f, " + {:#x}", b)
} else {
write!(f, " - {:#x}", b * -1)
}
}
Self::IWord(w) => {
if *w > 0 {
write!(f, " + {:#x}", w)
} else {
write!(f, " - {:#x}", w * -1)
}
}
}
}
}
/// A memory index operand is usually created by ModRM bytes or words.
/// e.g. [bx+si]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemoryIndex {
pub base: Option<Register>,
pub index: Option<Register>,
pub displacement: Option<Displacement>,
}
impl fmt::Display for MemoryIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.base {
Some(base) => match &self.index {
Some(index) => match &self.displacement {
Some(displacement) => {
write!(f, "[{} + {}{}]", base, index, displacement)
}
None => write!(f, "[{} + {}]", base, index),
},
None => match &self.displacement {
Some(displacement) => write!(f, "[{}{}]", base, displacement),
None => write!(f, "[{}]", base),
},
},
None => match &self.index {
Some(index) => match &self.displacement {
Some(displacement) => write!(f, "[{}{}]", index, displacement),
None => write!(f, "[{}]", index),
},
None => match &self.displacement {
Some(displacement) => write!(f, "[{:#x}]", displacement),
None => panic!("Memory Index without base, index and displacement"),
},
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// 16-bit pointer for access, usually with a [`SegmentRegister`] as segment
/// and [`Pointer16`] as offset.
pub struct Pointer16 {
pub word: Word,
}
impl std::fmt::Display for Pointer16 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "ptr [{:#04x}]", self.word)
}
}
impl TryFrom<ModRmTarget> for Pointer16 {
type Error = DisasmError;
fn try_from(target: ModRmTarget) -> Result<Self, Self::Error> {
match target {
ModRmTarget::Memory(mem) => match mem.displacement {
Some(disp) => match disp {
Displacement::IWord(word) => Ok(Pointer16 { word: word as Word }),
_ => {
return Err(DisasmError::IllegalOperand("Word expected".into()));
}
},
_ => {
return Err(DisasmError::IllegalOperand("Displacement expected".into()));
}
},
_ => {
return Err(DisasmError::IllegalOperand("MemoryIndex expected".into()));
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// 32-bit segment:offset pointer for long jumps.
/// Both [`Word`]s are immediately encoded after the instruction
pub struct Pointer32 {
pub raw: DWord,
pub segment: Word,
pub offset: Word,
}
impl std::fmt::Display for Pointer32 {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:#04x}:{:#04x}", self.segment, self.offset)
}
}