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)
{
// This handler only handles opcode 0xF7
// The reg field check (for TEST operation) will be done in the Decode method
return opcode == 0xF7;
if (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>
@ -36,39 +50,24 @@ public class TestImmWithRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Set the mnemonic
instruction.Mnemonic = "test";
// Read the ModR/M byte
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
if (mod == 3)
{
destOperand = ModRMDecoder.GetRegisterName(rm, 32);
}
position = Decoder.GetPosition();
// Read the immediate value
if (position + 3 >= Length)
if (!Decoder.CanReadUInt())
{
return false;
}
// Read the immediate value
uint imm32 = Decoder.ReadUInt32();
// Set the operands

View File

@ -24,8 +24,22 @@ public class TestImmWithRm8Handler : InstructionHandler
public override bool CanHandle(byte opcode)
{
// This handler only handles opcode 0xF6
// The reg field check (for TEST operation) will be done in the Decode method
return opcode == 0xF6;
if (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>
@ -36,38 +50,26 @@ public class TestImmWithRm8Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
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
instruction.Mnemonic = "test";
// For direct register addressing (mod == 3), the r/m field specifies a register
if (mod == 3)
// Read the ModR/M byte
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);
}
// Use the ModR/M decoder for memory addressing
// Read the immediate value
if (position >= Length)
// Check if we have enough bytes for the immediate value
if (!Decoder.CanReadByte())
{
return false;
}
// Read the immediate value
byte imm8 = Decoder.ReadByte();
// Set the operands