[FAST486]
[reactos.git] / lib / fast486 / extraops.c
index a7335d2..ba2b5bd 100644 (file)
@@ -53,10 +53,10 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
-    NULL, // TODO: OPCODE 0x10 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x11 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x12 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0x13 NOT IMPLEMENTED
+    NULL, // Invalid
+    NULL, // Invalid
+    NULL, // Invalid
+    NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
     NULL, // Invalid
@@ -201,34 +201,34 @@ Fast486ExtendedHandlers[FAST486_NUM_OPCODE_HANDLERS] =
     Fast486ExtOpcodePopFs,
     NULL, // Invalid
     Fast486ExtOpcodeBitTest,
-    NULL, // TODO: OPCODE 0xA4 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xA5 NOT IMPLEMENTED
+    Fast486ExtOpcodeShld,
+    Fast486ExtOpcodeShld,
     NULL, // Invalid
     NULL, // Invalid
     Fast486ExtOpcodePushGs,
     Fast486ExtOpcodePopGs,
     NULL, // Invalid
     Fast486ExtOpcodeBts,
-    NULL, // TODO: OPCODE 0xAC NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xAD NOT IMPLEMENTED
+    Fast486ExtOpcodeShrd,
+    Fast486ExtOpcodeShrd,
     NULL, // TODO: OPCODE 0xAE NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xAF NOT IMPLEMENTED
+    Fast486ExtOpcodeImul,
     Fast486ExtOpcodeCmpXchgByte,
     Fast486ExtOpcodeCmpXchg,
-    NULL, // TODO: OPCODE 0xB2 NOT IMPLEMENTED
+    Fast486ExtOpcodeLss,
     Fast486ExtOpcodeBtr,
-    NULL, // TODO: OPCODE 0xB4 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB5 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB6 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB7 NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xB8 NOT IMPLEMENTED
+    Fast486ExtOpcodeLfsLgs,
+    Fast486ExtOpcodeLfsLgs,
+    Fast486ExtOpcodeMovzxByte,
+    Fast486ExtOpcodeMovzxWord,
+    NULL, // Invalid
     Fast486OpcodeGroup0FB9,
     Fast486OpcodeGroup0FBA,
     Fast486ExtOpcodeBtc,
-    NULL, // TODO: OPCODE 0xBC NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xBD NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xBE NOT IMPLEMENTED
-    NULL, // TODO: OPCODE 0xBF NOT IMPLEMENTED
+    Fast486ExtOpcodeBsf,
+    Fast486ExtOpcodeBsr,
+    Fast486ExtOpcodeMovsxByte,
+    Fast486ExtOpcodeMovsxWord,
     Fast486ExtOpcodeXaddByte,
     Fast486ExtOpcodeXadd,
     NULL, // Invalid
@@ -427,7 +427,7 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeLoadControlReg)
         Fast486Exception(State, FAST486_EXCEPTION_UD);
         return FALSE;
     }
-    
+
     if (ModRegRm.Register != 0)
     {
         /* CR2 and CR3 and are stored in array indexes 1 and 2 */
@@ -601,6 +601,98 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBitTest)
     return TRUE;
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeShld)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    UCHAR Count;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT((Opcode & 0xFE) == 0xA4);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (Opcode == 0xA4)
+    {
+        /* Fetch the count */
+        if (!Fast486FetchByte(State, &Count))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        /* The count is in CL */
+        Count = State->GeneralRegs[FAST486_REG_ECX].LowByte;
+    }
+
+    /* Normalize the count */
+    if (OperandSize) Count &= 0x1F;
+    else Count &= 0x0F;
+
+    /* Do nothing if the count is zero */
+    if (Count == 0) return TRUE;
+
+    if (OperandSize)
+    {
+        ULONG Source, Destination, Result;
+
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Source, &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Calculate the result */
+        Result = (Destination << Count) | (Source >> (32 - Count));
+
+        /* Update flags */
+        State->Flags.Cf = (Destination >> (32 - Count)) & 1;
+        if (Count == 1) State->Flags.Of = (Result & SIGN_FLAG_LONG)
+                                          != (Destination & SIGN_FLAG_LONG);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_LONG) != 0);
+        State->Flags.Pf = Fast486CalculateParity(Result);
+
+        /* Write back the result */
+        return Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Result);
+    }
+    else
+    {
+        USHORT Source, Destination, Result;
+
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Source, &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Calculate the result */
+        Result = (Destination << Count) | (Source >> (16 - Count));
+
+        /* Update flags */
+        State->Flags.Cf = (Destination >> (16 - Count)) & 1;
+        if (Count == 1) State->Flags.Of = (Result & SIGN_FLAG_WORD)
+                                          != (Destination & SIGN_FLAG_WORD);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_WORD) != 0);
+        State->Flags.Pf = Fast486CalculateParity(Result);
+
+        /* Write back the result */
+        return Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Result);
+    }
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodePushGs)
 {
     /* Call the internal API */
@@ -712,6 +804,162 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBts)
     return TRUE;
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeShrd)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    UCHAR Count;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT((Opcode & 0xFE) == 0xAC);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (Opcode == 0xAC)
+    {
+        /* Fetch the count */
+        if (!Fast486FetchByte(State, &Count))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        /* The count is in CL */
+        Count = State->GeneralRegs[FAST486_REG_ECX].LowByte;
+    }
+
+    /* Normalize the count */
+    if (OperandSize) Count &= 0x1F;
+    else Count &= 0x0F;
+
+    /* Do nothing if the count is zero */
+    if (Count == 0) return TRUE;
+
+    if (OperandSize)
+    {
+        ULONG Source, Destination, Result;
+
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Source, &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Calculate the result */
+        Result = (Destination >> Count) | (Source << (32 - Count));
+
+        /* Update flags */
+        State->Flags.Cf = (Destination >> (Count - 1)) & 1;
+        if (Count == 1) State->Flags.Of = (Result & SIGN_FLAG_LONG)
+                                          != (Destination & SIGN_FLAG_LONG);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_LONG) != 0);
+        State->Flags.Pf = Fast486CalculateParity(Result);
+
+        /* Write back the result */
+        return Fast486WriteModrmDwordOperands(State, &ModRegRm, FALSE, Result);
+    }
+    else
+    {
+        USHORT Source, Destination, Result;
+
+        if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Source, &Destination))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Calculate the result */
+        Result = (Destination >> Count) | (Source << (16 - Count));
+
+        /* Update flags */
+        State->Flags.Cf = (Result >> (Count - 1)) & 1;
+        if (Count == 1) State->Flags.Of = (Result & SIGN_FLAG_WORD)
+                                          != (Destination & SIGN_FLAG_WORD);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_WORD) != 0);
+        State->Flags.Pf = Fast486CalculateParity(Result);
+
+        /* Write back the result */
+        return Fast486WriteModrmWordOperands(State, &ModRegRm, FALSE, Result);
+    }
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeImul)
+{
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (OperandSize)
+    {
+        LONG Source, Destination;
+        LONGLONG Result;
+
+        /* Read the operands */
+        if (!Fast486ReadModrmDwordOperands(State,
+                                           &ModRegRm,
+                                           (PULONG)&Destination,
+                                           (PULONG)&Source))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Calculate the result */
+        Result = (LONGLONG)Source * (LONGLONG)Destination;
+
+        /* Update the flags */
+        State->Flags.Cf = State->Flags.Of = ((Result < -2147483648LL) || (Result > 2147483647LL));
+
+        /* Write back the result */
+        return Fast486WriteModrmDwordOperands(State, &ModRegRm, TRUE, (ULONG)((LONG)Result));
+    }
+    else
+    {
+        SHORT Source, Destination;
+        LONG Result;
+
+        /* Read the operands */
+        if (!Fast486ReadModrmWordOperands(State,
+                                          &ModRegRm,
+                                          (PUSHORT)&Destination,
+                                          (PUSHORT)&Source))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Calculate the result */
+        Result = (LONG)Source * (LONG)Destination;
+
+        /* Update the flags */
+        State->Flags.Cf = State->Flags.Of = ((Result < -32768) || (Result > 32767));
+
+        /* Write back the result */
+        return Fast486WriteModrmWordOperands(State, &ModRegRm, TRUE, (USHORT)((SHORT)Result));
+    }
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchgByte)
 {
     FAST486_MOD_REG_RM ModRegRm;
@@ -739,12 +987,12 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchgByte)
     Result = Accumulator - Destination;
 
     /* Update the flags */
-    State->Flags.Cf = Accumulator < Destination;
+    State->Flags.Cf = (Accumulator < Destination);
     State->Flags.Of = ((Accumulator & SIGN_FLAG_BYTE) != (Destination & SIGN_FLAG_BYTE))
                       && ((Accumulator & SIGN_FLAG_BYTE) != (Result & SIGN_FLAG_BYTE));
     State->Flags.Af = (Accumulator & 0x0F) < (Destination & 0x0F);
-    State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-    State->Flags.Sf = (Result & SIGN_FLAG_BYTE) ? TRUE : FALSE;
+    State->Flags.Zf = (Result == 0);
+    State->Flags.Sf = ((Result & SIGN_FLAG_BYTE) != 0);
     State->Flags.Pf = Fast486CalculateParity(Result);
 
     if (State->Flags.Zf)
@@ -795,12 +1043,12 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchg)
         Result = Accumulator - Destination;
 
         /* Update the flags */
-        State->Flags.Cf = Accumulator < Destination;
+        State->Flags.Cf = (Accumulator < Destination);
         State->Flags.Of = ((Accumulator & SIGN_FLAG_LONG) != (Destination & SIGN_FLAG_LONG))
                           && ((Accumulator & SIGN_FLAG_LONG) != (Result & SIGN_FLAG_LONG));
         State->Flags.Af = (Accumulator & 0x0F) < (Destination & 0x0F);
-        State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-        State->Flags.Sf = (Result & SIGN_FLAG_LONG) ? TRUE : FALSE;
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_LONG) != 0);
         State->Flags.Pf = Fast486CalculateParity(Result);
 
         if (State->Flags.Zf)
@@ -830,12 +1078,12 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchg)
         Result = Accumulator - Destination;
 
         /* Update the flags */
-        State->Flags.Cf = Accumulator < Destination;
+        State->Flags.Cf = (Accumulator < Destination);
         State->Flags.Of = ((Accumulator & SIGN_FLAG_WORD) != (Destination & SIGN_FLAG_WORD))
                           && ((Accumulator & SIGN_FLAG_WORD) != (Result & SIGN_FLAG_WORD));
         State->Flags.Af = (Accumulator & 0x0F) < (Destination & 0x0F);
-        State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-        State->Flags.Sf = (Result & SIGN_FLAG_WORD) ? TRUE : FALSE;
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_WORD) != 0);
         State->Flags.Pf = Fast486CalculateParity(Result);
 
         if (State->Flags.Zf)
@@ -854,6 +1102,74 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeCmpXchg)
     return TRUE;
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeLss)
+{
+    UCHAR FarPointer[6];
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xB2);
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (!ModRegRm.Memory)
+    {
+        /* Invalid */
+        Fast486Exception(State, FAST486_EXCEPTION_UD);
+        return FALSE;
+    }
+
+    if (!Fast486ReadMemory(State,
+                           (State->PrefixFlags & FAST486_PREFIX_SEG)
+                           ? State->SegmentOverride : FAST486_REG_DS,
+                           ModRegRm.MemoryAddress,
+                           FALSE,
+                           FarPointer,
+                           OperandSize ? 6 : 4))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (OperandSize)
+    {
+        ULONG Offset = *((PULONG)FarPointer);
+        USHORT Segment = *((PUSHORT)&FarPointer[sizeof(ULONG)]);
+
+        /* Set the register to the offset */
+        State->GeneralRegs[ModRegRm.Register].Long = Offset;
+
+        /* Load the segment */
+        return Fast486LoadSegment(State,
+                                  FAST486_REG_SS,
+                                  Segment);
+    }
+    else
+    {
+        USHORT Offset = *((PUSHORT)FarPointer);
+        USHORT Segment = *((PUSHORT)&FarPointer[sizeof(USHORT)]);
+
+        /* Set the register to the offset */
+        State->GeneralRegs[ModRegRm.Register].LowWord = Offset;
+
+        /* Load the segment */
+        return Fast486LoadSegment(State,
+                                  FAST486_REG_SS,
+                                  Segment);
+    }
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBtr)
 {
     BOOLEAN OperandSize, AddressSize;
@@ -945,6 +1261,140 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBtr)
     return TRUE;
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeLfsLgs)
+{
+    UCHAR FarPointer[6];
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    /* Make sure this is the right instruction */
+    ASSERT((Opcode & 0xFE) == 0xB4);
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (!ModRegRm.Memory)
+    {
+        /* Invalid */
+        Fast486Exception(State, FAST486_EXCEPTION_UD);
+        return FALSE;
+    }
+
+    if (!Fast486ReadMemory(State,
+                           (State->PrefixFlags & FAST486_PREFIX_SEG)
+                           ? State->SegmentOverride : FAST486_REG_DS,
+                           ModRegRm.MemoryAddress,
+                           FALSE,
+                           FarPointer,
+                           OperandSize ? 6 : 4))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    if (OperandSize)
+    {
+        ULONG Offset = *((PULONG)FarPointer);
+        USHORT Segment = *((PUSHORT)&FarPointer[sizeof(ULONG)]);
+
+        /* Set the register to the offset */
+        State->GeneralRegs[ModRegRm.Register].Long = Offset;
+
+        /* Load the segment */
+        return Fast486LoadSegment(State,
+                                  (Opcode == 0xB4)
+                                  ? FAST486_REG_FS : FAST486_REG_GS,
+                                  Segment);
+    }
+    else
+    {
+        USHORT Offset = *((PUSHORT)FarPointer);
+        USHORT Segment = *((PUSHORT)&FarPointer[sizeof(USHORT)]);
+
+        /* Set the register to the offset */
+        State->GeneralRegs[ModRegRm.Register].LowWord = Offset;
+
+        /* Load the segment */
+        return Fast486LoadSegment(State,
+                                  (Opcode == 0xB4)
+                                  ? FAST486_REG_FS : FAST486_REG_GS,
+                                  Segment);
+    }
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeMovzxByte)
+{
+    UCHAR Dummy, Value;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xB6);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the operands */
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, &Value))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Write back the zero-extended value */
+    return Fast486WriteModrmDwordOperands(State,
+                                          &ModRegRm,
+                                          TRUE,
+                                          (ULONG)Value);
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeMovzxWord)
+{
+    USHORT Dummy, Value;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xB7);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the operands */
+    if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, &Value))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Write back the zero-extended value */
+    return Fast486WriteModrmDwordOperands(State,
+                                          &ModRegRm,
+                                          TRUE,
+                                          (ULONG)Value);
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBtc)
 {
     BOOLEAN OperandSize, AddressSize;
@@ -1036,6 +1486,252 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBtc)
     return TRUE;
 }
 
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBsf)
+{
+    INT i;
+    ULONG Dummy = 0, Value = 0;
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    ULONG BitNumber;
+    UINT DataSize;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xBC);
+
+    /* Get the number of bits */
+    if (OperandSize) DataSize = 32;
+    else DataSize = 16;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the value */
+    if (OperandSize)
+    {
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        if (!Fast486ReadModrmWordOperands(State,
+                                          &ModRegRm,
+                                          (PUSHORT)&Dummy,
+                                          (PUSHORT)&Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    /* Clear ZF */
+    State->Flags.Zf = FALSE;
+
+    for (i = 0; i < DataSize; i++)
+    {
+        if(Value & (1 << i))
+        {
+            /* Set ZF */
+            State->Flags.Zf = TRUE;
+
+            /* Save the bit number */
+            BitNumber = i;
+
+            /* Exit the loop */
+            break;
+        }
+    }
+
+    if (State->Flags.Zf)
+    {
+        /* Write back the result */
+        if (OperandSize)
+        {
+            if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, TRUE, BitNumber))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+        }
+        else
+        {
+            if (!Fast486WriteModrmWordOperands(State, &ModRegRm, TRUE, LOWORD(BitNumber)))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+        }
+    }
+
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeBsr)
+{
+    INT i;
+    ULONG Dummy = 0, Value = 0;
+    BOOLEAN OperandSize, AddressSize;
+    FAST486_MOD_REG_RM ModRegRm;
+    ULONG BitNumber;
+    UINT DataSize;
+
+    OperandSize = AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    TOGGLE_OPSIZE(OperandSize);
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xBD);
+
+    /* Get the number of bits */
+    if (OperandSize) DataSize = 32;
+    else DataSize = 16;
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the value */
+    if (OperandSize)
+    {
+        if (!Fast486ReadModrmDwordOperands(State, &ModRegRm, &Dummy, &Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        if (!Fast486ReadModrmWordOperands(State,
+                                          &ModRegRm,
+                                          (PUSHORT)&Dummy,
+                                          (PUSHORT)&Value))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    /* Clear ZF */
+    State->Flags.Zf = FALSE;
+
+    for (i = DataSize - 1; i >= 0; i--)
+    {
+        if(Value & (1 << i))
+        {
+            /* Set ZF */
+            State->Flags.Zf = TRUE;
+
+            /* Save the bit number */
+            BitNumber = i;
+
+            /* Exit the loop */
+            break;
+        }
+    }
+
+    if (State->Flags.Zf)
+    {
+        /* Write back the result */
+        if (OperandSize)
+        {
+            if (!Fast486WriteModrmDwordOperands(State, &ModRegRm, TRUE, BitNumber))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+        }
+        else
+        {
+            if (!Fast486WriteModrmWordOperands(State, &ModRegRm, TRUE, LOWORD(BitNumber)))
+            {
+                /* Exception occurred */
+                return FALSE;
+            }
+        }
+    }
+
+    return TRUE;
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeMovsxByte)
+{
+    UCHAR Dummy;
+    CHAR Value;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xBE);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the operands */
+    if (!Fast486ReadModrmByteOperands(State, &ModRegRm, &Dummy, (PUCHAR)&Value))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Write back the sign-extended value */
+    return Fast486WriteModrmDwordOperands(State,
+                                          &ModRegRm,
+                                          TRUE,
+                                          (ULONG)((LONG)Value));
+}
+
+FAST486_OPCODE_HANDLER(Fast486ExtOpcodeMovsxWord)
+{
+    USHORT Dummy;
+    SHORT Value;
+    BOOLEAN AddressSize = State->SegmentRegs[FAST486_REG_CS].Size;
+    FAST486_MOD_REG_RM ModRegRm;
+
+    TOGGLE_ADSIZE(AddressSize);
+
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xBF);
+
+    /* Get the operands */
+    if (!Fast486ParseModRegRm(State, AddressSize, &ModRegRm))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Read the operands */
+    if (!Fast486ReadModrmWordOperands(State, &ModRegRm, &Dummy, (PUSHORT)&Value))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Write back the sign-extended value */
+    return Fast486WriteModrmDwordOperands(State,
+                                          &ModRegRm,
+                                          TRUE,
+                                          (ULONG)((LONG)Value));
+}
+
 FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalJmp)
 {
     BOOLEAN Jump = FALSE;
@@ -1138,7 +1834,7 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeConditionalJmp)
 
     if (Jump)
     {
-        /* Move the instruction pointer */        
+        /* Move the instruction pointer */
         State->InstPtr.Long += Offset;
     }
 
@@ -1267,9 +1963,9 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXaddByte)
     State->Flags.Cf = (Result < Source) && (Result < Destination);
     State->Flags.Of = ((Source & SIGN_FLAG_BYTE) == (Destination & SIGN_FLAG_BYTE))
                       && ((Source & SIGN_FLAG_BYTE) != (Result & SIGN_FLAG_BYTE));
-    State->Flags.Af = (((Source & 0x0F) + (Destination & 0x0F)) & 0x10) ? TRUE : FALSE;
-    State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-    State->Flags.Sf = (Result & SIGN_FLAG_BYTE) ? TRUE : FALSE;
+    State->Flags.Af = ((((Source & 0x0F) + (Destination & 0x0F)) & 0x10) != 0);
+    State->Flags.Zf = (Result == 0);
+    State->Flags.Sf = ((Result & SIGN_FLAG_BYTE) != 0);
     State->Flags.Pf = Fast486CalculateParity(Result);
 
     /* Write the sum to the destination */
@@ -1322,7 +2018,7 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXadd)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = Source + Destination;
 
@@ -1330,9 +2026,9 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXadd)
         State->Flags.Cf = (Result < Source) && (Result < Destination);
         State->Flags.Of = ((Source & SIGN_FLAG_LONG) == (Destination & SIGN_FLAG_LONG))
                           && ((Source & SIGN_FLAG_LONG) != (Result & SIGN_FLAG_LONG));
-        State->Flags.Af = (((Source & 0x0F) + (Destination & 0x0F)) & 0x10) ? TRUE : FALSE;
-        State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-        State->Flags.Sf = (Result & SIGN_FLAG_LONG) ? TRUE : FALSE;
+        State->Flags.Af = ((((Source & 0x0F) + (Destination & 0x0F)) & 0x10) != 0);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_LONG) != 0);
         State->Flags.Pf = Fast486CalculateParity(Result);
 
         /* Write the sum to the destination */
@@ -1361,7 +2057,7 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXadd)
             /* Exception occurred */
             return FALSE;
         }
-    
+
         /* Calculate the result */
         Result = Source + Destination;
 
@@ -1369,9 +2065,9 @@ FAST486_OPCODE_HANDLER(Fast486ExtOpcodeXadd)
         State->Flags.Cf = (Result < Source) && (Result < Destination);
         State->Flags.Of = ((Source & SIGN_FLAG_WORD) == (Destination & SIGN_FLAG_WORD))
                           && ((Source & SIGN_FLAG_WORD) != (Result & SIGN_FLAG_WORD));
-        State->Flags.Af = (((Source & 0x0F) + (Destination & 0x0F)) & 0x10) ? TRUE : FALSE;
-        State->Flags.Zf = (Result == 0) ? TRUE : FALSE;
-        State->Flags.Sf = (Result & SIGN_FLAG_WORD) ? TRUE : FALSE;
+        State->Flags.Af = ((((Source & 0x0F) + (Destination & 0x0F)) & 0x10) != 0);
+        State->Flags.Zf = (Result == 0);
+        State->Flags.Sf = ((Result & SIGN_FLAG_WORD) != 0);
         State->Flags.Pf = Fast486CalculateParity(Result);
 
         /* Write the sum to the destination */