mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-05-21 04:41: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;
|
||||
instruction.Mnemonic = Mnemonics[index];
|
||||
|
||||
// Get the current position in the code buffer
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
if (position >= Length)
|
||||
// Check if we can read the offset byte
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
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
|
||||
// Read the offset and calculate target address
|
||||
int position = Decoder.GetPosition();
|
||||
sbyte offset = (sbyte)Decoder.ReadByte();
|
||||
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
|
||||
// Format the target address as a hexadecimal value
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
return true;
|
||||
|
@ -34,34 +34,26 @@ public class JgeRel8Handler : InstructionHandler
|
||||
/// <returns>True if the instruction was successfully decoded</returns>
|
||||
public override bool Decode(byte opcode, Instruction instruction)
|
||||
{
|
||||
// Save the original position for raw bytes calculation
|
||||
int startPosition = Decoder.GetPosition();
|
||||
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "jge";
|
||||
|
||||
if (startPosition >= Length)
|
||||
// Check if we can read the offset byte
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
instruction.Operands = "??";
|
||||
instruction.RawBytes = new byte[] { opcode };
|
||||
return true;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
sbyte offset = (sbyte)CodeBuffer[startPosition];
|
||||
Decoder.SetPosition(startPosition + 1);
|
||||
// Read the offset and calculate target address
|
||||
int position = Decoder.GetPosition();
|
||||
sbyte offset = (sbyte)Decoder.ReadByte();
|
||||
|
||||
// Calculate the target address
|
||||
// The target is calculated from the address of the next instruction (EIP + 2)
|
||||
// EIP + 2 + offset
|
||||
uint targetAddress = (uint)(instruction.Address + offset + 2);
|
||||
// Calculate target address (instruction address + instruction length + offset)
|
||||
uint targetAddress = (uint)(instruction.Address + 2 + offset);
|
||||
|
||||
// Set the operands
|
||||
// Format the target address
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
// Set the raw bytes
|
||||
instruction.RawBytes = new byte[] { opcode, (byte)offset };
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class JmpRel8Handler : InstructionHandler
|
||||
/// <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)
|
||||
public JmpRel8Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
|
||||
: base(codeBuffer, decoder, length)
|
||||
{
|
||||
}
|
||||
@ -34,37 +34,25 @@ public class JmpRel8Handler : InstructionHandler
|
||||
/// <returns>True if the instruction was successfully decoded</returns>
|
||||
public override bool Decode(byte opcode, Instruction instruction)
|
||||
{
|
||||
// Save the original position for raw bytes calculation
|
||||
int startPosition = Decoder.GetPosition();
|
||||
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "jmp";
|
||||
|
||||
// Check if we have enough bytes for the offset
|
||||
if (startPosition >= Length)
|
||||
// Check if we can read the offset byte
|
||||
if (!Decoder.CanReadByte())
|
||||
{
|
||||
// Not enough bytes for the offset
|
||||
instruction.Operands = "??";
|
||||
instruction.RawBytes = new byte[] { opcode };
|
||||
return true;
|
||||
}
|
||||
|
||||
// Read the relative offset
|
||||
sbyte offset = (sbyte)CodeBuffer[startPosition];
|
||||
// Read the offset and calculate target address
|
||||
int position = Decoder.GetPosition();
|
||||
sbyte offset = (sbyte)Decoder.ReadByte();
|
||||
|
||||
// Advance the decoder position past the offset byte
|
||||
Decoder.SetPosition(startPosition + 1);
|
||||
// Calculate target address (instruction address + instruction length + offset)
|
||||
uint targetAddress = (uint)(instruction.Address + 2 + offset);
|
||||
|
||||
// Calculate 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
|
||||
// Format the target address
|
||||
instruction.Operands = $"0x{targetAddress:X8}";
|
||||
|
||||
// Set the raw bytes
|
||||
instruction.RawBytes = new byte[] { opcode, (byte)offset };
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user