From 2699292790476eccd726fc5dae179b3a688a1468 Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Fri, 6 Dec 2024 23:32:58 +0100 Subject: asm: add initial support for memory operand only instructions * add dec, inc instruction for with memory operand --- src/asm.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 2 deletions(-) (limited to 'src/asm.rs') diff --git a/src/asm.rs b/src/asm.rs index 7deeb70..711de8a 100644 --- a/src/asm.rs +++ b/src/asm.rs @@ -154,6 +154,52 @@ impl Asm { self.emit(&[opc, modrm]); } + /// Encode a memory operand instruction. + pub(crate) fn encode_m(&mut self, opc: u8, opc_ext: u8, op1: T) + where + Self: EncodeM, + { + let op1 = op1.mem_op(); + + // M operand encoding. + // op1 -> modrm.rm + let (mode, rm) = match op1 { + MemOp::Indirect(..) => { + assert!(!op1.base().need_sib() && !op1.base().is_pc_rel()); + (0b00, op1.base().idx()) + } + MemOp::IndirectDisp(..) => { + assert!(!op1.base().need_sib()); + (0b10, op1.base().idx()) + } + MemOp::IndirectBaseIndex(..) => { + assert!(!op1.base().is_pc_rel()); + // Using rsp as index register is interpreted as just base w/o offset. + // https://wiki.osdev.org/X86-64_Instruction_Encoding#32.2F64-bit_addressing_2 + // Disallow this case, as guard for the user. + assert!(!matches!(op1.index(), Reg64::rsp)); + (0b00, 0b100) + } + }; + + let modrm = modrm( + mode, /* mode */ + opc_ext, /* reg */ + rm, /* rm */ + ); + + let prefix = >::legacy_prefix(); + let rex = >::rex(&op1); + + self.emit_optional(&[prefix, rex]); + self.emit(&[opc, modrm]); + match op1 { + MemOp::Indirect(..) => {} + MemOp::IndirectDisp(_, disp) => self.emit(&disp.to_ne_bytes()), + MemOp::IndirectBaseIndex(base, index) => self.emit(&[sib(0, index.idx(), base.idx())]), + } + } + /// Encode a memory-immediate instruction. pub(crate) fn encode_mi(&mut self, opc: u8, opc_ext: u8, op1: MemOp, op2: T) where @@ -330,7 +376,7 @@ pub(crate) trait EncodeMR { } fn rex(op1: &MemOp, op2: T) -> Option { - if op2.need_rex() || (op1.base().is_ext()) { + if op2.need_rex() || op1.base().is_ext() || op1.index().is_ext() { Some(rex( op2.rexw(), op2.idx(), @@ -359,7 +405,7 @@ pub(crate) trait EncodeMI { } fn rex(op1: &MemOp) -> Option { - if op1.base().is_ext() { + if op1.base().is_ext() || op1.index().is_ext() { Some(rex(false, 0, op1.index().idx(), op1.base().idx())) } else { None @@ -374,3 +420,40 @@ impl EncodeMI for Asm { } } impl EncodeMI for Asm {} + +/// Encode helper for memory operand instructions. +pub(crate) trait EncodeM { + fn legacy_prefix() -> Option { + None + } + + fn rex(op1: &MemOp) -> Option { + if op1.base().is_ext() || op1.index().is_ext() || Self::is_64bit() { + Some(rex( + Self::is_64bit(), + 0, + op1.index().idx(), + op1.base().idx(), + )) + } else { + None + } + } + + fn is_64bit() -> bool { + false + } +} + +impl EncodeM for Asm {} +impl EncodeM for Asm { + fn legacy_prefix() -> Option { + Some(0x66) + } +} +impl EncodeM for Asm {} +impl EncodeM for Asm { + fn is_64bit() -> bool { + true + } +} -- cgit v1.2.3