0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +03:00

more refactoring

This commit is contained in:
bird_egop
2025-04-14 01:08:14 +03:00
parent f54dc10596
commit 99b93523a4
78 changed files with 379 additions and 594 deletions

View File

@ -11,11 +11,11 @@ public class DivRm32Handler : InstructionHandler
/// <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 DivRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public DivRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -25,18 +25,18 @@ public class DivRm32Handler : InstructionHandler
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 6 (DIV)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 6; // 6 = DIV
}
/// <summary>
/// Decodes a DIV r/m32 instruction
/// </summary>
@ -47,20 +47,18 @@ public class DivRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "div";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the operands
instruction.Operands = destOperand;
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class IdivRm32Handler : InstructionHandler
/// <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 IdivRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public IdivRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -25,18 +25,18 @@ public class IdivRm32Handler : InstructionHandler
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 7 (IDIV)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = IDIV
}
/// <summary>
/// Decodes an IDIV r/m32 instruction
/// </summary>
@ -47,20 +47,18 @@ public class IdivRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "idiv";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the operands
instruction.Operands = destOperand;
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class ImulRm32Handler : InstructionHandler
/// <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 ImulRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public ImulRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -25,18 +25,18 @@ public class ImulRm32Handler : InstructionHandler
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 5 (IMUL)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 5; // 5 = IMUL
}
/// <summary>
/// Decodes an IMUL r/m32 instruction
/// </summary>
@ -47,20 +47,18 @@ public class ImulRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "imul";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the operands
instruction.Operands = destOperand;
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class MulRm32Handler : InstructionHandler
/// <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 MulRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MulRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -25,18 +25,18 @@ public class MulRm32Handler : InstructionHandler
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 4 (MUL)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 4; // 4 = MUL
}
/// <summary>
/// Decodes a MUL r/m32 instruction
/// </summary>
@ -47,20 +47,18 @@ public class MulRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "mul";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the operands
instruction.Operands = destOperand;
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class NegRm32Handler : InstructionHandler
/// <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 NegRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public NegRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -25,18 +25,18 @@ public class NegRm32Handler : InstructionHandler
{
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 3 (NEG)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 3; // 3 = NEG
}
/// <summary>
/// Decodes a NEG r/m32 instruction
/// </summary>
@ -47,20 +47,18 @@ public class NegRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "neg";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the operands
instruction.Operands = destOperand;
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class NotRm32Handler : InstructionHandler
/// <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 NotRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public NotRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -26,18 +26,18 @@ public class NotRm32Handler : InstructionHandler
// This handler only handles opcode 0xF7
if (opcode != 0xF7)
return false;
// Check if the reg field of the ModR/M byte is 2 (NOT)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 2; // 2 = NOT
}
/// <summary>
/// Decodes a NOT r/m32 instruction
/// </summary>
@ -46,36 +46,32 @@ public class NotRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Verify this is a NOT instruction
if (reg != RegisterIndex.C)
{
return false;
}
// Set the mnemonic
instruction.Mnemonic = "not";
Decoder.SetPosition(position);
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
{
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
// Set the operands
instruction.Operands = destOperand;
return true;
}
}
}