0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 11:51:17 +03:00

Simplified TEST instruction handlers by removing special cases and improving code structure

This commit is contained in:
bird_egop 2025-04-14 00:53:16 +03:00
parent dae52fc3ec
commit b7c6092b7f
2 changed files with 47 additions and 46 deletions

View File

@ -24,8 +24,22 @@ public class TestImmWithRm32Handler : InstructionHandler
public override bool CanHandle(byte opcode) public override bool CanHandle(byte opcode)
{ {
// This handler only handles opcode 0xF7 // This handler only handles opcode 0xF7
// The reg field check (for TEST operation) will be done in the Decode method if (opcode != 0xF7)
return opcode == 0xF7; {
return false;
}
// Check if we have enough bytes to read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the reg field is 0 (TEST operation)
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = TEST
} }
/// <summary> /// <summary>
@ -36,39 +50,24 @@ public class TestImmWithRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
int position = Decoder.GetPosition(); // Set the mnemonic
instruction.Mnemonic = "test";
if (position >= Length)
{
return false;
}
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); 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";
// For direct register addressing (mod == 3), the r/m field specifies a register // For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3) if (mod == 3)
{ {
destOperand = ModRMDecoder.GetRegisterName(rm, 32); destOperand = ModRMDecoder.GetRegisterName(rm, 32);
} }
position = Decoder.GetPosition();
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (!Decoder.CanReadUInt())
{ {
return false; return false;
} }
// Read the immediate value
uint imm32 = Decoder.ReadUInt32(); uint imm32 = Decoder.ReadUInt32();
// Set the operands // Set the operands

View File

@ -24,8 +24,22 @@ public class TestImmWithRm8Handler : InstructionHandler
public override bool CanHandle(byte opcode) public override bool CanHandle(byte opcode)
{ {
// This handler only handles opcode 0xF6 // This handler only handles opcode 0xF6
// The reg field check (for TEST operation) will be done in the Decode method if (opcode != 0xF6)
return opcode == 0xF6; {
return false;
}
// Check if we have enough bytes to read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Check if the reg field is 0 (TEST operation)
byte modRM = CodeBuffer[Decoder.GetPosition()];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 0; // 0 = TEST
} }
/// <summary> /// <summary>
@ -36,38 +50,26 @@ public class TestImmWithRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(true);
// Check if the reg field is 0 (TEST operation)
if (reg != RegisterIndex.A)
{
return false; // Not a TEST instruction
}
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "test"; instruction.Mnemonic = "test";
// For direct register addressing (mod == 3), the r/m field specifies a register // Read the ModR/M byte
if (mod == 3) var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the destination operand based on addressing mode
if (mod == 3) // Register operand
{ {
// For direct register addressing, use the correct 8-bit register name
destOperand = ModRMDecoder.GetRegisterName(rm, 8); destOperand = ModRMDecoder.GetRegisterName(rm, 8);
} }
// Use the ModR/M decoder for memory addressing // Check if we have enough bytes for the immediate value
// Read the immediate value if (!Decoder.CanReadByte())
if (position >= Length)
{ {
return false; return false;
} }
// Read the immediate value
byte imm8 = Decoder.ReadByte(); byte imm8 = Decoder.ReadByte();
// Set the operands // Set the operands