mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 08:18:36 +03:00
nice big refactor
This commit is contained in:
@ -68,17 +68,7 @@ public class AddEaxImmHandler : InstructionHandler
|
||||
}
|
||||
|
||||
// Read the 32-bit immediate value
|
||||
uint imm32 = 0;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
if (startPosition + i < Length)
|
||||
{
|
||||
imm32 |= (uint)(CodeBuffer[startPosition + i] << (i * 8));
|
||||
}
|
||||
}
|
||||
|
||||
// Advance the decoder position
|
||||
Decoder.SetPosition(startPosition + 4);
|
||||
uint imm32 = Decoder.ReadUInt32();
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"eax, 0x{imm32:X8}";
|
||||
|
@ -56,16 +56,7 @@ public class AddImmToRm32Handler : 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); // Should be 0 for ADD
|
||||
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)
|
||||
@ -74,18 +65,11 @@ public class AddImmToRm32Handler : InstructionHandler
|
||||
}
|
||||
|
||||
// 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}";
|
||||
|
@ -59,24 +59,19 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
|
||||
}
|
||||
|
||||
// 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); // Should be 0 for ADD
|
||||
byte rm = (byte)(modRM & 0x07);
|
||||
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
// 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
|
||||
if (mod != 3 && rm == RegisterIndex.Si) // SIB byte present
|
||||
{
|
||||
if (startPosition + bytesNeeded >= Length)
|
||||
{
|
||||
instruction.Operands = "??";
|
||||
instruction.RawBytes = new byte[] { opcode, modRM };
|
||||
instruction.RawBytes = new byte[] { opcode, CodeBuffer[startPosition] };
|
||||
return true;
|
||||
}
|
||||
sib = CodeBuffer[startPosition + bytesNeeded];
|
||||
@ -85,7 +80,7 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
|
||||
|
||||
// Handle displacement
|
||||
int dispSize = 0;
|
||||
if (mod == 0 && rm == 5) // 32-bit displacement
|
||||
if (mod == 0 && rm == RegisterIndex.Di) // 32-bit displacement
|
||||
{
|
||||
dispSize = 4;
|
||||
}
|
||||
@ -102,21 +97,15 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
|
||||
if (startPosition + bytesNeeded + dispSize >= Length)
|
||||
{
|
||||
instruction.Operands = "??";
|
||||
instruction.RawBytes = new byte[] { opcode, modRM };
|
||||
instruction.RawBytes = new byte[] { opcode, CodeBuffer[startPosition] };
|
||||
return true;
|
||||
}
|
||||
|
||||
bytesNeeded += dispSize; // Add displacement bytes
|
||||
|
||||
// Use ModRMDecoder to decode the destination operand
|
||||
var modRMDecoder = new ModRMDecoder(CodeBuffer, Decoder, Length);
|
||||
|
||||
// Set the decoder position to after the ModR/M byte
|
||||
Decoder.SetPosition(startPosition + 1);
|
||||
|
||||
// Decode the destination operand
|
||||
string destOperand = modRMDecoder.DecodeModRM(mod, rm, false);
|
||||
|
||||
|
||||
// Get the position after decoding the ModR/M byte
|
||||
int newPosition = Decoder.GetPosition();
|
||||
|
||||
@ -141,15 +130,21 @@ public class AddImmToRm32SignExtendedHandler : InstructionHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
// Read the immediate value as a signed byte and sign-extend it
|
||||
sbyte imm8 = (sbyte)CodeBuffer[newPosition];
|
||||
newPosition++; // Advance past the immediate byte
|
||||
// Read the immediate value as a signed byte and automatically sign-extend it to int
|
||||
int signExtendedImm = (sbyte)Decoder.ReadByte();
|
||||
|
||||
// Set the decoder position
|
||||
Decoder.SetPosition(newPosition);
|
||||
|
||||
// Format the immediate value as a 32-bit hex value (sign-extended)
|
||||
string immStr = $"0x{(uint)imm8:X8}";
|
||||
// Format the immediate value as a 32-bit hex value
|
||||
string immStr;
|
||||
if (signExtendedImm < 0)
|
||||
{
|
||||
// For negative values, use the full 32-bit representation (0xFFFFFFxx)
|
||||
immStr = $"0x{(uint)signExtendedImm:X8}";
|
||||
}
|
||||
else
|
||||
{
|
||||
// For positive values, use the regular format
|
||||
immStr = $"0x{signExtendedImm:X8}";
|
||||
}
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, {immStr}";
|
||||
|
@ -11,11 +11,11 @@ public class AddImmToRm8Handler : 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 AddImmToRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
public AddImmToRm8Handler(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 AddImmToRm8Handler : InstructionHandler
|
||||
{
|
||||
if (opcode != 0x80)
|
||||
return false;
|
||||
|
||||
|
||||
// Check if the reg field of the ModR/M byte is 0 (ADD)
|
||||
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 == 0; // 0 = ADD
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an ADD r/m8, imm8 instruction
|
||||
/// </summary>
|
||||
@ -47,49 +47,35 @@ public class AddImmToRm8Handler : InstructionHandler
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "add";
|
||||
|
||||
|
||||
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 ADD
|
||||
byte rm = (byte)(modRM & 0x07);
|
||||
|
||||
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
// For direct register addressing (mod == 3), use 8-bit register names
|
||||
string destOperand;
|
||||
if (mod == 3)
|
||||
{
|
||||
// Use 8-bit register names for direct register addressing
|
||||
destOperand = GetRegister8(rm);
|
||||
destOperand = ModRMDecoder.GetRegisterName(rm, 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use ModR/M decoder for memory addressing
|
||||
destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
|
||||
}
|
||||
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
|
||||
// Read the immediate value
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
byte imm8 = CodeBuffer[position++];
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
|
||||
byte imm8 = Decoder.ReadByte();
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, 0x{imm8:X2}";
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -42,19 +42,13 @@ public class AddR32Rm32Handler : 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 = "add";
|
||||
|
||||
// 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
|
||||
@ -64,7 +58,7 @@ public class AddR32Rm32Handler : InstructionHandler
|
||||
}
|
||||
else // Register operand
|
||||
{
|
||||
string rmName = GetRegister32(rm);
|
||||
string rmName = ModRMDecoder.GetRegisterName(rm, 32);
|
||||
instruction.Operands = $"{regName}, {rmName}";
|
||||
}
|
||||
|
||||
|
@ -42,19 +42,13 @@ public class AddRm32R32Handler : 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 = "add";
|
||||
|
||||
// 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
|
||||
@ -64,7 +58,7 @@ public class AddRm32R32Handler : InstructionHandler
|
||||
}
|
||||
else // Register operand
|
||||
{
|
||||
string rmName = GetRegister32(rm);
|
||||
string rmName = ModRMDecoder.GetRegisterName(rm, 32);;
|
||||
instruction.Operands = $"{rmName}, {regName}";
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user