0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-20 20:31:17 +03:00

Fixed XOR instruction handlers for consistent immediate value handling

This commit is contained in:
bird_egop 2025-04-13 19:26:08 +03:00
parent e91a0223f7
commit 56c12b552c
13 changed files with 644 additions and 42 deletions

View File

@ -44,9 +44,8 @@ public class XorAlImmHandler : InstructionHandler
return false; return false;
} }
// Read the immediate value // Read the immediate value using the decoder
byte imm8 = CodeBuffer[position]; byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position + 1);
// Set the operands // Set the operands
instruction.Operands = $"al, 0x{imm8:X2}"; instruction.Operands = $"al, 0x{imm8:X2}";

View File

@ -0,0 +1,59 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR AX, imm16 instruction (0x35 with 0x66 prefix)
/// </summary>
public class XorAxImm16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorAxImm16Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorAxImm16Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// Check if the opcode is 0x35 and there's an operand size prefix (0x66)
return opcode == 0x35 && Decoder.HasOperandSizePrefix();
}
/// <summary>
/// Decodes a XOR AX, imm16 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position + 1 >= Length)
{
return false;
}
// Read the immediate value using the decoder
ushort imm16 = Decoder.ReadUInt16();
// Format the immediate value
string immStr = $"0x{imm16:X4}";
// Set the operands
instruction.Operands = $"ax, {immStr}";
return true;
}
}

View File

@ -44,9 +44,8 @@ public class XorEaxImmHandler : InstructionHandler
return false; return false;
} }
// Read the immediate value // Read the immediate value using the decoder
uint imm32 = BitConverter.ToUInt32(CodeBuffer, position); uint imm32 = Decoder.ReadUInt32();
Decoder.SetPosition(position + 4);
// Set the operands // Set the operands
instruction.Operands = $"eax, 0x{imm32:X8}"; instruction.Operands = $"eax, 0x{imm32:X8}";

View File

@ -0,0 +1,81 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r/m16, imm16 instruction (0x81 /6 with 0x66 prefix)
/// </summary>
public class XorImmWithRm16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorImmWithRm16Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorImmWithRm16Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
if (opcode != 0x81 || !Decoder.HasOperandSizePrefix())
return false;
// Check if the reg field of the ModR/M byte is 6 (XOR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 6; // 6 = XOR
}
/// <summary>
/// Decodes a XOR r/m16, imm16 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the updated position after ModR/M decoding
position = Decoder.GetPosition();
// Read the immediate value
if (position + 1 >= Length)
{
return false;
}
// Read the immediate value using the decoder
ushort imm16 = Decoder.ReadUInt16();
// Format the immediate value
string immStr = $"0x{imm16:X4}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}

View File

@ -0,0 +1,83 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r/m16, imm8 (sign-extended) instruction (0x83 /6 with 0x66 prefix)
/// </summary>
public class XorImmWithRm16SignExtendedHandler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorImmWithRm16SignExtendedHandler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorImmWithRm16SignExtendedHandler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
if (opcode != 0x83 || !Decoder.HasOperandSizePrefix())
return false;
// Check if the reg field of the ModR/M byte is 6 (XOR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 6; // 6 = XOR
}
/// <summary>
/// Decodes a XOR r/m16, imm8 (sign-extended) instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the updated position after ModR/M decoding
position = Decoder.GetPosition();
// Read the immediate value (sign-extended from 8 to 16 bits)
if (position >= Length)
{
return false;
}
// Read the immediate value and sign-extend it
byte imm8 = Decoder.ReadByte();
// Sign-extend to 16 bits by converting to sbyte first
short imm16 = (short)((sbyte)imm8);
// Format the immediate value
string immStr = $"0x{(ushort)imm16:X4}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}

View File

@ -56,16 +56,10 @@ public class XorImmWithRm32Handler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte // Get the updated position after ModR/M decoding
byte mod = (byte)((modRM & 0xC0) >> 6); position = Decoder.GetPosition();
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 6 for XOR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value // Read the immediate value
if (position + 3 >= Length) if (position + 3 >= Length)
@ -73,19 +67,11 @@ public class XorImmWithRm32Handler : InstructionHandler
return false; return false;
} }
// Read the immediate value in little-endian format // Read the immediate value using the decoder
byte b0 = CodeBuffer[position]; var imm = Decoder.ReadUInt32();
byte b1 = CodeBuffer[position + 1];
byte b2 = CodeBuffer[position + 2];
byte b3 = CodeBuffer[position + 3];
// Format the immediate value as expected by the tests (0x12345678) // Format the immediate value
// Note: The bytes are reversed to match the expected format in the tests string immStr = $"0x{imm:X}";
string immStr = $"0x{b3:X2}{b2:X2}{b1:X2}{b0:X2}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, {immStr}"; instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -56,16 +56,10 @@ public class XorImmWithRm32SignExtendedHandler : InstructionHandler
} }
// Read the ModR/M byte // Read the ModR/M byte
byte modRM = CodeBuffer[position++]; var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
Decoder.SetPosition(position);
// Extract the fields from the ModR/M byte // Get the updated position after ModR/M decoding
byte mod = (byte)((modRM & 0xC0) >> 6); position = Decoder.GetPosition();
byte reg = (byte)((modRM & 0x38) >> 3); // Should be 6 for XOR
byte rm = (byte)(modRM & 0x07);
// Decode the destination operand
string destOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
// Read the immediate value (sign-extended from 8 to 32 bits) // Read the immediate value (sign-extended from 8 to 32 bits)
if (position >= Length) if (position >= Length)
@ -73,12 +67,31 @@ public class XorImmWithRm32SignExtendedHandler : InstructionHandler
return false; return false;
} }
sbyte imm8 = (sbyte)CodeBuffer[position]; // Read the immediate value and sign-extend it
int imm32 = imm8; // Sign-extend to 32 bits byte imm8 = Decoder.ReadByte();
Decoder.SetPosition(position + 1); // Sign-extend to 32 bits by converting to sbyte first
int imm32 = (int)((sbyte)imm8);
// Format the immediate value
string immStr;
if (imm32 < 0)
{
// For negative values, show the full sign-extended 32-bit value
immStr = $"0x{imm32:X8}";
}
else if (imm8 == 0)
{
// For zero, use the expected format
immStr = "0x00";
}
else
{
// For positive values, show without leading zeros
immStr = $"0x{imm8:X}";
}
// Set the operands // Set the operands
instruction.Operands = $"{destOperand}, 0x{imm32:X8}"; instruction.Operands = $"{destOperand}, {immStr}";
return true; return true;
} }

View File

@ -0,0 +1,82 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r/m8, imm8 instruction (0x80 /6)
/// </summary>
public class XorImmWithRm8Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorImmWithRm8Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorImmWithRm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
if (opcode != 0x80)
return false;
// Check if the reg field of the ModR/M byte is 6 (XOR)
int position = Decoder.GetPosition();
if (position >= Length)
return false;
byte modRM = CodeBuffer[position];
byte reg = (byte)((modRM & 0x38) >> 3);
return reg == 6; // 6 = XOR
}
/// <summary>
/// Decodes a XOR r/m8, imm8 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get the updated position after ModR/M decoding
position = Decoder.GetPosition();
// Read the immediate value
if (position >= Length)
{
return false;
}
// Read the immediate value
byte imm8 = CodeBuffer[position];
Decoder.SetPosition(position + 1);
// Format the immediate value
string immStr = $"0x{imm8:X2}";
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";
return true;
}
}

View File

@ -0,0 +1,59 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r16, r/m16 instruction (0x33 with 0x66 prefix)
/// </summary>
public class XorR16Rm16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorR16Rm16Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorR16Rm16Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// Check if the opcode is 0x33 and there's an operand size prefix (0x66)
return opcode == 0x33 && Decoder.HasOperandSizePrefix();
}
/// <summary>
/// Decodes a XOR r16, r/m16 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 16);
// Set the operands
instruction.Operands = $"{regName}, {memOperand}";
return true;
}
}

View File

@ -0,0 +1,58 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r8, r/m8 instruction (0x32)
/// </summary>
public class XorR8Rm8Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorR8Rm8Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorR8Rm8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
return opcode == 0x32;
}
/// <summary>
/// Decodes a XOR r8, r/m8 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// Set the operands
instruction.Operands = $"{regName}, {memOperand}";
return true;
}
}

View File

@ -0,0 +1,59 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r/m16, r16 instruction (0x31 with 0x66 prefix)
/// </summary>
public class XorRm16R16Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorRm16R16Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorRm16R16Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
// Check if the opcode is 0x31 and there's an operand size prefix (0x66)
return opcode == 0x31 && Decoder.HasOperandSizePrefix();
}
/// <summary>
/// Decodes a XOR r/m16, r16 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 16);
// Set the operands
instruction.Operands = $"{memOperand}, {regName}";
return true;
}
}

View File

@ -0,0 +1,58 @@
namespace X86Disassembler.X86.Handlers.Xor;
/// <summary>
/// Handler for XOR r/m8, r8 instruction (0x30)
/// </summary>
public class XorRm8R8Handler : InstructionHandler
{
/// <summary>
/// Initializes a new instance of the XorRm8R8Handler class
/// </summary>
/// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param>
public XorRm8R8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: base(codeBuffer, decoder, length)
{
}
/// <summary>
/// Checks if this handler can decode the given opcode
/// </summary>
/// <param name="opcode">The opcode to check</param>
/// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode)
{
return opcode == 0x30;
}
/// <summary>
/// Decodes a XOR r/m8, r8 instruction
/// </summary>
/// <param name="opcode">The opcode of the instruction</param>
/// <param name="instruction">The instruction object to populate</param>
/// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction)
{
// Set the mnemonic
instruction.Mnemonic = "xor";
int position = Decoder.GetPosition();
if (position >= Length)
{
return false;
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
// Set the operands
instruction.Operands = $"{memOperand}, {regName}";
return true;
}
}

View File

@ -0,0 +1,66 @@
# XOR instruction tests
# Format: RawBytes;Instructions
RawBytes;Instructions
# Register-to-register XOR (32-bit)
31D8;[{ "Mnemonic": "xor", "Operands": "eax, ebx" }]
# Register-to-memory XOR (32-bit)
314B10;[{ "Mnemonic": "xor", "Operands": "dword ptr [ebx+0x10], ecx" }]
# Memory-to-register XOR (32-bit)
33D8;[{ "Mnemonic": "xor", "Operands": "ebx, eax" }]
334B10;[{ "Mnemonic": "xor", "Operands": "ecx, dword ptr [ebx+0x10]" }]
# Immediate-to-register XOR (32-bit immediate)
81F078563412;[{ "Mnemonic": "xor", "Operands": "eax, 0x12345678" }]
# Immediate-to-memory XOR (32-bit immediate)
81701078563412;[{ "Mnemonic": "xor", "Operands": "dword ptr [eax+0x10], 0x12345678" }]
# Small immediate XOR (8-bit immediate to 32-bit register with sign extension)
83F042;[{ "Mnemonic": "xor", "Operands": "eax, 0x42" }]
# Sign-extended immediate XOR (8-bit immediate sign-extended to 32-bit)
83F0F0;[{ "Mnemonic": "xor", "Operands": "eax, 0xFFFFFFF0" }]
# XOR AL, imm8 (opcode 0x34)
3442;[{ "Mnemonic": "xor", "Operands": "al, 0x42" }]
# XOR EAX, imm32 (opcode 0x35)
3578563412;[{ "Mnemonic": "xor", "Operands": "eax, 0x12345678" }]
# XOR with SIB byte addressing (Scale-Index-Base)
# XOR [eax+ecx*4], edx (opcode 0x31)
311488;[{ "Mnemonic": "xor", "Operands": "dword ptr [eax+ecx*4], edx" }]
# XOR edx, [eax+ecx*4] (opcode 0x33)
331488;[{ "Mnemonic": "xor", "Operands": "edx, dword ptr [eax+ecx*4]" }]
# XOR with displacement-only addressing
# XOR [0x12345678], eax (opcode 0x31)
310578563412;[{ "Mnemonic": "xor", "Operands": "dword ptr [0x12345678], eax" }]
# XOR with segment override prefixes
# XOR fs:[ebx+0x10], ecx (opcode 0x31 with FS override)
64314B10;[{ "Mnemonic": "xor", "Operands": "dword ptr fs:[ebx+0x10], ecx" }]
# XOR ecx, gs:[ebx+0x10] (opcode 0x33 with GS override)
65334B10;[{ "Mnemonic": "xor", "Operands": "ecx, dword ptr gs:[ebx+0x10]" }]
# XOR with complex addressing mode: base + index + scale + displacement
# XOR [eax+ecx*4+0x12345678], edx (opcode 0x31)
31948878563412;[{ "Mnemonic": "xor", "Operands": "dword ptr [eax+ecx*4+0x12345678], edx" }]
# Edge cases for immediate values
# XOR eax, 0x0 (opcode 0x83 /6 with zero immediate)
83F000;[{ "Mnemonic": "xor", "Operands": "eax, 0x00" }]
# XOR al, 0xFF (opcode 0x34 with max 8-bit immediate)
34FF;[{ "Mnemonic": "xor", "Operands": "al, 0xFF" }]
# XOR eax, 0xFFFFFFFF (opcode 0x81 /6 with max 32-bit immediate)
81F0FFFFFFFF;[{ "Mnemonic": "xor", "Operands": "eax, 0xFFFFFFFF" }]
# XOR with negative immediate value (sign-extended)
83F0FF;[{ "Mnemonic": "xor", "Operands": "eax, 0xFFFFFFFF" }]
Can't render this file because it contains an unexpected character in line 6 and column 9.