0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-08-04 02:16:33 +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 TestAlImmHandler : 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 TestAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public TestAlImmHandler(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 TestAlImmHandler : InstructionHandler
{
return opcode == 0xA8;
}
/// <summary>
/// Decodes a TEST AL, imm8 instruction
/// </summary>
@@ -36,21 +36,20 @@ public class TestAlImmHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "test";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the immediate value
byte imm8 = CodeBuffer[position];
Decoder.SetPosition(position + 1);
byte imm8 = Decoder.ReadByte();
// Set the operands
instruction.Operands = $"al, 0x{imm8:X2}";
return true;
}
}
}

View File

@@ -11,11 +11,11 @@ public class TestEaxImmHandler : 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 TestEaxImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public TestEaxImmHandler(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 TestEaxImmHandler : InstructionHandler
{
return opcode == 0xA9;
}
/// <summary>
/// Decodes a TEST EAX, imm32 instruction
/// </summary>
@@ -36,28 +36,20 @@ public class TestEaxImmHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "test";
int position = Decoder.GetPosition();
if (position + 3 >= Length)
{
return false;
}
// Read the immediate value - x86 is little-endian, so we need to read the bytes in the correct order
byte b0 = CodeBuffer[position];
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Combine the bytes to form a 32-bit immediate value
uint imm32 = (uint)(b0 | (b1 << 8) | (b2 << 16) | (b3 << 24));
Decoder.SetPosition(position + 4);
var imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"eax, 0x{imm32:X8}";
return true;
}
}
}

View File

@@ -11,11 +11,11 @@ public class TestImmWithRm32Handler : 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 TestImmWithRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public TestImmWithRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@@ -27,7 +27,7 @@ public class TestImmWithRm32Handler : InstructionHandler
// The reg field check (for TEST operation) will be done in the Decode method
return opcode == 0xF7;
}
/// <summary>
/// Decodes a TEST r/m32, imm32 instruction
/// </summary>
@@ -37,58 +37,44 @@ public class TestImmWithRm32Handler : InstructionHandler
public override bool Decode(byte opcode, Instruction instruction)
{
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); // Should be 0 for TEST
byte rm = (byte)(modRM & 0x07);
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Check if the reg field is 0 (TEST operation)
if (reg != 0)
{
return false; // Not a TEST instruction
}
// Set the mnemonic
instruction.Mnemonic = "test";
Decoder.SetPosition(position);
// Get the operand based on the addressing mode
string destOperand;
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
{
destOperand = GetRegister32(rm);
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else
{
// Use the ModR/M decoder for memory addressing
destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
}
position = Decoder.GetPosition();
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
// Read the immediate value using BitConverter
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position);
Decoder.SetPosition(position + 4);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
return true;
}
}
}

View File

@@ -11,11 +11,11 @@ public class TestImmWithRm8Handler : 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 TestImmWithRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public TestImmWithRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
@@ -27,7 +27,7 @@ public class TestImmWithRm8Handler : InstructionHandler
// The reg field check (for TEST operation) will be done in the Decode method
return opcode == 0xF6;
}
/// <summary>
/// Decodes a TEST r/m8, imm8 instruction
/// </summary>
@@ -37,57 +37,42 @@ public class TestImmWithRm8Handler : InstructionHandler
public override bool Decode(byte opcode, Instruction instruction)
{
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); // Should be 0 for TEST
byte rm = (byte)(modRM & 0x07);
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(true);
// Check if the reg field is 0 (TEST operation)
if (reg != 0)
if (reg != RegisterIndex.A)
{
return false; // Not a TEST instruction
}
// Set the mnemonic
instruction.Mnemonic = "test";
Decoder.SetPosition(position);
// Get the operand based on the addressing mode
string destOperand;
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
{
destOperand = GetRegister8(rm);
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
}
else
{
// Use the ModR/M decoder for memory addressing
destOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
}
// Use the ModR/M decoder for memory addressing
// Read the immediate value
if (position >= Length)
{
return false;
}
byte imm8 = CodeBuffer[position];
Decoder.SetPosition(position + 1);
byte imm8 = Decoder.ReadByte();
// Set the operands
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
return true;
}
}
}

View File

@@ -11,11 +11,11 @@ public class TestRegMem8Handler : 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 TestRegMem8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public TestRegMem8Handler(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 TestRegMem8Handler : InstructionHandler
{
return opcode == 0x84;
}
/// <summary>
/// Decodes a TEST r/m8, r8 instruction
/// </summary>
@@ -36,30 +36,24 @@ public class TestRegMem8Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "test";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(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);
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
{
// Get the register names
string rmReg = GetRegister8(rm);
string regReg = GetRegister8(reg);
string rmReg = ModRMDecoder.GetRegisterName(rm, 8);
string regReg = ModRMDecoder.GetRegisterName(reg, 8);
// Set the operands (TEST r/m8, r8)
// In x86 assembly, the TEST instruction has the operand order r/m8, r8
// According to Ghidra and standard x86 assembly convention, it should be TEST CL,AL
@@ -68,16 +62,13 @@ public class TestRegMem8Handler : InstructionHandler
}
else
{
// Decode the memory operand
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, true);
// Get the register name
string regReg = GetRegister8(reg);
string regReg = ModRMDecoder.GetRegisterName(reg, 8);
// Set the operands (TEST r/m8, r8)
instruction.Operands = $"{memOperand}, {regReg}";
instruction.Operands = $"{destOperand}, {regReg}";
}
return true;
}
}
}

View File

@@ -11,11 +11,11 @@ public class TestRegMemHandler : 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 TestRegMemHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public TestRegMemHandler(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 TestRegMemHandler : InstructionHandler
{
return opcode == 0x85;
}
/// <summary>
/// Decodes a TEST r/m32, r32 instruction
/// </summary>
@@ -36,30 +36,24 @@ public class TestRegMemHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "test";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[position++];
Decoder.SetPosition(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);
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
{
// Get the register names
string rmReg = GetRegister32(rm);
string regReg = GetRegister32(reg);
string rmReg = ModRMDecoder.GetRegisterName(rm, 32);
string regReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands (TEST r/m32, r32)
// In x86 assembly, the TEST instruction has the operand order r/m32, r32
// According to Ghidra and standard x86 assembly convention, it should be TEST ECX,EAX
@@ -68,16 +62,13 @@ public class TestRegMemHandler : InstructionHandler
}
else
{
// Decode the memory operand
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Get the register name
string regReg = GetRegister32(reg);
string regReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands (TEST r/m32, r32)
instruction.Operands = $"{memOperand}, {regReg}";
instruction.Operands = $"{destOperand}, {regReg}";
}
return true;
}
}
}