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

@ -36,11 +36,9 @@ public class AndAlImmHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
// Read immediate value
if (position >= Length)
if (!Decoder.CanReadByte())
{
instruction.Operands = "al, ??";
return true;

View File

@ -36,11 +36,9 @@ public class AndEaxImmHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
// Read immediate value
if (position + 3 >= Length)
if (!Decoder.CanReadUInt())
{
instruction.Operands = "eax, ??";
return true;

View File

@ -11,11 +11,11 @@ public class AndImmToRm32Handler : 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 AndImmToRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndImmToRm32Handler(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 AndImmToRm32Handler : InstructionHandler
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 4 (AND)
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 = AND
}
/// <summary>
/// Decodes an AND r/m32, imm32 instruction
/// </summary>
@ -47,33 +47,31 @@ public class AndImmToRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
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;
}
}
}

View File

@ -11,11 +11,11 @@ public class AndImmToRm32SignExtendedHandler : 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 AndImmToRm32SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndImmToRm32SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -27,22 +27,22 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
{
return false;
}
// Check if we have enough bytes to read the ModR/M byte
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte to check the reg field (bits 5-3)
byte modRM = CodeBuffer[position];
int reg = (modRM >> 3) & 0x7;
// reg = 4 means AND operation
return reg == 4;
}
/// <summary>
/// Decodes an AND r/m32, imm8 (sign-extended) instruction
/// </summary>
@ -53,22 +53,18 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get the position after decoding the ModR/M byte
int position = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false; // Not enough bytes for the immediate value
}
// 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 destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
@ -81,23 +77,23 @@ public class AndImmToRm32SignExtendedHandler : InstructionHandler
// Memory operand already includes dword ptr prefix
destOperand = memOperand;
}
// Format the immediate value
string immStr;
if (signExtendedImm < 0)
if (imm < 0)
{
// For negative values, use the full 32-bit representation
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;
}
}
}

View File

@ -11,11 +11,11 @@ public class AndImmToRm8Handler : 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 AndImmToRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndImmToRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@ -27,22 +27,22 @@ public class AndImmToRm8Handler : InstructionHandler
{
return false;
}
// Check if we have enough bytes to read the ModR/M byte
int position = Decoder.GetPosition();
if (position >= Length)
if (Decoder.CanReadByte())
{
return false;
}
int position = Decoder.GetPosition();
// Read the ModR/M byte to check the reg field (bits 5-3)
byte modRM = CodeBuffer[position];
int reg = (modRM >> 3) & 0x7;
// reg = 4 means AND operation
return reg == 4;
}
/// <summary>
/// Decodes an AND r/m8, imm8 instruction
/// </summary>
@ -53,22 +53,18 @@ public class AndImmToRm8Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get the position after decoding the ModR/M byte
int position = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false; // Not enough bytes for the immediate value
}
// Read the immediate value
byte imm8 = Decoder.ReadByte();
// Format the destination operand based on addressing mode
string destOperand;
if (mod == 3) // Register addressing mode
@ -81,13 +77,13 @@ public class AndImmToRm8Handler : InstructionHandler
// Add byte ptr prefix for memory operands
destOperand = $"byte ptr {memOperand}";
}
// Format the immediate value
string immStr = $"0x{imm8:X2}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}
}

View File

@ -28,7 +28,7 @@ public class AndImmWithRm32Handler : InstructionHandler
// Check if the reg field of the ModR/M byte is 4 (AND)
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
return false;
byte modRM = CodeBuffer[position];
@ -55,7 +55,7 @@ public class AndImmWithRm32Handler : InstructionHandler
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
}

View File

@ -11,11 +11,11 @@ public class AndMemRegHandler : 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 AndMemRegHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndMemRegHandler(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 AndMemRegHandler : InstructionHandler
{
return opcode == 0x21;
}
/// <summary>
/// Decodes an AND r/m32, r32 instruction
/// </summary>
@ -36,20 +36,18 @@ public class AndMemRegHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For mod == 3, both operands are registers
if (mod == 3)
{
@ -60,4 +58,4 @@ public class AndMemRegHandler : InstructionHandler
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class AndR32Rm32Handler : 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 AndR32Rm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndR32Rm32Handler(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 AndR32Rm32Handler : InstructionHandler
{
return opcode == 0x23;
}
/// <summary>
/// Decodes an AND r32, r/m32 instruction
/// </summary>
@ -36,20 +36,18 @@ public class AndR32Rm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For mod == 3, both operands are registers
if (mod == 3)
{
@ -60,4 +58,4 @@ public class AndR32Rm32Handler : InstructionHandler
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class AndR8Rm8Handler : 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 AndR8Rm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndR8Rm8Handler(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 AndR8Rm8Handler : InstructionHandler
{
return opcode == 0x22;
}
/// <summary>
/// Decodes an AND r8, r/m8 instruction
/// </summary>
@ -36,20 +36,18 @@ public class AndR8Rm8Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// For mod == 3, both operands are registers
if (mod == 3)
{
@ -60,7 +58,7 @@ public class AndR8Rm8Handler : InstructionHandler
{
instruction.Operands = $"{regName}, byte ptr {memOperand}";
}
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class AndRm8R8Handler : 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 AndRm8R8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public AndRm8R8Handler(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 AndRm8R8Handler : InstructionHandler
{
return opcode == 0x20;
}
/// <summary>
/// Decodes an AND r/m8, r8 instruction
/// </summary>
@ -36,20 +36,18 @@ public class AndRm8R8Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "and";
int position = Decoder.GetPosition();
if (position >= Length)
if (!Decoder.CanReadByte())
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// For mod == 3, both operands are registers
if (mod == 3)
{
@ -60,7 +58,7 @@ public class AndRm8R8Handler : InstructionHandler
{
instruction.Operands = $"byte ptr {memOperand}, {regName}";
}
return true;
}
}
}