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 CmpAlImmHandler : 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 CmpAlImmHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public CmpAlImmHandler(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 CmpAlImmHandler : InstructionHandler
{
return opcode == 0x3C;
}
/// <summary>
/// Decodes a CMP AL, imm8 instruction
/// </summary>
@ -36,21 +36,21 @@ public class CmpAlImmHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the immediate value
byte imm8 = CodeBuffer[position++];
byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"al, 0x{imm8:X2}";
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class CmpImmWithRm32Handler : 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 CmpImmWithRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public CmpImmWithRm32Handler(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 CmpImmWithRm32Handler : InstructionHandler
{
if (opcode != 0x81)
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP
}
/// <summary>
/// Decodes a CMP r/m32, imm32 instruction
/// </summary>
@ -47,49 +47,33 @@ public class CmpImmWithRm32Handler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
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); // Should be 7 for CMP
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the immediate value
if (position + 3 >= Length)
{
return false;
}
// Read the immediate value in little-endian format
byte b0 = CodeBuffer[position];
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
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{b3:X2}{b2:X2}{b1:X2}{b0:X2}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
string immStr = $"0x{imm:X8}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}
}

View File

@ -11,11 +11,11 @@ public class CmpImmWithRm32SignExtendedHandler : 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 CmpImmWithRm32SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
public CmpImmWithRm32SignExtendedHandler(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 CmpImmWithRm32SignExtendedHandler : InstructionHandler
{
if (opcode != 0x83)
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP
}
/// <summary>
/// Decodes a CMP r/m32, imm8 (sign-extended) instruction
/// </summary>
@ -47,39 +47,30 @@ public class CmpImmWithRm32SignExtendedHandler : InstructionHandler
{
// Set the mnemonic
instruction.Mnemonic = "cmp";
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); // Should be 7 for CMP
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the immediate value
if (position >= Length)
{
return false;
}
// Read the immediate value as a signed byte and sign-extend it
sbyte imm8 = (sbyte)CodeBuffer[position++];
sbyte imm8 = (sbyte) Decoder.ReadByte();
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, 0x{(uint)imm8:X2}";
instruction.Operands = $"{destOperand}, 0x{(uint) imm8:X2}";
return true;
}
}
}

View File

@ -25,15 +25,15 @@ public class CmpImmWithRm8Handler : InstructionHandler
{
if (opcode != 0x80)
return false;
// Check if the reg field of the ModR/M byte is 7 (CMP)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
byte reg = (byte) ((modRM & 0x38) >> 3);
return reg == 7; // 7 = CMP
}
@ -47,39 +47,30 @@ public class CmpImmWithRm8Handler : InstructionHandler
{
// Save the original position for raw bytes calculation
int startPosition = Decoder.GetPosition();
// Set the mnemonic
instruction.Mnemonic = "cmp";
if (startPosition >= Length)
{
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode };
instruction.RawBytes = new byte[] {opcode};
return true;
}
// Read the ModR/M byte
byte modRM = CodeBuffer[startPosition];
// 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();
// CMP r/m8, imm8 is encoded as 80 /7
if (reg != 7)
if (reg != RegisterIndex.Bp)
{
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode, modRM };
return true;
}
// Use ModRMDecoder to decode the ModR/M byte
var (_, _, _, rmOperand) = ModRMDecoder.ReadModRM(false);
// Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value
if (newPosition >= Length)
{
@ -93,36 +84,36 @@ public class CmpImmWithRm8Handler : InstructionHandler
rawBytesImm[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytesImm;
return true;
}
// Read the immediate byte
byte imm8 = CodeBuffer[newPosition];
Decoder.SetPosition(newPosition + 1);
byte imm8 = Decoder.ReadByte();
// Replace the size prefix with "byte ptr"
string operand;
if (rmOperand.StartsWith("qword ptr "))
if (destOperand.StartsWith("qword ptr "))
{
operand = rmOperand.Replace("qword ptr ", "byte ptr ");
operand = destOperand.Replace("qword ptr ", "byte ptr ");
}
else if (rmOperand.StartsWith("dword ptr "))
else if (destOperand.StartsWith("dword ptr "))
{
operand = rmOperand.Replace("dword ptr ", "byte ptr ");
operand = destOperand.Replace("dword ptr ", "byte ptr ");
}
else if (mod != 3) // Memory operand without prefix
{
operand = $"byte ptr {rmOperand}";
operand = $"byte ptr {destOperand}";
}
else // Register operand
{
operand = GetRegister8(rm);
operand = ModRMDecoder.GetRegisterName(rm, 8);
}
// Set the operands
instruction.Operands = $"{operand}, 0x{imm8:X2}";
// Set the raw bytes
byte[] rawBytes = new byte[newPosition - startPosition + 2]; // +1 for opcode, +1 for immediate
rawBytes[0] = opcode;
@ -133,9 +124,10 @@ public class CmpImmWithRm8Handler : InstructionHandler
rawBytes[i + 1] = CodeBuffer[startPosition + i];
}
}
rawBytes[rawBytes.Length - 1] = imm8;
instruction.RawBytes = rawBytes;
return true;
}
}
}

View File

@ -42,32 +42,23 @@ public class CmpR32Rm32Handler : InstructionHandler
}
// 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();
// Set the mnemonic
instruction.Mnemonic = "cmp";
// Get the register name
string regName = GetRegister32(reg);
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand
if (mod != 3) // Memory operand
// For register operands, set the operand
if (mod == 3)
{
string operand = ModRMDecoder.DecodeModRM(mod, rm, false);
instruction.Operands = $"{regName}, {operand}";
}
else // Register operand
{
string rmName = GetRegister32(rm);
instruction.Operands = $"{regName}, {rmName}";
// Register operand
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
instruction.Operands = $"{regName}, {destOperand}";
return true;
}
}

View File

@ -37,118 +37,34 @@ public class CmpRm32R32Handler : InstructionHandler
// Set the mnemonic
instruction.Mnemonic = "cmp";
int position = Decoder.GetPosition();
// Save the original position to properly handle the ModR/M byte
int originalPosition = Decoder.GetPosition();
if (position >= Length)
if (originalPosition >= 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); // Top 2 bits
byte reg = (byte)((modRM & 0x38) >> 3); // Middle 3 bits
byte rm = (byte)(modRM & 0x07); // Bottom 3 bits
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the register name for the reg field
string regName = GetRegister32(reg);
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// Handle the different addressing modes
string rmOperand;
// Use the destOperand directly from ModRMDecoder
string rmOperand = destOperand;
if (mod == 3) // Direct register addressing
// If it's a direct register operand, we need to remove the size prefix
if (mod == 3)
{
// Get the register name for the r/m field
rmOperand = GetRegister32(rm);
rmOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
else // Memory addressing
else if (rmOperand.StartsWith("dword ptr "))
{
// Handle SIB byte if needed
if (mod != 3 && rm == 4) // SIB byte present
{
if (position >= Length)
{
return false;
}
byte sib = CodeBuffer[position++];
// Extract the fields from the SIB byte
byte scale = (byte)((sib & 0xC0) >> 6);
byte index = (byte)((sib & 0x38) >> 3);
byte base_ = (byte)(sib & 0x07);
// TODO: Handle SIB byte properly
rmOperand = $"[complex addressing]";
}
else if (mod == 0 && rm == 5) // Displacement only addressing
{
if (position + 3 >= Length)
{
return false;
}
// Read the 32-bit displacement
uint disp = (uint)(CodeBuffer[position] |
(CodeBuffer[position + 1] << 8) |
(CodeBuffer[position + 2] << 16) |
(CodeBuffer[position + 3] << 24));
position += 4;
rmOperand = $"[0x{disp:X8}]";
}
else // Simple addressing modes
{
string baseReg = GetRegister32(rm);
if (mod == 0) // No displacement
{
rmOperand = $"[{baseReg}]";
}
else // Displacement
{
uint disp;
if (mod == 1) // 8-bit displacement
{
if (position >= Length)
{
return false;
}
// Sign-extend the 8-bit displacement
sbyte dispByte = (sbyte)CodeBuffer[position++];
disp = (uint)(int)dispByte;
// Format the displacement
string dispStr = dispByte < 0 ? $"-0x{-dispByte:X2}" : $"0x{dispByte:X2}";
rmOperand = $"[{baseReg}+{dispStr}]";
}
else // 32-bit displacement
{
if (position + 3 >= Length)
{
return false;
}
// Read the 32-bit displacement
disp = (uint)(CodeBuffer[position] |
(CodeBuffer[position + 1] << 8) |
(CodeBuffer[position + 2] << 16) |
(CodeBuffer[position + 3] << 24));
position += 4;
rmOperand = $"[{baseReg}+0x{disp:X8}]";
}
}
}
// Remove the "dword ptr " prefix as we'll handle the operands differently
rmOperand = rmOperand.Substring(10);
}
// Update the decoder position
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{rmOperand}, {regName}";