mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 00:18:02 +03:00
nice big refactor
This commit is contained in:
@ -11,11 +11,11 @@ public class SbbImmFromRm32Handler : 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 SbbImmFromRm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
public SbbImmFromRm32Handler(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 SbbImmFromRm32Handler : InstructionHandler
|
||||
{
|
||||
if (opcode != 0x81)
|
||||
return false;
|
||||
|
||||
|
||||
// Check if the reg field of the ModR/M byte is 3 (SBB)
|
||||
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 == 3; // 3 = SBB
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a SBB r/m32, imm32 instruction
|
||||
/// </summary>
|
||||
@ -47,49 +47,33 @@ public class SbbImmFromRm32Handler : InstructionHandler
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "sbb";
|
||||
|
||||
|
||||
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 3 for SBB
|
||||
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 imm32 = 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{imm32:X8}";
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, {immStr}";
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,11 +11,11 @@ public class SbbImmFromRm32SignExtendedHandler : 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 SbbImmFromRm32SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
public SbbImmFromRm32SignExtendedHandler(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 SbbImmFromRm32SignExtendedHandler : InstructionHandler
|
||||
{
|
||||
if (opcode != 0x83)
|
||||
return false;
|
||||
|
||||
|
||||
// Check if the reg field of the ModR/M byte is 3 (SBB)
|
||||
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 == 3; // 3 = SBB
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a SBB r/m32, imm8 (sign-extended) instruction
|
||||
/// </summary>
|
||||
@ -47,39 +47,29 @@ public class SbbImmFromRm32SignExtendedHandler : InstructionHandler
|
||||
{
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "sbb";
|
||||
|
||||
|
||||
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 3 for SBB
|
||||
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 (sign-extended from 8 to 32 bits)
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
sbyte imm8 = (sbyte)CodeBuffer[position];
|
||||
int imm32 = imm8; // Sign-extend to 32 bits
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
|
||||
// Sign-extend to 32 bits
|
||||
int imm32 = (sbyte) Decoder.ReadByte();
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"{destOperand}, 0x{imm32:X8}";
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user