mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 08:18:36 +03:00
more refactoring
This commit is contained in:
@ -11,11 +11,11 @@ public class AddEaxImmHandler : 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 AddEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
public AddEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks if this handler can decode the given opcode
|
||||
/// </summary>
|
||||
@ -25,7 +25,7 @@ public class AddEaxImmHandler : InstructionHandler
|
||||
{
|
||||
return opcode == 0x05;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an ADD EAX, imm32 instruction
|
||||
/// </summary>
|
||||
@ -36,24 +36,21 @@ public class AddEaxImmHandler : InstructionHandler
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "add";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
// Check if we have enough bytes for the immediate value
|
||||
if (position + 3 >= Length)
|
||||
|
||||
if (!Decoder.CanReadUInt())
|
||||
{
|
||||
return false; // Not enough bytes for the immediate value
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read the 32-bit immediate value
|
||||
uint imm32 = Decoder.ReadUInt32();
|
||||
|
||||
|
||||
// Format the immediate value
|
||||
string immStr = $"0x{imm32:X}";
|
||||
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"eax, {immStr}";
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,11 +11,11 @@ public class AddImmToRm32Handler : 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 AddImmToRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
public AddImmToRm32Handler(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 AddImmToRm32Handler : InstructionHandler
|
||||
{
|
||||
if (opcode != 0x81)
|
||||
return false;
|
||||
|
||||
|
||||
// Check if the reg field of the ModR/M byte is 0 (ADD)
|
||||
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 == 0; // 0 = ADD
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an ADD r/m32, imm32 instruction
|
||||
/// </summary>
|
||||
@ -47,33 +47,31 @@ public class AddImmToRm32Handler : InstructionHandler
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "add";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read the ModR/M byte
|
||||
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
|
||||
// Read the immediate value
|
||||
if (position + 3 >= Length)
|
||||
if (!Decoder.CanReadUInt())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read the immediate value in little-endian format
|
||||
var imm = Decoder.ReadUInt32();
|
||||
|
||||
|
||||
// Format the immediate value as expected by the tests (0x12345678)
|
||||
// Note: The bytes are reversed to match the expected format in the tests
|
||||
string immStr = $"0x{imm:X8}";
|
||||
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, {immStr}";
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,11 +11,11 @@ public class AddImmToRm32SignExtendedHandler : 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 AddImmToRm32SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
public AddImmToRm32SignExtendedHandler(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 AddImmToRm32SignExtendedHandler : InstructionHandler
|
||||
{
|
||||
if (opcode != 0x83)
|
||||
return false;
|
||||
|
||||
|
||||
// Check if the reg field of the ModR/M byte is 0 (ADD)
|
||||
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 == 0; // 0 = ADD
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an ADD r/m32, imm8 (sign-extended) instruction
|
||||
/// </summary>
|
||||
@ -47,45 +47,40 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "add";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read the ModR/M byte
|
||||
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
// Get the position after decoding the ModR/M byte
|
||||
position = Decoder.GetPosition();
|
||||
|
||||
|
||||
// Check if we have enough bytes for the immediate value
|
||||
if (position >= Length)
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read the immediate value as a signed byte and automatically sign-extend it to int
|
||||
int signExtendedImm = (sbyte)Decoder.ReadByte();
|
||||
|
||||
int imm = (sbyte) Decoder.ReadByte();
|
||||
|
||||
// Format the immediate value
|
||||
string immStr;
|
||||
if (signExtendedImm < 0)
|
||||
if (imm < 0)
|
||||
{
|
||||
// For negative values, use the full 32-bit representation (0xFFFFFFxx)
|
||||
immStr = $"0x{(uint)signExtendedImm:X8}";
|
||||
immStr = $"0x{(uint) imm:X8}";
|
||||
}
|
||||
else
|
||||
{
|
||||
// For positive values, use the regular format with leading zeros
|
||||
immStr = $"0x{signExtendedImm:X8}";
|
||||
immStr = $"0x{imm:X8}";
|
||||
}
|
||||
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, {immStr}";
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,7 @@ public class AddImmToRm8Handler : InstructionHandler
|
||||
|
||||
// Check if the reg field of the ModR/M byte is 0 (ADD)
|
||||
int position = Decoder.GetPosition();
|
||||
if (position >= Length)
|
||||
if (!Decoder.CanReadByte())
|
||||
return false;
|
||||
|
||||
byte modRM = CodeBuffer[position];
|
||||
@ -48,9 +48,7 @@ public class AddImmToRm8Handler : InstructionHandler
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "add";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -66,7 +64,7 @@ public class AddImmToRm8Handler : InstructionHandler
|
||||
}
|
||||
|
||||
// Read the immediate value
|
||||
if (position >= Length)
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -34,9 +34,7 @@ public class AddR32Rm32Handler : 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;
|
||||
}
|
||||
|
@ -34,9 +34,7 @@ public class AddRm32R32Handler : 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user