Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement imul_r16_rm16 #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions x86/src/ops/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,32 +808,44 @@ pub fn imul_rm8(cpu: &mut CPU, mem: Mem, instr: &Instruction) {
cpu.regs.set16(Register::AX, res);
}

fn imul_trunc(x: i32, y: i32, _flags: &mut Flags) -> i32 {
fn imul_trunc_32(x: i32, y: i32, _flags: &mut Flags) -> i32 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at functions like this, which are generic over the types of integers:

pub(crate) fn and<I: Int>(x: I, y: I, flags: &mut Flags) -> I {

In particular that will let the 16-bit and 32-bit versions will share the (TODO) flag-setting code.

...Also, if your program is calling imul, it might need those flags set, so I filed #57 with some notes.

// TODO: flags.
x.wrapping_mul(y)
}

pub fn imul_r32_rm32(cpu: &mut CPU, mem: Mem, instr: &Instruction) {
let x = cpu.regs.get32(instr.op0_register()) as i32;
let y = op1_rm32(cpu, mem, instr) as i32;
let value = imul_trunc(x, y, &mut cpu.flags);
let value = imul_trunc_32(x, y, &mut cpu.flags);
cpu.regs.set32(instr.op0_register(), value as u32);
}

pub fn imul_r32_rm32_imm32(cpu: &mut CPU, mem: Mem, instr: &Instruction) {
let x = op1_rm32(cpu, mem, instr) as i32;
let y = instr.immediate32() as i32;
let value = imul_trunc(x, y, &mut cpu.flags);
let value = imul_trunc_32(x, y, &mut cpu.flags);
cpu.regs.set32(instr.op0_register(), value as u32);
}

pub fn imul_r32_rm32_imm8(cpu: &mut CPU, mem: Mem, instr: &Instruction) {
let x = op1_rm32(cpu, mem, instr) as i32;
let y = instr.immediate8to32();
let value = imul_trunc(x, y, &mut cpu.flags);
let value = imul_trunc_32(x, y, &mut cpu.flags);
cpu.regs.set32(instr.op0_register(), value as u32);
}

fn imul_trunc_16(x: i16, y: i16, _flags: &mut Flags) -> i16 {
// TODO: flags.
x.wrapping_mul(y)
}

pub fn imul_r16_rm16(cpu: &mut CPU, mem: Mem, instr: &Instruction) {
let x = cpu.regs.get16(instr.op0_register()) as i16;
let y = op1_rm16(cpu, mem, instr) as i16;
let value = imul_trunc_16(x, y, &mut cpu.flags);
cpu.regs.set16(instr.op0_register(), value as u16);
}

pub fn idiv_rm32(cpu: &mut CPU, mem: Mem, instr: &Instruction) {
let x = get_edx_eax(cpu) as i64;
let y = rm32(cpu, mem, instr).get() as i32 as i64;
Expand Down
1 change: 1 addition & 0 deletions x86/src/ops/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ pub unsafe fn init_op_tab() {
OP_TAB[iced_x86::Code::Imul_r32_rm32 as usize] = Some(imul_r32_rm32);
OP_TAB[iced_x86::Code::Imul_r32_rm32_imm32 as usize] = Some(imul_r32_rm32_imm32);
OP_TAB[iced_x86::Code::Imul_r32_rm32_imm8 as usize] = Some(imul_r32_rm32_imm8);
OP_TAB[iced_x86::Code::Imul_r16_rm16 as usize] = Some(imul_r16_rm16);
OP_TAB[iced_x86::Code::Idiv_rm32 as usize] = Some(idiv_rm32);
OP_TAB[iced_x86::Code::Idiv_rm16 as usize] = Some(idiv_rm16);
OP_TAB[iced_x86::Code::Idiv_rm8 as usize] = Some(idiv_rm8);
Expand Down