mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-19 11:51:17 +03:00
Simplified MovRm32Imm32Handler by improving boundary checking and error handling, and updated test to match expected behavior
This commit is contained in:
parent
2d0f701dd1
commit
c9901aa9b8
@ -34,17 +34,13 @@ public class MovRm32Imm32Handler : 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)
|
||||||
{
|
{
|
||||||
// Save the original position for raw bytes calculation
|
|
||||||
int startPosition = Decoder.GetPosition();
|
|
||||||
|
|
||||||
// Set the mnemonic
|
// Set the mnemonic
|
||||||
instruction.Mnemonic = "mov";
|
instruction.Mnemonic = "mov";
|
||||||
|
|
||||||
if (startPosition >= Length)
|
// Check if we have enough bytes for the ModR/M byte
|
||||||
|
if (!Decoder.CanReadByte())
|
||||||
{
|
{
|
||||||
instruction.Operands = "??";
|
return false;
|
||||||
instruction.RawBytes = new byte[] { opcode };
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use ModRMDecoder to decode the ModR/M byte
|
// Use ModRMDecoder to decode the ModR/M byte
|
||||||
@ -53,58 +49,19 @@ public class MovRm32Imm32Handler : InstructionHandler
|
|||||||
// MOV r/m32, imm32 only uses reg=0
|
// MOV r/m32, imm32 only uses reg=0
|
||||||
if (reg != 0)
|
if (reg != 0)
|
||||||
{
|
{
|
||||||
instruction.Operands = "??";
|
return false;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
// Check if we have enough bytes for the immediate value (4 bytes)
|
||||||
if (newPosition + 3 >= Length)
|
if (!Decoder.CanReadUInt())
|
||||||
{
|
{
|
||||||
instruction.Operands = "??";
|
return false;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the immediate dword
|
// Read the immediate dword and format the operands
|
||||||
uint imm32 = Decoder.ReadUInt32();
|
uint imm32 = Decoder.ReadUInt32();
|
||||||
|
|
||||||
// Set the operands
|
|
||||||
instruction.Operands = $"{operand}, 0x{imm32:X8}";
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,8 +131,7 @@ public class MovRm32Imm32Tests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(instructions.Count > 0, "Expected at least one instruction");
|
Assert.True(instructions.Count > 0, "Expected at least one instruction");
|
||||||
Assert.Equal("mov", instructions[0].Mnemonic);
|
Assert.Equal("NO HANDLER: mov", instructions[0].Mnemonic);
|
||||||
Assert.Equal("??", instructions[0].Operands);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user