Skip to content

Commit

Permalink
asm: add sub insn, add imm8
Browse files Browse the repository at this point in the history
  • Loading branch information
johannst committed Dec 11, 2024
1 parent 7fa0970 commit 4f60de3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod nop;
mod pop;
mod push;
mod ret;
mod sub;
mod test;
mod xor;

Expand Down Expand Up @@ -91,16 +92,22 @@ pub trait Mov<T, U> {
fn mov(&mut self, op1: T, op2: U);
}

/// Trait for [`pop`](https://www.felixcloutier.com/x86/pop) instruction kinds.
pub trait Pop<T> {
/// Emit a pop instruction.
fn pop(&mut self, op1: T);
}

/// Trait for [`push`](https://www.felixcloutier.com/x86/push) instruction kinds.
pub trait Push<T> {
/// Emit a push instruction.
fn push(&mut self, op1: T);
}

/// Trait for [`pop`](https://www.felixcloutier.com/x86/pop) instruction kinds.
pub trait Pop<T> {
/// Emit a pop instruction.
fn pop(&mut self, op1: T);
/// Trait for [`sub`](https://www.felixcloutier.com/x86/sub) instruction kinds.
pub trait Sub<T, U> {
/// Emit an sub instruction.
fn sub(&mut self, op1: T, op2: U);
}

/// Trait for [`test`](https://www.felixcloutier.com/x86/test) instruction kinds.
Expand Down
8 changes: 7 additions & 1 deletion src/insn/add.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Add;
use crate::{Asm, Imm16, MemOp, Reg16, Reg32, Reg64};
use crate::{Asm, Imm16, Imm8, MemOp, Reg16, Reg32, Reg64};

impl Add<Reg64, Reg64> for Asm {
fn add(&mut self, op1: Reg64, op2: Reg64) {
Expand All @@ -25,6 +25,12 @@ impl Add<MemOp, Reg16> for Asm {
}
}

impl Add<MemOp, Imm8> for Asm {
fn add(&mut self, op1: MemOp, op2: Imm8) {
self.encode_mi(0x83, 0, op1, op2);
}
}

impl Add<MemOp, Imm16> for Asm {
fn add(&mut self, op1: MemOp, op2: Imm16) {
self.encode_mi(0x81, 0, op1, op2);
Expand Down
14 changes: 14 additions & 0 deletions src/insn/sub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use super::Sub;
use crate::{Asm, Imm8, MemOp, Reg64};

impl Sub<Reg64, Reg64> for Asm {
fn sub(&mut self, op1: Reg64, op2: Reg64) {
self.encode_rr(&[0x29], op1, op2);
}
}

impl Sub<MemOp, Imm8> for Asm {
fn sub(&mut self, op1: MemOp, op2: Imm8) {
self.encode_mi(0x83, 5, op1, op2);
}
}

0 comments on commit 4f60de3

Please sign in to comment.