This commit is contained in:
2026-06-16 02:29:56 -04:00
parent 84e184518f
commit 4e06e474ea
3 changed files with 17 additions and 3 deletions
+10 -2
View File
@@ -172,7 +172,7 @@ impl Code {
self.bytes.push(0xc3); self.bytes.push(0xc3);
} }
pub fn sub(&mut self, dst: Reg, src: impl Into<Imm>) -> ERes { fn add_sub(&mut self, dst: Reg, src: impl Into<Imm>, ext: u8) -> ERes {
let mut src = src.into(); let mut src = src.into();
let mut width = src.width_signed()?; let mut width = src.width_signed()?;
let dst_width = dst.width().min(Width::B32); let dst_width = dst.width().min(Width::B32);
@@ -198,11 +198,19 @@ impl Code {
width = dst_width; width = dst_width;
} }
self.bytes.push(modrm(0b11, 0b101, dst.base())); self.bytes.push(modrm(0b11, ext, dst.base()));
self.imm(src, width); self.imm(src, width);
Ok(()) Ok(())
} }
pub fn add(&mut self, dst: Reg, src: impl Into<Imm>) -> ERes {
self.add_sub(dst, src, 0)
}
pub fn sub(&mut self, dst: Reg, src: impl Into<Imm>) -> ERes {
self.add_sub(dst, src, 5)
}
fn prefix16(&mut self, width: impl Into<Width>) { fn prefix16(&mut self, width: impl Into<Width>) {
if width.into() == Width::B16 { if width.into() == Width::B16 {
self.bytes.push(0x66); self.bytes.push(0x66);
+7 -1
View File
@@ -37,9 +37,15 @@ fn mov() {
} }
#[test] #[test]
fn sub() { fn add_sub() {
let c = &mut TestCtx::new("mov"); 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 dst in regs() {
for src in imms() { for src in imms() {
eq(c, format!("sub {dst}, {src}"), |c| c.sub(dst, src)) eq(c, format!("sub {dst}, {src}"), |c| c.sub(dst, src))
Binary file not shown.