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

nice big refactor

This commit is contained in:
bird_egop
2025-04-13 23:06:52 +03:00
parent 59df064ca4
commit 11a2cfada4
92 changed files with 981 additions and 1509 deletions

View File

@ -11,11 +11,11 @@ public class MovEaxMoffsHandler : 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 MovEaxMoffsHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MovEaxMoffsHandler(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 MovEaxMoffsHandler : InstructionHandler
{
return opcode == 0xA0 || opcode == 0xA1;
}
/// <summary>
/// Decodes a MOV EAX, moffs32 or MOV AL, moffs8 instruction
/// </summary>
@ -36,21 +36,24 @@ public class MovEaxMoffsHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Get the operand size and register name
int operandSize = (opcode == 0xA0) ? 8 : 32;
string regName = (opcode == 0xA0) ? "al" : "eax";
int operandSize = (opcode == 0xA0)
? 8
: 32;
string regName = ModRMDecoder.GetRegisterName(RegisterIndex.A, operandSize);
// Read the memory offset
uint offset = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
{
return false;
}
// Set the operands
instruction.Operands = $"{regName}, [0x{offset:X}]";
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class MovMemRegHandler : 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 MovMemRegHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MovMemRegHandler(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 MovMemRegHandler : InstructionHandler
{
return opcode == 0x88 || opcode == 0x89;
}
/// <summary>
/// Decodes a MOV r/m32, r32 or MOV r/m8, r8 instruction
/// </summary>
@ -36,24 +36,24 @@ public class MovMemRegHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "mov";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Determine operand size (0 = 8-bit, 1 = 32-bit)
bool operandSize32 = (opcode & 0x01) != 0;
int operandSize = operandSize32 ? 32 : 8;
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name based on size
string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
// For mod == 3, both operands are registers
if (mod == 3)
{
@ -64,7 +64,7 @@ public class MovMemRegHandler : InstructionHandler
{
instruction.Operands = $"{memOperand}, {regName}";
}
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class MovMoffsEaxHandler : 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 MovMoffsEaxHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MovMoffsEaxHandler(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 MovMoffsEaxHandler : InstructionHandler
{
return opcode == 0xA2 || opcode == 0xA3;
}
/// <summary>
/// Decodes a MOV moffs32, EAX or MOV moffs8, AL instruction
/// </summary>
@ -36,21 +36,22 @@ public class MovMoffsEaxHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Get the operand size and register name
int operandSize = (opcode == 0xA2) ? 8 : 32;
string regName = (opcode == 0xA2) ? "al" : "eax";
int operandSize = opcode == 0xA2 ? 8 : 32;
string regName = ModRMDecoder.GetRegisterName(RegisterIndex.A, operandSize);
// Read the memory offset
uint offset = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
{
return false;
}
// Set the operands
instruction.Operands = $"[0x{offset:X}], {regName}";
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class MovRegImm32Handler : 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 MovRegImm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MovRegImm32Handler(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 MovRegImm32Handler : InstructionHandler
{
return opcode >= 0xB8 && opcode <= 0xBF;
}
/// <summary>
/// Decodes a MOV r32, imm32 instruction
/// </summary>
@ -36,21 +36,21 @@ public class MovRegImm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07;
RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
if (Decoder.GetPosition() > Length)
{
return false;
}
// Set the operands
instruction.Operands = $"{regName}, 0x{imm32:X}";
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class MovRegImm8Handler : 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 MovRegImm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MovRegImm8Handler(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 MovRegImm8Handler : InstructionHandler
{
return opcode >= 0xB0 && opcode <= 0xB7;
}
/// <summary>
/// Decodes a MOV r8, imm8 instruction
/// </summary>
@ -36,21 +36,22 @@ public class MovRegImm8Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "mov";
// Register is encoded in the low 3 bits of the opcode
int reg = opcode & 0x07;
RegisterIndex reg = (RegisterIndex) (opcode & 0x07);
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// Read the immediate value
byte imm8 = Decoder.ReadByte();
if (Decoder.GetPosition() > Length)
{
return false;
}
// Set the operands
instruction.Operands = $"{regName}, 0x{imm8:X2}";
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class MovRegMemHandler : 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 MovRegMemHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public MovRegMemHandler(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 MovRegMemHandler : InstructionHandler
{
return opcode == 0x8A || opcode == 0x8B;
}
/// <summary>
/// Decodes a MOV r32, r/m32 or MOV r8, r/m8 instruction
/// </summary>
@ -36,42 +36,36 @@ public class MovRegMemHandler : InstructionHandler
{
// Save the original position for raw bytes calculation
int startPosition = Decoder.GetPosition();
// Set the mnemonic
instruction.Mnemonic = "mov";
if (startPosition >= Length)
{
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode };
instruction.RawBytes = new byte[] {opcode};
return true;
}
// Determine operand size (0 = 8-bit, 1 = 32-bit)
bool operandSize32 = (opcode & 0x01) != 0;
int operandSize = operandSize32 ? 32 : 8;
int operandSize = operandSize32
? 32
: 8;
// Use ModRMDecoder to decode the ModR/M byte
var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM(false); // false for 32-bit operand
var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM(); // false for 32-bit operand
// Get register name based on size
string regName;
if (operandSize == 8)
{
regName = GetRegister8(reg);
}
else
{
regName = GetRegister32(reg);
}
string regName = ModRMDecoder.GetRegisterName(reg, operandSize);
// Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition();
// Set the operands - register is the destination, r/m is the source (for 0x8B)
// This matches the correct x86 instruction format: MOV r32, r/m32
instruction.Operands = $"{regName}, {rmOperand}";
// Set the raw bytes
int totalBytes = newPosition - startPosition + 1; // +1 for opcode
byte[] rawBytes = new byte[totalBytes];
@ -83,8 +77,9 @@ public class MovRegMemHandler : InstructionHandler
rawBytes[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytes;
return true;
}
}
}

View File

@ -48,7 +48,7 @@ public class MovRm32Imm32Handler : InstructionHandler
}
// Use ModRMDecoder to decode the ModR/M byte
var (mod, reg, rm, rmOperand) = ModRMDecoder.ReadModRM(false);
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM(false);
// MOV r/m32, imm32 only uses reg=0
if (reg != 0)
@ -88,15 +88,10 @@ public class MovRm32Imm32Handler : InstructionHandler
}
// Read the immediate dword
byte b0 = CodeBuffer[newPosition];
byte b1 = CodeBuffer[newPosition + 1];
byte b2 = CodeBuffer[newPosition + 2];
byte b3 = CodeBuffer[newPosition + 3];
uint imm32 = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24));
Decoder.SetPosition(newPosition + 4);
uint imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{rmOperand}, 0x{imm32:X8}";
instruction.Operands = $"{operand}, 0x{imm32:X8}";
// Set the raw bytes
byte[] rawBytes = new byte[Decoder.GetPosition() - startPosition + 1]; // +1 for opcode

View File

@ -34,22 +34,18 @@ public class MovRm8Imm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Save the original position for raw bytes calculation
int startPosition = Decoder.GetPosition();
int position = startPosition;
// Set the mnemonic
instruction.Mnemonic = "mov";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
// Extract the fields from the ModR/M byte
byte mod = (byte)((modRM & 0xC0) >> 6);
byte reg = (byte)((modRM & 0x38) >> 3);
byte rm = (byte)(modRM & 0x07);
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// MOV r/m8, imm8 only uses reg=0
if (reg != 0)
@ -57,98 +53,28 @@ public class MovRm8Imm8Handler : InstructionHandler
return false;
}
// Track the bytes needed for this instruction
int bytesNeeded = 1; // ModR/M byte
// Process SIB byte if needed
byte sib = 0;
if (mod != 3 && rm == 4) // SIB byte present
// For direct register addressing (mod == 3), use 8-bit register names
if (mod == 3)
{
if (position >= Length)
{
return false;
}
sib = CodeBuffer[position++];
bytesNeeded++;
// Use 8-bit register names for direct register addressing
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else
{
// Replace the size prefix with "byte ptr" for memory operands
destOperand = destOperand.Replace("dword ptr", "byte ptr");
}
// Handle displacement
int dispSize = 0;
if (mod == 0 && rm == 5) // 32-bit displacement
{
dispSize = 4;
}
else if (mod == 1) // 8-bit displacement
{
dispSize = 1;
}
else if (mod == 2) // 32-bit displacement
{
dispSize = 4;
}
// Check if we have enough bytes for the displacement
if (position + dispSize > Length)
// Read the immediate value
if (Decoder.GetPosition() >= Length)
{
return false;
}
// Skip over the displacement bytes
position += dispSize;
bytesNeeded += dispSize;
byte imm8 = Decoder.ReadByte();
// Read the immediate byte
if (position >= Length)
{
return false;
}
byte imm8 = CodeBuffer[position++];
bytesNeeded++; // Immediate byte
// Update the decoder position
Decoder.SetPosition(position);
// Set the mnemonic
instruction.Mnemonic = "mov";
// Use ModRMDecoder to get the operand string
var modRMDecoder = new ModRMDecoder(CodeBuffer, Decoder, Length);
// Reset the decoder position to after the ModR/M byte
Decoder.SetPosition(startPosition + 1);
// Get the operand string
string operand;
if (mod != 3) // Memory operand
{
string memOperand = modRMDecoder.DecodeModRM(mod, rm, false);
// Replace the size prefix with "byte ptr"
operand = memOperand.Replace("dword ptr", "byte ptr");
}
else // Register operand
{
operand = GetRegister8(rm);
}
// Set the operands
instruction.Operands = $"{operand}, 0x{imm8:X2}";
// Set the raw bytes
byte[] rawBytes = new byte[bytesNeeded + 1]; // +1 for opcode
rawBytes[0] = opcode;
for (int i = 0; i < bytesNeeded; i++)
{
if (startPosition + i < Length)
{
rawBytes[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytes;
// Restore the decoder position
Decoder.SetPosition(position);
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
return true;
}