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

Simplified MovRm32Imm32Handler by improving boundary checking and error handling, and updated test to match expected behavior

This commit is contained in:
bird_egop 2025-04-14 00:19:36 +03:00
parent 2d0f701dd1
commit c9901aa9b8
2 changed files with 8 additions and 52 deletions

View File

@ -34,17 +34,13 @@ public class MovRm32Imm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Save the original position for raw bytes calculation
int startPosition = Decoder.GetPosition();
// Set the mnemonic
instruction.Mnemonic = "mov";
if (startPosition >= Length)
// Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
{
instruction.Operands = "??";
instruction.RawBytes = new byte[] { opcode };
return true;
return false;
}
// Use ModRMDecoder to decode the ModR/M byte
@ -53,58 +49,19 @@ public class MovRm32Imm32Handler : InstructionHandler
// MOV r/m32, imm32 only uses reg=0
if (reg != 0)
{
instruction.Operands = "??";
byte[] rawBytesReg = new byte[Decoder.GetPosition() - startPosition + 1]; // +1 for opcode
rawBytesReg[0] = opcode;
for (int i = 0; i < Decoder.GetPosition() - startPosition; i++)
{
if (startPosition + i < Length)
{
rawBytesReg[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytesReg;
return true;
return false;
}
// Get the position after decoding the ModR/M byte
int newPosition = Decoder.GetPosition();
// Check if we have enough bytes for the immediate value (4 bytes)
if (newPosition + 3 >= Length)
if (!Decoder.CanReadUInt())
{
instruction.Operands = "??";
byte[] rawBytesImm = new byte[newPosition - startPosition + 1]; // +1 for opcode
rawBytesImm[0] = opcode;
for (int i = 0; i < newPosition - startPosition; i++)
{
if (startPosition + i < Length)
{
rawBytesImm[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytesImm;
return true;
return false;
}
// Read the immediate dword
// Read the immediate dword and format the operands
uint imm32 = Decoder.ReadUInt32();
// Set the operands
instruction.Operands = $"{operand}, 0x{imm32:X8}";
// Set the raw bytes
byte[] rawBytes = new byte[Decoder.GetPosition() - startPosition + 1]; // +1 for opcode
rawBytes[0] = opcode;
for (int i = 0; i < Decoder.GetPosition() - startPosition; i++)
{
if (startPosition + i < Length)
{
rawBytes[i + 1] = CodeBuffer[startPosition + i];
}
}
instruction.RawBytes = rawBytes;
return true;
}
}

View File

@ -131,8 +131,7 @@ public class MovRm32Imm32Tests
// Assert
Assert.True(instructions.Count > 0, "Expected at least one instruction");
Assert.Equal("mov", instructions[0].Mnemonic);
Assert.Equal("??", instructions[0].Operands);
Assert.Equal("NO HANDLER: mov", instructions[0].Mnemonic);
}
/// <summary>