mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 08:18:36 +03:00
move handlers
This commit is contained in:
77
X86Disassembler/X86/Handlers/Jump/ConditionalJumpHandler.cs
Normal file
77
X86Disassembler/X86/Handlers/Jump/ConditionalJumpHandler.cs
Normal file
@ -0,0 +1,77 @@
|
||||
namespace X86Disassembler.X86.Handlers.Jump;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for conditional jump instructions (0x70-0x7F)
|
||||
/// </summary>
|
||||
public class ConditionalJumpHandler : InstructionHandler
|
||||
{
|
||||
// Mnemonics for conditional jumps
|
||||
private static readonly string[] ConditionalJumpMnemonics = new string[]
|
||||
{
|
||||
"jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "jnbe",
|
||||
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the ConditionalJumpHandler 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 ConditionalJumpHandler(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)
|
||||
{
|
||||
// Conditional jumps are in the range 0x70-0x7F
|
||||
return opcode >= 0x70 && opcode <= 0x7F;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a conditional jump 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)
|
||||
{
|
||||
// Get the mnemonic from the table
|
||||
int index = opcode - 0x70;
|
||||
instruction.Mnemonic = ConditionalJumpMnemonics[index];
|
||||
|
||||
// Get the current position in the code buffer
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
sbyte offset = (sbyte)CodeBuffer[position];
|
||||
|
||||
// According to x86 architecture, the jump offset is relative to the instruction following the jump
|
||||
// For a conditional jump, the instruction is 2 bytes: opcode (1 byte) + offset (1 byte)
|
||||
|
||||
// Calculate the target address:
|
||||
// 1. Start with the current position (where the offset byte is)
|
||||
// 2. Add 1 to account for the size of the offset byte itself
|
||||
// 3. Add the offset value
|
||||
int targetAddress = position + 1 + offset;
|
||||
|
||||
// Move the decoder position past the offset byte
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Set the operands to the calculated target address
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
59
X86Disassembler/X86/Handlers/Jump/JmpRel32Handler.cs
Normal file
59
X86Disassembler/X86/Handlers/Jump/JmpRel32Handler.cs
Normal file
@ -0,0 +1,59 @@
|
||||
namespace X86Disassembler.X86.Handlers.Jump;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for JMP rel32 instruction (0xE9)
|
||||
/// </summary>
|
||||
public class JmpRel32Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the JmpRel32Handler 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 JmpRel32Handler(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 == 0xE9;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a JMP rel32 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 = "jmp";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
int offset = BitConverter.ToInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
59
X86Disassembler/X86/Handlers/Jump/JmpRel8Handler.cs
Normal file
59
X86Disassembler/X86/Handlers/Jump/JmpRel8Handler.cs
Normal file
@ -0,0 +1,59 @@
|
||||
namespace X86Disassembler.X86.Handlers.Jump;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for JMP rel8 instruction (0xEB)
|
||||
/// </summary>
|
||||
public class JmpRel8Handler : InstructionHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the JmpRel8Handler 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 JmpRel8Handler(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 == 0xEB;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a JMP rel8 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 = "jmp";
|
||||
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
sbyte offset = (sbyte)CodeBuffer[position];
|
||||
Decoder.SetPosition(position + 1);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 1);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
namespace X86Disassembler.X86.Handlers.Jump;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for two-byte conditional jump instructions (0x0F 0x80-0x8F)
|
||||
/// </summary>
|
||||
public class TwoByteConditionalJumpHandler : InstructionHandler
|
||||
{
|
||||
// Mnemonics for conditional jumps
|
||||
private static readonly string[] ConditionalJumpMnemonics = new string[]
|
||||
{
|
||||
"jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "jnbe",
|
||||
"js", "jns", "jp", "jnp", "jl", "jnl", "jle", "jnle"
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the TwoByteConditionalJumpHandler 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 TwoByteConditionalJumpHandler(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)
|
||||
{
|
||||
// Two-byte conditional jumps start with 0x0F
|
||||
if (opcode == 0x0F)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
if (position < Length)
|
||||
{
|
||||
byte secondByte = CodeBuffer[position];
|
||||
// Second byte must be in the range 0x80-0x8F
|
||||
return secondByte >= 0x80 && secondByte <= 0x8F;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a two-byte conditional jump 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)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the second byte of the opcode
|
||||
byte secondByte = CodeBuffer[position++];
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
// Get the mnemonic from the table
|
||||
int index = secondByte - 0x80;
|
||||
instruction.Mnemonic = ConditionalJumpMnemonics[index];
|
||||
|
||||
if (position + 4 > Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the relative offset (32-bit)
|
||||
int offset = BitConverter.ToInt32(CodeBuffer, position);
|
||||
Decoder.SetPosition(position + 4);
|
||||
|
||||
// Calculate the target address
|
||||
uint targetAddress = (uint)(position + offset + 4);
|
||||
|
||||
// Set the operands
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user