mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-21 12:51:18 +03:00
Simplified jump instruction handlers by using consistent decoder methods and improving code organization
This commit is contained in:
parent
5daab494e1
commit
38770de005
@ -46,30 +46,18 @@ public class ConditionalJumpHandler : InstructionHandler
|
|||||||
int index = opcode - 0x70;
|
int index = opcode - 0x70;
|
||||||
instruction.Mnemonic = Mnemonics[index];
|
instruction.Mnemonic = Mnemonics[index];
|
||||||
|
|
||||||
// Get the current position in the code buffer
|
// Check if we can read the offset byte
|
||||||
int position = Decoder.GetPosition();
|
if (!Decoder.CanReadByte())
|
||||||
|
|
||||||
if (position >= Length)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the relative offset
|
// Read the offset and calculate target address
|
||||||
sbyte offset = (sbyte)CodeBuffer[position];
|
int position = Decoder.GetPosition();
|
||||||
|
sbyte offset = (sbyte)Decoder.ReadByte();
|
||||||
// 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;
|
int targetAddress = position + 1 + offset;
|
||||||
|
|
||||||
// Move the decoder position past the offset byte
|
// Format the target address as a hexadecimal value
|
||||||
Decoder.SetPosition(position + 1);
|
|
||||||
|
|
||||||
// Set the operands to the calculated target address
|
|
||||||
instruction.Operands = $"0x{targetAddress:X8}";
|
instruction.Operands = $"0x{targetAddress:X8}";
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -34,34 +34,26 @@ public class JgeRel8Handler : 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 = "jge";
|
instruction.Mnemonic = "jge";
|
||||||
|
|
||||||
if (startPosition >= Length)
|
// Check if we can read the offset byte
|
||||||
|
if (!Decoder.CanReadByte())
|
||||||
{
|
{
|
||||||
instruction.Operands = "??";
|
instruction.Operands = "??";
|
||||||
instruction.RawBytes = new byte[] { opcode };
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the relative offset
|
// Read the offset and calculate target address
|
||||||
sbyte offset = (sbyte)CodeBuffer[startPosition];
|
int position = Decoder.GetPosition();
|
||||||
Decoder.SetPosition(startPosition + 1);
|
sbyte offset = (sbyte)Decoder.ReadByte();
|
||||||
|
|
||||||
// Calculate the target address
|
// Calculate target address (instruction address + instruction length + offset)
|
||||||
// The target is calculated from the address of the next instruction (EIP + 2)
|
uint targetAddress = (uint)(instruction.Address + 2 + offset);
|
||||||
// EIP + 2 + offset
|
|
||||||
uint targetAddress = (uint)(instruction.Address + offset + 2);
|
|
||||||
|
|
||||||
// Set the operands
|
// Format the target address
|
||||||
instruction.Operands = $"0x{targetAddress:X8}";
|
instruction.Operands = $"0x{targetAddress:X8}";
|
||||||
|
|
||||||
// Set the raw bytes
|
|
||||||
instruction.RawBytes = new byte[] { opcode, (byte)offset };
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ public class JmpRel8Handler : InstructionHandler
|
|||||||
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
/// <param name="codeBuffer">The buffer containing the code to decode</param>
|
||||||
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
/// <param name="decoder">The instruction decoder that owns this handler</param>
|
||||||
/// <param name="length">The length of the buffer</param>
|
/// <param name="length">The length of the buffer</param>
|
||||||
public JmpRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
public JmpRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||||
: base(codeBuffer, decoder, length)
|
: base(codeBuffer, decoder, length)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -34,37 +34,25 @@ public class JmpRel8Handler : 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 = "jmp";
|
instruction.Mnemonic = "jmp";
|
||||||
|
|
||||||
// Check if we have enough bytes for the offset
|
// Check if we can read the offset byte
|
||||||
if (startPosition >= Length)
|
if (!Decoder.CanReadByte())
|
||||||
{
|
{
|
||||||
// Not enough bytes for the offset
|
|
||||||
instruction.Operands = "??";
|
|
||||||
instruction.RawBytes = new byte[] { opcode };
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the relative offset
|
// Read the offset and calculate target address
|
||||||
sbyte offset = (sbyte)CodeBuffer[startPosition];
|
int position = Decoder.GetPosition();
|
||||||
|
sbyte offset = (sbyte)Decoder.ReadByte();
|
||||||
|
|
||||||
// Advance the decoder position past the offset byte
|
// Calculate target address (instruction address + instruction length + offset)
|
||||||
Decoder.SetPosition(startPosition + 1);
|
uint targetAddress = (uint)(instruction.Address + 2 + offset);
|
||||||
|
|
||||||
// Calculate the target address
|
// Format the target address
|
||||||
// The target is relative to the next instruction (after the JMP instruction)
|
|
||||||
uint targetAddress = (uint)(instruction.Address + offset + 2);
|
|
||||||
|
|
||||||
// Set the operands
|
|
||||||
instruction.Operands = $"0x{targetAddress:X8}";
|
instruction.Operands = $"0x{targetAddress:X8}";
|
||||||
|
|
||||||
// Set the raw bytes
|
|
||||||
instruction.RawBytes = new byte[] { opcode, (byte)offset };
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user