[SOFT386]
[reactos.git] / lib / soft386 / opcodes.c
index bb32977..e2dac35 100644 (file)
@@ -466,12 +466,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeIncrement)
     ULONG Value;
     BOOLEAN Size = State->SegmentRegs[SOFT386_REG_CS].Size;
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -509,12 +510,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeDecrement)
     ULONG Value;
     BOOLEAN Size = State->SegmentRegs[SOFT386_REG_CS].Size;
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+    
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -618,12 +620,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeExchangeEax)
     INT Reg = Opcode & 0x07;
     BOOLEAN Size = State->SegmentRegs[SOFT386_REG_CS].Size;
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -974,12 +977,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeIn)
     /* Make sure this is the right instruction */
     ASSERT((Opcode & 0xF7) == 0xE5);
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -1073,12 +1077,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeOut)
     /* Make sure this is the right instruction */
     ASSERT((Opcode & 0xF7) == 0xE7);
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -4533,12 +4538,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeCwde)
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0x98);
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -4573,12 +4579,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeCdq)
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0x99);
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -4825,7 +4832,7 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeRet)
         return FALSE;
     }
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
@@ -5538,7 +5545,7 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeJecxz)
         return FALSE;
     }
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
@@ -5570,12 +5577,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeCall)
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xE8);
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -5635,12 +5643,13 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeJmp)
     /* Make sure this is the right instruction */
     ASSERT(Opcode == 0xE9);
 
-    if (State->PrefixFlags == SOFT386_PREFIX_OPSIZE)
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
     {
         /* The OPSIZE prefix toggles the size */
         Size = !Size;
     }
-    else if (State->PrefixFlags != 0)
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
     {
         /* Invalid prefix */
         Soft386Exception(State, SOFT386_EXCEPTION_UD);
@@ -5681,10 +5690,63 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeJmp)
 
 SOFT386_OPCODE_HANDLER(Soft386OpcodeJmpAbs)
 {
-    // TODO: NOT IMPLEMENTED
-    UNIMPLEMENTED;
+    USHORT Segment = 0;
+    ULONG Offset = 0;
+    BOOLEAN Size = State->SegmentRegs[SOFT386_REG_CS].Size;
 
-    return FALSE;
+    /* Make sure this is the right instruction */
+    ASSERT(Opcode == 0xEA);
+
+    if (State->PrefixFlags & SOFT386_PREFIX_OPSIZE)
+    {
+        /* The OPSIZE prefix toggles the size */
+        Size = !Size;
+    }
+
+    if (State->PrefixFlags & SOFT386_PREFIX_LOCK)
+    {
+        /* Invalid prefix */
+        Soft386Exception(State, SOFT386_EXCEPTION_UD);
+        return FALSE;
+    }
+
+    /* Fetch the offset */
+    if (Size)
+    {
+        if (!Soft386FetchDword(State, &Offset))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+    else
+    {
+        if (!Soft386FetchWord(State, (PUSHORT)&Offset))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+    }
+
+    /* Fetch the segment */
+    if (!Soft386FetchWord(State, &Segment))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Load the new CS */
+    if (!Soft386LoadSegment(State, SOFT386_REG_CS, Segment))
+    {
+        /* Exception occurred */
+        return FALSE;
+    }
+
+    /* Load new (E)IP */
+    if (Size) State->InstPtr.Long = Offset;
+    else State->InstPtr.LowWord = LOWORD(Offset);
+
+    return TRUE;
 }
 
 SOFT386_OPCODE_HANDLER(Soft386OpcodeMovAlOffset)
@@ -5920,68 +5982,163 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeMovs)
         AddressSize = !AddressSize;
     }
 
-    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
-        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
-    {
-        // TODO: The REP/REPZ/REPNZ prefixes need to be implemented!
-        Soft386Exception(State, SOFT386_EXCEPTION_UD);
-        return FALSE;
-    }
-
     /* Calculate the size */
     if (Opcode == 0xA4) DataSize = sizeof(UCHAR);
     else DataSize = OperandSize ? sizeof(ULONG) : sizeof(USHORT);
 
-    /* Read from the source operand */
-    if (!Soft386ReadMemory(State,
-                           SOFT386_REG_DS,
-                           AddressSize ? State->GeneralRegs[SOFT386_REG_ESI].Long
-                                       : State->GeneralRegs[SOFT386_REG_ESI].LowWord,
-                           FALSE,
-                           &Data,
-                           DataSize))
+    if (State->PrefixFlags & SOFT386_PREFIX_REP)
     {
-        /* Exception occurred */
-        return FALSE;
-    }
+        UCHAR Block[STRING_BLOCK_SIZE];
+        ULONG Count = OperandSize ? State->GeneralRegs[SOFT386_REG_ECX].Long
+                                  : State->GeneralRegs[SOFT386_REG_ECX].LowWord;
 
-    /* Write to the destination operand */
-    if (!Soft386WriteMemory(State,
-                            SOFT386_REG_ES,
-                            AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
-                                        : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
-                            &Data,
-                            DataSize))
-    {
-        /* Exception occurred */
-        return FALSE;
-    }
+        /* Clear the memory block */
+        RtlZeroMemory(Block, sizeof(Block));
 
-    /* Increment/decrement ESI and EDI */
-    if (OperandSize)
-    {
-        if (State->Flags.Df)
-        {
-            State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
-            State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
-        }
-        else
+        /* Transfer until finished */
+        while (Count)
         {
-            State->GeneralRegs[SOFT386_REG_ESI].Long -= DataSize;
-            State->GeneralRegs[SOFT386_REG_EDI].Long -= DataSize;
+            ULONG Processed = min(Count, STRING_BLOCK_SIZE / DataSize);
+
+            /* Simulate the 16-bit wrap-around of SI and DI in 16-bit address mode */
+            if (!AddressSize)
+            {
+                ULONG MaxBytesSrc = State->Flags.Df
+                                    ? (ULONG)State->GeneralRegs[SOFT386_REG_ESI].LowWord
+                                    : (0x10000 - (ULONG)State->GeneralRegs[SOFT386_REG_ESI].LowWord);
+                ULONG MaxBytesDest = State->Flags.Df
+                                     ? (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord
+                                     : (0x10000 - (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord);
+
+
+                Processed = min(Processed, min(MaxBytesSrc, MaxBytesDest) / DataSize);
+                if (Processed == 0) Processed = 1;
+            }
+
+            if (State->Flags.Df)
+            {
+                /* Reduce ESI and EDI by the number of bytes to transfer */
+                if (AddressSize)
+                {
+                    State->GeneralRegs[SOFT386_REG_ESI].Long -= Processed * DataSize;
+                    State->GeneralRegs[SOFT386_REG_EDI].Long -= Processed * DataSize;
+                }
+                else
+                {
+                    State->GeneralRegs[SOFT386_REG_ESI].LowWord -= Processed * DataSize;
+                    State->GeneralRegs[SOFT386_REG_EDI].LowWord -= Processed * DataSize;
+                }
+            }
+
+            /* Read from memory */
+            if (!Soft386ReadMemory(State,
+                                   SOFT386_REG_DS,
+                                   AddressSize ? State->GeneralRegs[SOFT386_REG_ESI].Long
+                                               : State->GeneralRegs[SOFT386_REG_ESI].LowWord,
+                                   FALSE,
+                                   Block,
+                                   Processed * DataSize))
+            {
+                /* Set ECX */
+                if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = Count;
+                else State->GeneralRegs[SOFT386_REG_ECX].LowWord = LOWORD(Count);
+
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            /* Write to memory */
+            if (!Soft386WriteMemory(State,
+                                    SOFT386_REG_ES,
+                                    AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
+                                                : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
+                                    Block,
+                                    Processed * DataSize))
+            {
+                /* Set ECX */
+                if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = Count;
+                else State->GeneralRegs[SOFT386_REG_ECX].LowWord = LOWORD(Count);
+
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            if (!State->Flags.Df)
+            {
+                /* Increase ESI and EDI by the number of bytes transfered */
+                if (AddressSize)
+                {
+                    State->GeneralRegs[SOFT386_REG_ESI].Long += Processed * DataSize;
+                    State->GeneralRegs[SOFT386_REG_EDI].Long += Processed * DataSize;
+                }
+                else
+                {
+                    State->GeneralRegs[SOFT386_REG_ESI].LowWord += Processed * DataSize;
+                    State->GeneralRegs[SOFT386_REG_EDI].LowWord += Processed * DataSize;
+                }
+            }
+
+            /* Reduce the total count by the number processed in this run */
+            Count -= Processed;
         }
+
+        /* Clear ECX */
+        if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = 0;
+        else State->GeneralRegs[SOFT386_REG_ECX].LowWord = 0;
     }
     else
     {
-        if (State->Flags.Df)
+        /* Read from the source operand */
+        if (!Soft386ReadMemory(State,
+                               SOFT386_REG_DS,
+                               AddressSize ? State->GeneralRegs[SOFT386_REG_ESI].Long
+                                           : State->GeneralRegs[SOFT386_REG_ESI].LowWord,
+                               FALSE,
+                               &Data,
+                               DataSize))
         {
-            State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
-            State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Write to the destination operand */
+        if (!Soft386WriteMemory(State,
+                                SOFT386_REG_ES,
+                                AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
+                                            : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
+                                &Data,
+                                DataSize))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Increment/decrement ESI and EDI */
+        if (OperandSize)
+        {
+            if (!State->Flags.Df)
+            {
+                State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
+                State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
+            }
+            else
+            {
+                State->GeneralRegs[SOFT386_REG_ESI].Long -= DataSize;
+                State->GeneralRegs[SOFT386_REG_EDI].Long -= DataSize;
+            }
         }
         else
         {
-            State->GeneralRegs[SOFT386_REG_ESI].LowWord -= DataSize;
-            State->GeneralRegs[SOFT386_REG_EDI].LowWord -= DataSize;
+            if (!State->Flags.Df)
+            {
+                State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
+                State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
+            }
+            else
+            {
+                State->GeneralRegs[SOFT386_REG_ESI].LowWord -= DataSize;
+                State->GeneralRegs[SOFT386_REG_EDI].LowWord -= DataSize;
+            }
         }
     }
 
@@ -6071,7 +6228,7 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeCmps)
     /* Increment/decrement ESI and EDI */
     if (OperandSize)
     {
-        if (State->Flags.Df)
+        if (!State->Flags.Df)
         {
             State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
             State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
@@ -6084,7 +6241,7 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeCmps)
     }
     else
     {
-        if (State->Flags.Df)
+        if (!State->Flags.Df)
         {
             State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
             State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
@@ -6096,6 +6253,43 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeCmps)
         }
     }
 
+    // FIXME: This method is slow!
+    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
+        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
+    {
+        BOOLEAN Repeat = TRUE;
+        
+        if (OperandSize)
+        {
+            if ((--State->GeneralRegs[SOFT386_REG_ECX].Long) == 0)
+            {
+                /* ECX is 0 */
+                Repeat = FALSE;
+            }
+        }
+        else
+        {
+            if ((--State->GeneralRegs[SOFT386_REG_ECX].LowWord) == 0)
+            {
+                /* CX is 0 */
+                Repeat = FALSE;
+            }
+        }
+
+        if (((State->PrefixFlags & SOFT386_PREFIX_REP) && !State->Flags.Zf)
+            || ((State->PrefixFlags & SOFT386_PREFIX_REPNZ) && State->Flags.Zf))
+        {
+            /* REPZ with ZF = 0 or REPNZ with ZF = 1 */
+            Repeat = FALSE;
+        }
+
+        if (Repeat)
+        {
+            /* Repeat the instruction */
+            State->InstPtr = State->SavedInstPtr;
+        }
+    }
+
     /* Return success */
     return TRUE;
 }
@@ -6122,40 +6316,117 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeStos)
         AddressSize = !AddressSize;
     }
 
-    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
-        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
-    {
-        // TODO: The REP/REPZ/REPNZ prefixes need to be implemented!
-        Soft386Exception(State, SOFT386_EXCEPTION_UD);
-        return FALSE;
-    }
-
     /* Calculate the size */
     if (Opcode == 0xAA) DataSize = sizeof(UCHAR);
     else DataSize = OperandSize ? sizeof(ULONG) : sizeof(USHORT);
 
-    /* Write to the destination operand */
-    if (!Soft386WriteMemory(State,
-                            SOFT386_REG_ES,
-                            AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
-                                        : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
-                            &State->GeneralRegs[SOFT386_REG_EAX].Long,
-                            DataSize))
+    if (State->PrefixFlags & SOFT386_PREFIX_REP)
     {
-        /* Exception occurred */
-        return FALSE;
-    }
+        UCHAR Block[STRING_BLOCK_SIZE];
+        ULONG Count = OperandSize ? State->GeneralRegs[SOFT386_REG_ECX].Long
+                                  : State->GeneralRegs[SOFT386_REG_ECX].LowWord;
 
-    /* Increment/decrement EDI */
-    if (OperandSize)
-    {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
-        else State->GeneralRegs[SOFT386_REG_EDI].Long -= DataSize;
+        /* Fill the memory block with the data */
+        if (DataSize == sizeof(UCHAR))
+        {
+            RtlFillMemory(Block, sizeof(Block), State->GeneralRegs[SOFT386_REG_EAX].LowByte);
+        }
+        else
+        {
+            ULONG i;
+
+            for (i = 0; i < STRING_BLOCK_SIZE / DataSize; i++)
+            {
+                if (DataSize == sizeof(USHORT))
+                {
+                    ((PUSHORT)Block)[i] = State->GeneralRegs[SOFT386_REG_EAX].LowWord;
+                }
+                else
+                {
+                    ((PULONG)Block)[i] = State->GeneralRegs[SOFT386_REG_EAX].Long;
+                }
+            }
+        }
+
+        /* Transfer until finished */
+        while (Count)
+        {
+            ULONG Processed = min(Count, STRING_BLOCK_SIZE / DataSize);
+
+            /* Simulate the 16-bit wrap-around of DI in 16-bit address mode */
+            if (!AddressSize)
+            {
+                ULONG MaxBytes = State->Flags.Df
+                                 ? (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord
+                                 : (0x10000 - (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord);
+
+                Processed = min(Processed, MaxBytes / DataSize);
+                if (Processed == 0) Processed = 1;
+            }
+
+            if (State->Flags.Df)
+            {
+                /* Reduce EDI by the number of bytes to transfer */
+                if (AddressSize) State->GeneralRegs[SOFT386_REG_EDI].Long -= Processed * DataSize;
+                else State->GeneralRegs[SOFT386_REG_EDI].LowWord -= Processed * DataSize;
+            }
+
+            /* Write to memory */
+            if (!Soft386WriteMemory(State,
+                                    SOFT386_REG_ES,
+                                    AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
+                                                : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
+                                    Block,
+                                    Processed * DataSize))
+            {
+                /* Set ECX */
+                if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = Count;
+                else State->GeneralRegs[SOFT386_REG_ECX].LowWord = LOWORD(Count);
+
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            if (!State->Flags.Df)
+            {
+                /* Increase EDI by the number of bytes transfered */
+                if (AddressSize) State->GeneralRegs[SOFT386_REG_EDI].Long += Processed * DataSize;
+                else State->GeneralRegs[SOFT386_REG_EDI].LowWord += Processed * DataSize;
+            }
+
+            /* Reduce the total count by the number processed in this run */
+            Count -= Processed;
+        }
+
+        /* Clear ECX */
+        if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = 0;
+        else State->GeneralRegs[SOFT386_REG_ECX].LowWord = 0;
     }
     else
     {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
-        else State->GeneralRegs[SOFT386_REG_EDI].LowWord -= DataSize;
+        /* Write to the destination operand */
+        if (!Soft386WriteMemory(State,
+                                SOFT386_REG_ES,
+                                AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
+                                            : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
+                                &State->GeneralRegs[SOFT386_REG_EAX].Long,
+                                DataSize))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Increment/decrement EDI */
+        if (OperandSize)
+        {
+            if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
+            else State->GeneralRegs[SOFT386_REG_EDI].Long -= DataSize;
+        }
+        else
+        {
+            if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
+            else State->GeneralRegs[SOFT386_REG_EDI].LowWord -= DataSize;
+        }
     }
 
     /* Return success */
@@ -6184,18 +6455,31 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeLods)
         AddressSize = !AddressSize;
     }
 
-    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
-        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
-    {
-        // TODO: The REP/REPZ/REPNZ prefixes need to be implemented!
-        Soft386Exception(State, SOFT386_EXCEPTION_UD);
-        return FALSE;
-    }
-
     /* Calculate the size */
     if (Opcode == 0xAC) DataSize = sizeof(UCHAR);
     else DataSize = OperandSize ? sizeof(ULONG) : sizeof(USHORT);
 
+    if (State->PrefixFlags & SOFT386_PREFIX_REP)
+    {
+        ULONG Count = OperandSize ? State->GeneralRegs[SOFT386_REG_ECX].Long
+                                  : State->GeneralRegs[SOFT386_REG_ECX].LowWord;
+
+        /* If the count is 0, do nothing */
+        if (Count == 0) return TRUE;
+
+        /* Only the last entry will be loaded */
+        if (!State->Flags.Df)
+        {
+            if (AddressSize) State->GeneralRegs[SOFT386_REG_ESI].Long += (Count - 1) * DataSize;
+            else State->GeneralRegs[SOFT386_REG_ESI].LowWord += (Count - 1) * DataSize;
+        }
+        else
+        {
+            if (AddressSize) State->GeneralRegs[SOFT386_REG_ESI].Long -= (Count - 1) * DataSize;
+            else State->GeneralRegs[SOFT386_REG_ESI].LowWord -= (Count - 1) * DataSize;
+        }
+    }
+
     /* Read from the source operand */
     if (!Soft386ReadMemory(State,
                            SOFT386_REG_DS,
@@ -6212,12 +6496,12 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeLods)
     /* Increment/decrement ESI */
     if (OperandSize)
     {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
+        if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
         else State->GeneralRegs[SOFT386_REG_ESI].Long -= DataSize;
     }
     else
     {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
+        if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
         else State->GeneralRegs[SOFT386_REG_ESI].LowWord -= DataSize;
     }
 
@@ -6250,16 +6534,8 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeScas)
         AddressSize = !AddressSize;
     }
 
-    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
-        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
-    {
-        // TODO: The REP/REPZ/REPNZ prefixes need to be implemented!
-        Soft386Exception(State, SOFT386_EXCEPTION_UD);
-        return FALSE;
-    }
-
     /* Calculate the size */
-    if (Opcode == 0xA6) DataSize = sizeof(UCHAR);
+    if (Opcode == 0xAE) DataSize = sizeof(UCHAR);
     else DataSize = OperandSize ? sizeof(ULONG) : sizeof(USHORT);
 
     /* Calculate the mask and sign flag */
@@ -6296,15 +6572,52 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeScas)
     /* Increment/decrement EDI */
     if (OperandSize)
     {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
+        if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
         else State->GeneralRegs[SOFT386_REG_EDI].Long -= DataSize;
     }
     else
     {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
+        if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
         else State->GeneralRegs[SOFT386_REG_EDI].LowWord -= DataSize;
     }
 
+    // FIXME: This method is slow!
+    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
+        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
+    {
+        BOOLEAN Repeat = TRUE;
+        
+        if (OperandSize)
+        {
+            if ((--State->GeneralRegs[SOFT386_REG_ECX].Long) == 0)
+            {
+                /* ECX is 0 */
+                Repeat = FALSE;
+            }
+        }
+        else
+        {
+            if ((--State->GeneralRegs[SOFT386_REG_ECX].LowWord) == 0)
+            {
+                /* CX is 0 */
+                Repeat = FALSE;
+            }
+        }
+
+        if (((State->PrefixFlags & SOFT386_PREFIX_REP) && !State->Flags.Zf)
+            || ((State->PrefixFlags & SOFT386_PREFIX_REPNZ) && State->Flags.Zf))
+        {
+            /* REPZ with ZF = 0 or REPNZ with ZF = 1 */
+            Repeat = FALSE;
+        }
+
+        if (Repeat)
+        {
+            /* Repeat the instruction */
+            State->InstPtr = State->SavedInstPtr;
+        }
+    }
+
     /* Return success */
     return TRUE;
 }
@@ -6349,6 +6662,17 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeIns)
         {
             ULONG Processed = min(Count, STRING_BLOCK_SIZE / DataSize);
 
+            /* Simulate the 16-bit wrap-around of DI in 16-bit address mode */
+            if (!AddressSize)
+            {
+                ULONG MaxBytes = State->Flags.Df
+                                 ? (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord
+                                 : (0x10000 - (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord);
+
+                Processed = min(Processed, MaxBytes / DataSize);
+                if (Processed == 0) Processed = 1;
+            }
+
             /* Read from the I/O port */
             State->IoReadCallback(State,
                                   State->GeneralRegs[SOFT386_REG_EDX].LowWord,
@@ -6432,12 +6756,12 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeIns)
         /* Increment/decrement EDI */
         if (OperandSize)
         {
-            if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
+            if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].Long += DataSize;
             else State->GeneralRegs[SOFT386_REG_EDI].Long -= DataSize;
         }
         else
         {
-            if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
+            if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_EDI].LowWord += DataSize;
             else State->GeneralRegs[SOFT386_REG_EDI].LowWord -= DataSize;
         }
     }
@@ -6448,7 +6772,6 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeIns)
 
 SOFT386_OPCODE_HANDLER(Soft386OpcodeOuts)
 {
-    ULONG Data = 0;
     ULONG DataSize;
     BOOLEAN OperandSize, AddressSize;
 
@@ -6469,47 +6792,128 @@ SOFT386_OPCODE_HANDLER(Soft386OpcodeOuts)
         AddressSize = !AddressSize;
     }
 
-    if ((State->PrefixFlags & SOFT386_PREFIX_REP)
-        || (State->PrefixFlags & SOFT386_PREFIX_REPNZ))
-    {
-        // TODO: The REP/REPZ/REPNZ prefixes need to be implemented!
-        Soft386Exception(State, SOFT386_EXCEPTION_UD);
-        return FALSE;
-    }
-
     /* Calculate the size */
     if (Opcode == 0x6E) DataSize = sizeof(UCHAR);
     else DataSize = OperandSize ? sizeof(ULONG) : sizeof(USHORT);
 
-    /* Read from the source operand */
-    if (!Soft386ReadMemory(State,
-                           SOFT386_REG_DS,
-                           AddressSize ? State->GeneralRegs[SOFT386_REG_ESI].Long
-                                       : State->GeneralRegs[SOFT386_REG_ESI].LowWord,
-                           FALSE,
-                           &Data,
-                           DataSize))
+    if (State->PrefixFlags & SOFT386_PREFIX_REP)
     {
-        /* Exception occurred */
-        return FALSE;
-    }
+        UCHAR Block[STRING_BLOCK_SIZE];
+        ULONG Count = OperandSize ? State->GeneralRegs[SOFT386_REG_ECX].Long
+                                  : State->GeneralRegs[SOFT386_REG_ECX].LowWord;
 
-    /* Write to the I/O port */
-    State->IoWriteCallback(State,
-                           State->GeneralRegs[SOFT386_REG_EDX].LowWord,
-                           &Data,
-                           DataSize);
+        /* Clear the memory block */
+        RtlZeroMemory(Block, sizeof(Block));
 
-    /* Increment/decrement ESI */
-    if (OperandSize)
-    {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
-        else State->GeneralRegs[SOFT386_REG_ESI].Long -= DataSize;
+        /* Transfer until finished */
+        while (Count)
+        {
+            ULONG Processed = min(Count, STRING_BLOCK_SIZE / DataSize);
+
+            /* Simulate the 16-bit wrap-around of DI in 16-bit address mode */
+            if (!AddressSize)
+            {
+                ULONG MaxBytes = State->Flags.Df
+                                 ? (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord
+                                 : (0x10000 - (ULONG)State->GeneralRegs[SOFT386_REG_EDI].LowWord);
+
+                Processed = min(Processed, MaxBytes / DataSize);
+                if (Processed == 0) Processed = 1;
+            }
+
+            /* Read from memory */
+            if (!Soft386ReadMemory(State,
+                                   SOFT386_REG_ES,
+                                   AddressSize ? State->GeneralRegs[SOFT386_REG_EDI].Long
+                                               : State->GeneralRegs[SOFT386_REG_EDI].LowWord,
+                                   FALSE,
+                                   Block,
+                                   Processed * DataSize))
+            {
+                /* Set ECX */
+                if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = Count;
+                else State->GeneralRegs[SOFT386_REG_ECX].LowWord = LOWORD(Count);
+
+                /* Exception occurred */
+                return FALSE;
+            }
+
+            if (State->Flags.Df)
+            {
+                ULONG i, j;
+
+                /* Reduce EDI by the number of bytes to transfer */
+                if (AddressSize) State->GeneralRegs[SOFT386_REG_EDI].Long -= Processed * DataSize;
+                else State->GeneralRegs[SOFT386_REG_EDI].LowWord -= Processed * DataSize;
+
+                /* Reverse the block data */
+                for (i = 0; i < Processed / 2; i++)
+                {
+                    /* Swap the values */
+                    for (j = 0; j < DataSize; j++)
+                    {
+                        UCHAR Temp = Block[i * DataSize + j];
+                        Block[i * DataSize + j] = Block[(Processed - i - 1) * DataSize + j];
+                        Block[(Processed - i - 1) * DataSize + j] = Temp;
+                    }
+                }
+            }
+
+            /* Write to the I/O port */
+            State->IoWriteCallback(State,
+                                   State->GeneralRegs[SOFT386_REG_EDX].LowWord,
+                                   Block,
+                                   Processed * DataSize);
+
+            if (!State->Flags.Df)
+            {
+                /* Increase EDI by the number of bytes transfered */
+                if (AddressSize) State->GeneralRegs[SOFT386_REG_EDI].Long += Processed * DataSize;
+                else State->GeneralRegs[SOFT386_REG_EDI].LowWord += Processed * DataSize;
+            }
+
+            /* Reduce the total count by the number processed in this run */
+            Count -= Processed;
+        }
+
+        /* Clear ECX */
+        if (OperandSize) State->GeneralRegs[SOFT386_REG_ECX].Long = 0;
+        else State->GeneralRegs[SOFT386_REG_ECX].LowWord = 0;
     }
     else
     {
-        if (State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
-        else State->GeneralRegs[SOFT386_REG_ESI].LowWord -= DataSize;
+        ULONG Data = 0;
+
+        /* Read from the source operand */
+        if (!Soft386ReadMemory(State,
+                               SOFT386_REG_DS,
+                               AddressSize ? State->GeneralRegs[SOFT386_REG_ESI].Long
+                                           : State->GeneralRegs[SOFT386_REG_ESI].LowWord,
+                               FALSE,
+                               &Data,
+                               DataSize))
+        {
+            /* Exception occurred */
+            return FALSE;
+        }
+
+        /* Write to the I/O port */
+        State->IoWriteCallback(State,
+                               State->GeneralRegs[SOFT386_REG_EDX].LowWord,
+                               &Data,
+                               DataSize);
+
+        /* Increment/decrement ESI */
+        if (OperandSize)
+        {
+            if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].Long += DataSize;
+            else State->GeneralRegs[SOFT386_REG_ESI].Long -= DataSize;
+        }
+        else
+        {
+            if (!State->Flags.Df) State->GeneralRegs[SOFT386_REG_ESI].LowWord += DataSize;
+            else State->GeneralRegs[SOFT386_REG_ESI].LowWord -= DataSize;
+        }
     }
 
     /* Return success */