ft: Implement memory pointer (Mp) operand

This commit is contained in:
2025-05-25 11:00:47 +09:00
parent 6762195378
commit 73b1a99cbd
3 changed files with 63 additions and 19 deletions

View File

@@ -1,7 +1,10 @@
//! 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::register::Register;
use crate::{
disasm::{DisasmError, Disassembler},
register::Register,
};
use core::fmt;
pub type Byte = u8; // b
@@ -136,6 +139,33 @@ pub struct Pointer {
pub offset: Word,
}
impl Pointer {
pub fn new(disasm: &mut Disassembler) -> Result<Self, DisasmError> {
let byte0 = disasm
.text
.get(disasm.offset)
.ok_or(DisasmError::IndexOutOfBounds(disasm.offset))?;
let byte1 = disasm
.text
.get(disasm.offset + 1)
.ok_or(DisasmError::IndexOutOfBounds(disasm.offset + 1))?;
let byte2 = disasm
.text
.get(disasm.offset + 2)
.ok_or(DisasmError::IndexOutOfBounds(disasm.offset + 2))?;
let byte3 = disasm
.text
.get(disasm.offset + 3)
.ok_or(DisasmError::IndexOutOfBounds(disasm.offset + 3))?;
Ok(Pointer {
raw: DWord::from_le_bytes([*byte0, *byte1, *byte2, *byte3]),
segment: disasm.parse_word(),
offset: disasm.parse_word(),
})
}
}
impl std::fmt::Display for Pointer {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "[{:#04x}] ({}:{})", self.raw, self.segment, self.offset)