diff --git a/src/arch/x86_64/encode.rs b/src/arch/x86_64/encode.rs index 978ab44..b3674bb 100644 --- a/src/arch/x86_64/encode.rs +++ b/src/arch/x86_64/encode.rs @@ -172,7 +172,7 @@ impl Code { self.bytes.push(0xc3); } - pub fn sub(&mut self, dst: Reg, src: impl Into) -> ERes { + fn add_sub(&mut self, dst: Reg, src: impl Into, ext: u8) -> ERes { let mut src = src.into(); let mut width = src.width_signed()?; let dst_width = dst.width().min(Width::B32); @@ -198,11 +198,19 @@ impl Code { width = dst_width; } - self.bytes.push(modrm(0b11, 0b101, dst.base())); + self.bytes.push(modrm(0b11, ext, dst.base())); self.imm(src, width); Ok(()) } + pub fn add(&mut self, dst: Reg, src: impl Into) -> ERes { + self.add_sub(dst, src, 0) + } + + pub fn sub(&mut self, dst: Reg, src: impl Into) -> ERes { + self.add_sub(dst, src, 5) + } + fn prefix16(&mut self, width: impl Into) { if width.into() == Width::B16 { self.bytes.push(0x66); diff --git a/src/arch/x86_64/test/asm/mod.rs b/src/arch/x86_64/test/asm/mod.rs index e84f419..9cd7a02 100644 --- a/src/arch/x86_64/test/asm/mod.rs +++ b/src/arch/x86_64/test/asm/mod.rs @@ -37,9 +37,15 @@ fn mov() { } #[test] -fn sub() { +fn add_sub() { let c = &mut TestCtx::new("mov"); + for dst in regs() { + for src in imms() { + eq(c, format!("add {dst}, {src}"), |c| c.add(dst, src)) + } + } + for dst in regs() { for src in imms() { eq(c, format!("sub {dst}, {src}"), |c| c.sub(dst, src)) diff --git a/test/nasm_cache/mov b/test/nasm_cache/mov index b65b4cb..9e836a3 100644 Binary files a/test/nasm_cache/mov and b/test/nasm_cache/mov differ