Implement 16-bit and 32-bit BSF and BSR instructions
authorRobert Pengelly <robertapengelly@hotmail.com>
Sun, 25 Oct 2026 18:07:33 +0000 (18:07 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sun, 25 Oct 2026 18:07:33 +0000 (18:07 +0000)
cpu.c

diff --git a/cpu.c b/cpu.c
index 3fa2efa694e39fba831ac2252aadfb02bce8ef23..a7e69b37a02699b02265162054599a71d4af96cc 100644 (file)
--- 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 */