mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-19 11:51:17 +03:00
Fixed segment override prefix handling for 0x64 (FS) opcode with tests
This commit is contained in:
parent
b4a85d2839
commit
bfaeba0d5f
@ -86,7 +86,7 @@ public class InstructionDecoder
|
|||||||
_addressSizePrefix = true;
|
_addressSizePrefix = true;
|
||||||
_position++;
|
_position++;
|
||||||
}
|
}
|
||||||
else if (prefix >= 0x26 && prefix <= 0x3E && (prefix & 0x7) == 0x6) // Segment override prefix
|
else if ((prefix >= 0x26 && prefix <= 0x3E && (prefix & 0x7) == 0x6) || prefix == 0x64 || prefix == 0x65) // Segment override prefix
|
||||||
{
|
{
|
||||||
_segmentOverridePrefix = true;
|
_segmentOverridePrefix = true;
|
||||||
switch (prefix)
|
switch (prefix)
|
||||||
@ -118,6 +118,21 @@ public class InstructionDecoder
|
|||||||
|
|
||||||
if (_position >= _length)
|
if (_position >= _length)
|
||||||
{
|
{
|
||||||
|
// If we reached the end of the buffer while processing prefixes,
|
||||||
|
// create an instruction with just the prefix information
|
||||||
|
if (_segmentOverridePrefix)
|
||||||
|
{
|
||||||
|
instruction.Mnemonic = _segmentOverride;
|
||||||
|
instruction.Operands = "";
|
||||||
|
|
||||||
|
// Set the raw bytes
|
||||||
|
int length = _position - startPosition;
|
||||||
|
instruction.RawBytes = new byte[length];
|
||||||
|
Array.Copy(_codeBuffer, startPosition, instruction.RawBytes, 0, length);
|
||||||
|
|
||||||
|
return instruction;
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,10 +157,21 @@ public class InstructionDecoder
|
|||||||
instruction.Operands = "??";
|
instruction.Operands = "??";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add segment override prefix to the instruction if present
|
||||||
|
if (_segmentOverridePrefix && !string.IsNullOrEmpty(instruction.Operands))
|
||||||
|
{
|
||||||
|
// If the instruction has memory operands, add the segment override
|
||||||
|
if (instruction.Operands.Contains("["))
|
||||||
|
{
|
||||||
|
// Replace the first '[' with the segment override
|
||||||
|
instruction.Operands = instruction.Operands.Replace("[", $"{_segmentOverride}:[" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set the raw bytes
|
// Set the raw bytes
|
||||||
int length = _position - startPosition;
|
int instructionLength = _position - startPosition;
|
||||||
instruction.RawBytes = new byte[length];
|
instruction.RawBytes = new byte[instructionLength];
|
||||||
Array.Copy(_codeBuffer, startPosition, instruction.RawBytes, 0, length);
|
Array.Copy(_codeBuffer, startPosition, instruction.RawBytes, 0, instructionLength);
|
||||||
|
|
||||||
return instruction;
|
return instruction;
|
||||||
}
|
}
|
||||||
|
51
X86DisassemblerTests/SegmentOverrideTests.cs
Normal file
51
X86DisassemblerTests/SegmentOverrideTests.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
namespace X86DisassemblerTests;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Xunit;
|
||||||
|
using X86Disassembler.X86;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for segment override prefixes
|
||||||
|
/// </summary>
|
||||||
|
public class SegmentOverrideTests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tests that the FS segment override prefix (0x64) is correctly recognized
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void FsSegmentOverride_IsRecognized()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
// FS segment override prefix (0x64) followed by MOV [0], ESP (89 25 00 00 00 00)
|
||||||
|
byte[] codeBuffer = new byte[] { 0x64, 0x89, 0x25, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var instruction = decoder.DecodeInstruction();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(instruction);
|
||||||
|
Assert.Equal("mov", instruction.Mnemonic);
|
||||||
|
Assert.Equal("esp, dword ptr fs:[0x00000000]", instruction.Operands);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests that the FS segment override prefix (0x64) is correctly recognized when it's the only byte
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void FsSegmentOverride_Alone_IsRecognized()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
// Just the FS segment override prefix (0x64)
|
||||||
|
byte[] codeBuffer = new byte[] { 0x64 };
|
||||||
|
var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var instruction = decoder.DecodeInstruction();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(instruction);
|
||||||
|
Assert.Equal("fs", instruction.Mnemonic);
|
||||||
|
Assert.Equal("", instruction.Operands);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user