0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-19 07:59:47 +03:00

Simplified TestRegMemHandler by improving boundary checking and removing redundant code

This commit is contained in:
bird_egop
2025-04-14 00:56:57 +03:00
parent d3d2c4c63f
commit 4567465570

View File

@ -37,9 +37,8 @@ public class TestRegMemHandler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "test"; instruction.Mnemonic = "test";
int position = Decoder.GetPosition(); // Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
if (position >= Length)
{ {
return false; return false;
} }
@ -47,26 +46,19 @@ public class TestRegMemHandler : InstructionHandler
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// For direct register addressing (mod == 3), the r/m field specifies a register // Get the register name for the reg field
string regOperand = ModRMDecoder.GetRegisterName(reg, 32);
// For direct register addressing (mod == 3), get the r/m register name
if (mod == 3) if (mod == 3)
{ {
// Get the register names string rmOperand = ModRMDecoder.GetRegisterName(rm, 32);
string rmReg = ModRMDecoder.GetRegisterName(rm, 32); instruction.Operands = $"{rmOperand}, {regOperand}";
string regReg = ModRMDecoder.GetRegisterName(reg, 32);
// Set the operands (TEST r/m32, r32)
// In x86 assembly, the TEST instruction has the operand order r/m32, r32
// According to Ghidra and standard x86 assembly convention, it should be TEST ECX,EAX
// where ECX is the r/m operand and EAX is the reg operand
instruction.Operands = $"{rmReg}, {regReg}";
} }
else else
{ {
// Get the register name // For memory operands, use the decoded operand string
string regReg = ModRMDecoder.GetRegisterName(reg, 32); instruction.Operands = $"{destOperand}, {regOperand}";
// Set the operands (TEST r/m32, r32)
instruction.Operands = $"{destOperand}, {regReg}";
} }
return true; return true;