0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +03:00

Fix x86 disassembler issues with direct memory addressing and immediate value formatting

This commit is contained in:
bird_egop
2025-04-15 02:29:32 +03:00
parent d351f41808
commit 3ea327064a
67 changed files with 854 additions and 453 deletions

View File

@ -26,7 +26,7 @@ public class SegmentOverrideTests
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
@ -43,7 +43,6 @@ public class SegmentOverrideTests
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal(0, memoryOperand.Address);
Assert.Equal("cs", memoryOperand.SegmentOverride);
}
@ -65,7 +64,7 @@ public class SegmentOverrideTests
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
@ -82,7 +81,6 @@ public class SegmentOverrideTests
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal(0, memoryOperand.Address);
Assert.Equal("ds", memoryOperand.SegmentOverride);
}
@ -104,7 +102,7 @@ public class SegmentOverrideTests
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
@ -121,7 +119,6 @@ public class SegmentOverrideTests
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal(0, memoryOperand.Address);
Assert.Equal("es", memoryOperand.SegmentOverride);
}
@ -132,8 +129,8 @@ public class SegmentOverrideTests
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 };
// FS segment override prefix (0x64) followed by MOV ESP, [0] (8B 25 00 00 00 00)
byte[] codeBuffer = new byte[] { 0x64, 0x8B, 0x25, 0x00, 0x00, 0x00, 0x00 };
var disassembler = new Disassembler(codeBuffer, 0);
// Act
@ -143,25 +140,24 @@ public class SegmentOverrideTests
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
// Check the first operand (memory operand with FS segment override)
var memOperand = instruction.StructuredOperands[0];
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal(0, memoryOperand.Address);
Assert.Equal("fs", memoryOperand.SegmentOverride);
// Check the second operand (ESP)
var espOperand = instruction.StructuredOperands[1];
// Check the first operand (ESP)
var espOperand = instruction.StructuredOperands[0];
Assert.IsType<RegisterOperand>(espOperand);
var registerOperand = (RegisterOperand)espOperand;
Assert.Equal(RegisterIndex.Sp, registerOperand.Register);
Assert.Equal(32, registerOperand.Size); // Validate that it's a 32-bit register (ESP)
// Check the second operand (memory operand with FS segment override)
var memOperand = instruction.StructuredOperands[1];
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal("fs", memoryOperand.SegmentOverride);
}
/// <summary>
@ -178,11 +174,18 @@ public class SegmentOverrideTests
// Act
var instructions = disassembler.Disassemble();
// Debug output
Console.WriteLine($"Number of instructions: {instructions.Count}");
for (int i = 0; i < instructions.Count; i++)
{
Console.WriteLine($"Instruction {i}: Type={instructions[i].Type}, Address={instructions[i].Address:X}");
}
// Assert
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
@ -199,7 +202,6 @@ public class SegmentOverrideTests
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal(0, memoryOperand.Address);
Assert.Equal("gs", memoryOperand.SegmentOverride);
}
@ -221,7 +223,7 @@ public class SegmentOverrideTests
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
@ -282,7 +284,7 @@ public class SegmentOverrideTests
Assert.Single(instructions);
var instruction = instructions[0];
Assert.NotNull(instruction);
Assert.Equal(InstructionType.Move, instruction.Type);
Assert.Equal(InstructionType.Mov, instruction.Type);
// Check that we have two operands
Assert.Equal(2, instruction.StructuredOperands.Count);
@ -338,10 +340,10 @@ public class SegmentOverrideTests
// Check the second operand (memory operand with ES segment override)
var memOperand = instruction.StructuredOperands[1];
Assert.IsType<DirectMemoryOperand>(memOperand);
var memoryOperand = (DirectMemoryOperand)memOperand;
Assert.IsType<BaseRegisterMemoryOperand>(memOperand);
var memoryOperand = (BaseRegisterMemoryOperand)memOperand;
Assert.Equal(32, memoryOperand.Size); // Validate that it's a 32-bit memory reference
Assert.Equal(0, memoryOperand.Address);
Assert.Equal(RegisterIndex.Si, memoryOperand.BaseRegister);
Assert.Equal("es", memoryOperand.SegmentOverride);
}