From 6ad11c26588c98eb0eceb554510d0d86f0c8dc48 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Sun, 25 Oct 2026 18:07:33 +0000 Subject: [PATCH] Implement 16-bit and 32-bit BSF and BSR instructions --- cpu.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cpu.c b/cpu.c index 3fa2efa..a7e69b3 100644 --- a/cpu.c +++ b/cpu.c @@ -4327,6 +4327,48 @@ fetch_instruction: } + case 0xBC: case 0xBD: { /* BSF/BSR r16/32, r/m16/32 */ + + uint8_t modrm = fetch8 (cpu); + int reg = (modrm >> 3) & 7; + + OperandRef rm = decode_rm_operand (cpu, modrm); + uint32_t value = cpu->has_operand_override ? operand_read32 (cpu, &rm) : operand_read16 (cpu, &rm); + + if (value == 0) { + cpu->flags |= FLAG_ZF; + } else { + + unsigned bit = op2 == 0xBC ? 0 : cpu->has_operand_override ? 31 : 15; + + if (op2 == 0xBC) { + + while (!(value & (1UL << bit))) { + ++bit; + } + + } else { + + while (!(value & (1UL << bit))) { + --bit; + } + + } + + if (cpu->has_operand_override) { + set_reg32 (cpu, reg, bit); + } else { + cpu->regs[reg] = (uint16_t) bit; + } + + cpu->flags &= (uint16_t) ~FLAG_ZF; + + } + + break; + + } + case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF: { /* BSWAP r32 */ -- 2.34.1