mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-19 16:08:02 +03:00
Converted StringInstructionDecoder to StringInstructionHandler for better consistency with handler pattern
This commit is contained in:
@ -307,6 +307,7 @@ public class InstructionHandlerFactory
|
||||
{
|
||||
// Add String instruction handlers
|
||||
_handlers.Add(new RepMovsHandler(_codeBuffer, _decoder, _length));
|
||||
_handlers.Add(new StringInstructionHandler(_codeBuffer, _decoder, _length));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -0,0 +1,88 @@
|
||||
namespace X86Disassembler.X86.Handlers.String;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for string instructions (MOVS, STOS, LODS, SCAS)
|
||||
/// </summary>
|
||||
public class StringInstructionHandler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the StringInstructionHandler class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public StringInstructionHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if this handler can handle the given opcode
|
||||
/// </summary>
|
||||
/// <param name="opcode">The opcode to check</param>
|
||||
/// <returns>True if this handler can handle the opcode</returns>
|
||||
public override bool CanHandle(byte opcode)
|
||||
{
|
||||
return IsStringInstruction(opcode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a string instruction
|
||||
/// </summary>
|
||||
/// <param name="opcode">The opcode to decode</param>
|
||||
/// <param name="instruction">The instruction to populate</param>
|
||||
/// <returns>True if the instruction was successfully decoded</returns>
|
||||
public override bool Decode(byte opcode, Instruction instruction)
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = OpcodeMap.GetMnemonic(opcode);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = GetStringOperands(opcode);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the opcode is a string instruction
|
||||
/// </summary>
|
||||
/// <param name="opcode">The opcode to check</param>
|
||||
/// <returns>True if the opcode is a string instruction</returns>
|
||||
private bool IsStringInstruction(byte opcode)
|
||||
{
|
||||
return opcode == 0xA4 || opcode == 0xA5 || // MOVS
|
||||
opcode == 0xAA || opcode == 0xAB || // STOS
|
||||
opcode == 0xAC || opcode == 0xAD || // LODS
|
||||
opcode == 0xAE || opcode == 0xAF; // SCAS
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the operands for a string instruction
|
||||
/// </summary>
|
||||
/// <param name="stringOp">The string operation opcode</param>
|
||||
/// <returns>The operands string</returns>
|
||||
private string GetStringOperands(byte stringOp)
|
||||
{
|
||||
switch (stringOp)
|
||||
{
|
||||
case 0xA4: // MOVSB
|
||||
return "byte ptr [edi], byte ptr [esi]";
|
||||
case 0xA5: // MOVSD
|
||||
return "dword ptr [edi], dword ptr [esi]";
|
||||
case 0xAA: // STOSB
|
||||
return "byte ptr [edi], al";
|
||||
case 0xAB: // STOSD
|
||||
return "dword ptr [edi], eax";
|
||||
case 0xAC: // LODSB
|
||||
return "al, byte ptr [esi]";
|
||||
case 0xAD: // LODSD
|
||||
return "eax, dword ptr [esi]";
|
||||
case 0xAE: // SCASB
|
||||
return "al, byte ptr [edi]";
|
||||
case 0xAF: // SCASD
|
||||
return "eax, dword ptr [edi]";
|
||||
default:
|
||||
return "??";
|
||||
}
|
||||
}
|
||||
}
|
@ -22,7 +22,6 @@ public class InstructionDecoder
|
||||
// Specialized decoders
|
||||
private readonly PrefixDecoder _prefixDecoder;
|
||||
private readonly ModRMDecoder _modRMDecoder;
|
||||
private readonly StringInstructionDecoder _stringDecoder;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the InstructionDecoder class
|
||||
@ -38,7 +37,6 @@ public class InstructionDecoder
|
||||
// Create specialized decoders
|
||||
_prefixDecoder = new PrefixDecoder();
|
||||
_modRMDecoder = new ModRMDecoder(codeBuffer, this, length);
|
||||
_stringDecoder = new StringInstructionDecoder(codeBuffer, length);
|
||||
|
||||
// Create the instruction handler factory
|
||||
_handlerFactory = new InstructionHandlerFactory(_codeBuffer, this, _length);
|
||||
@ -75,20 +73,6 @@ public class InstructionDecoder
|
||||
if (_prefixDecoder.DecodePrefix(prefix))
|
||||
{
|
||||
_position++;
|
||||
|
||||
// Special case for REP/REPNE prefix followed by string instruction
|
||||
if ((prefix == 0xF2 || prefix == 0xF3) && _position < _length)
|
||||
{
|
||||
byte nextByte = _codeBuffer[_position];
|
||||
if (_stringDecoder.IsStringInstruction(nextByte))
|
||||
{
|
||||
// Skip the string operation opcode
|
||||
_position++;
|
||||
|
||||
// Handle REP string instruction
|
||||
return _stringDecoder.CreateStringInstruction(prefix, nextByte, startPosition, _position);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1,98 +0,0 @@
|
||||
namespace X86Disassembler.X86;
|
||||
|
||||
/// <summary>
|
||||
/// Handles decoding of string instructions
|
||||
/// </summary>
|
||||
public class StringInstructionDecoder
|
||||
{
|
||||
// The buffer containing the code to decode
|
||||
private readonly byte[] _codeBuffer;
|
||||
|
||||
// The length of the buffer
|
||||
private readonly int _length;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the StringInstructionDecoder class
|
||||
/// </summary>
|
||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||
/// <param name="length">The length of the buffer</param>
|
||||
public StringInstructionDecoder(byte[] codeBuffer, int length)
|
||||
{
|
||||
_codeBuffer = codeBuffer;
|
||||
_length = length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the opcode is a string instruction
|
||||
/// </summary>
|
||||
/// <param name="opcode">The opcode to check</param>
|
||||
/// <returns>True if the opcode is a string instruction</returns>
|
||||
public bool IsStringInstruction(byte opcode)
|
||||
{
|
||||
return opcode == 0xA4 || opcode == 0xA5 || // MOVS
|
||||
opcode == 0xAA || opcode == 0xAB || // STOS
|
||||
opcode == 0xAC || opcode == 0xAD || // LODS
|
||||
opcode == 0xAE || opcode == 0xAF; // SCAS
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instruction for a string operation with REP/REPNE prefix
|
||||
/// </summary>
|
||||
/// <param name="prefix">The REP/REPNE prefix (0xF2 or 0xF3)</param>
|
||||
/// <param name="stringOp">The string operation opcode</param>
|
||||
/// <param name="startPosition">The start position of the instruction</param>
|
||||
/// <param name="currentPosition">The current position after reading the string opcode</param>
|
||||
/// <returns>The created instruction</returns>
|
||||
public Instruction CreateStringInstruction(byte prefix, byte stringOp, int startPosition, int currentPosition)
|
||||
{
|
||||
// Create a new instruction
|
||||
Instruction instruction = new Instruction
|
||||
{
|
||||
Address = (uint)startPosition,
|
||||
};
|
||||
|
||||
// Get the mnemonic for the string operation
|
||||
string mnemonic = OpcodeMap.GetMnemonic(stringOp);
|
||||
instruction.Mnemonic = prefix == 0xF3 ? $"rep {mnemonic}" : $"repne {mnemonic}";
|
||||
|
||||
// Set operands based on the string operation
|
||||
instruction.Operands = GetStringOperands(stringOp);
|
||||
|
||||
// Set the raw bytes
|
||||
int length = currentPosition - startPosition;
|
||||
instruction.RawBytes = new byte[length];
|
||||
Array.Copy(_codeBuffer, startPosition, instruction.RawBytes, 0, length);
|
||||
|
||||
return instruction;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the operands for a string instruction
|
||||
/// </summary>
|
||||
/// <param name="stringOp">The string operation opcode</param>
|
||||
/// <returns>The operands string</returns>
|
||||
private string GetStringOperands(byte stringOp)
|
||||
{
|
||||
switch (stringOp)
|
||||
{
|
||||
case 0xA4: // MOVSB
|
||||
return "byte ptr [edi], byte ptr [esi]";
|
||||
case 0xA5: // MOVSD
|
||||
return "dword ptr [edi], dword ptr [esi]";
|
||||
case 0xAA: // STOSB
|
||||
return "byte ptr [edi], al";
|
||||
case 0xAB: // STOSD
|
||||
return "dword ptr [edi], eax";
|
||||
case 0xAC: // LODSB
|
||||
return "al, byte ptr [esi]";
|
||||
case 0xAD: // LODSD
|
||||
return "eax, dword ptr [esi]";
|
||||
case 0xAE: // SCASB
|
||||
return "al, byte ptr [edi]";
|
||||
case 0xAF: // SCASD
|
||||
return "eax, dword ptr [edi]";
|
||||
default:
|
||||
return "??";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user