From ab5711a77e2a6872ca1392c1dbc2f545f7d3ab3b Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Mon, 27 Feb 2023 21:16:40 +0100 Subject: Add ADD, DEC, TEST, RET instructions --- src/insn.rs | 16 ++++++++++++++++ src/insn/add.rs | 7 +++++++ src/insn/dec.rs | 7 +++++++ src/insn/ret.rs | 7 +++++++ src/insn/test.rs | 7 +++++++ src/lib.rs | 24 ++++++++++++++++++++++-- src/prelude.rs | 2 +- 7 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/insn/add.rs create mode 100644 src/insn/dec.rs create mode 100644 src/insn/ret.rs create mode 100644 src/insn/test.rs (limited to 'src') diff --git a/src/insn.rs b/src/insn.rs index c0bcc4e..2bfaf18 100644 --- a/src/insn.rs +++ b/src/insn.rs @@ -1,5 +1,21 @@ +mod add; +mod dec; mod mov; +mod ret; +mod test; + +pub trait Add { + fn add(&mut self, op1: T, op2: U); +} + +pub trait Dec { + fn dec(&mut self, op1: T); +} pub trait Mov { fn mov(&mut self, op1: T, op2: U); } + +pub trait Test { + fn test(&mut self, op1: T, op2: U); +} diff --git a/src/insn/add.rs b/src/insn/add.rs new file mode 100644 index 0000000..b8456c4 --- /dev/null +++ b/src/insn/add.rs @@ -0,0 +1,7 @@ +use crate::prelude::*; + +impl Add for Asm { + fn add(&mut self, op1: Reg64, op2: Reg64) { + self.encode_rr(0x01, op1, op2); + } +} diff --git a/src/insn/dec.rs b/src/insn/dec.rs new file mode 100644 index 0000000..7de5a54 --- /dev/null +++ b/src/insn/dec.rs @@ -0,0 +1,7 @@ +use crate::prelude::*; + +impl Dec for Asm { + fn dec(&mut self, op1: Reg64) { + self.encode_r(0xff, 1, op1); + } +} diff --git a/src/insn/ret.rs b/src/insn/ret.rs new file mode 100644 index 0000000..74086d8 --- /dev/null +++ b/src/insn/ret.rs @@ -0,0 +1,7 @@ +use crate::Asm; + +impl Asm { + pub fn ret(&mut self) { + self.emit(&[0xc3]); + } +} diff --git a/src/insn/test.rs b/src/insn/test.rs new file mode 100644 index 0000000..1319e3f --- /dev/null +++ b/src/insn/test.rs @@ -0,0 +1,7 @@ +use crate::prelude::*; + +impl Test for Asm { + fn test(&mut self, op1: Reg64, op2: Reg64) { + self.encode_rr(0x85, op1, op2); + } +} diff --git a/src/lib.rs b/src/lib.rs index e38da14..27164ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,8 +107,8 @@ impl Asm { Self: EncodeRI, { // MI operand encoding. - // op1 -> modrm.rm - // op2 -> modrm.reg + // op1 -> modrm.rm + // opc extension -> modrm.reg let modrm = modrm( 0b11, /* mod */ opc_ext, /* reg */ @@ -123,6 +123,26 @@ impl Asm { self.emit(op2.bytes()); } + fn encode_r(&mut self, opc: u8, opc_ext: u8, op1: T) + where + Self: EncodeRI, + { + // M operand encoding. + // op1 -> modrm.rm + // opc extension -> modrm.reg + let modrm = modrm( + 0b11, /* mod */ + opc_ext, /* reg */ + op1.idx(), /* rm */ + ); + + let prefix = >::legacy_prefix(); + let rex = >::rex(op1); + + self.emit_optional(&[prefix, rex]); + self.emit(&[opc, modrm]); + } + fn encode_mr(&mut self, opc: u8, op1: MemOp, op2: T) where Self: EncodeMR, diff --git a/src/prelude.rs b/src/prelude.rs index d1cd794..bca972f 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,4 +3,4 @@ pub use crate::MemOp; pub use crate::{Imm16, Imm32, Imm64, Imm8}; pub use crate::{Reg16, Reg32, Reg64, Reg8}; -pub use crate::insn::{Add, Mov}; +pub use crate::insn::{Add, Dec, Mov, Test}; -- cgit v1.2.3