mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2025-06-20 08:18:36 +03:00
Refactor SUB handlers
This commit is contained in:
@ -36,32 +36,27 @@ public class SubImmFromRm16Handler : InstructionHandler
|
||||
public override bool Decode(byte opcode, Instruction instruction)
|
||||
{
|
||||
int position = Decoder.GetPosition();
|
||||
|
||||
|
||||
if (position >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read the ModR/M byte
|
||||
byte modRM = CodeBuffer[position++];
|
||||
|
||||
|
||||
// Extract the fields from the ModR/M byte
|
||||
byte mod = (byte)((modRM & 0xC0) >> 6);
|
||||
byte reg = (byte)((modRM & 0x38) >> 3);
|
||||
byte rm = (byte)(modRM & 0x07);
|
||||
|
||||
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM();
|
||||
|
||||
// Check if this is a SUB instruction (reg field must be 5)
|
||||
if (reg != 5)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Set the mnemonic
|
||||
instruction.Mnemonic = "sub";
|
||||
|
||||
|
||||
// Update the decoder position
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
|
||||
// For mod == 3, the r/m field specifies a register
|
||||
string destination;
|
||||
if (mod == 3)
|
||||
@ -73,43 +68,37 @@ public class SubImmFromRm16Handler : InstructionHandler
|
||||
{
|
||||
// Get the memory operand string
|
||||
destination = ModRMDecoder.DecodeModRM(mod, rm, false);
|
||||
|
||||
|
||||
// Replace "dword" with "word" in the memory operand
|
||||
destination = destination.Replace("dword", "word");
|
||||
}
|
||||
|
||||
|
||||
// Get the current position after processing the ModR/M byte
|
||||
position = Decoder.GetPosition();
|
||||
|
||||
|
||||
// Check if we have enough bytes for the immediate value
|
||||
if (position + 1 >= Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read the immediate value (16-bit)
|
||||
byte lowByte = CodeBuffer[position++];
|
||||
byte highByte = CodeBuffer[position++];
|
||||
|
||||
// Combine the bytes into a 16-bit value
|
||||
ushort immediate = (ushort)((highByte << 8) | lowByte);
|
||||
|
||||
// Update the decoder position
|
||||
Decoder.SetPosition(position);
|
||||
|
||||
ushort immediate = Decoder.ReadUInt16();
|
||||
|
||||
// Set the operands (note: we use 32-bit register names to match the disassembler's output)
|
||||
if (mod == 3)
|
||||
{
|
||||
// For register operands, use the 32-bit register name
|
||||
string reg32Name = destination.Replace("ax", "eax")
|
||||
.Replace("bx", "ebx")
|
||||
.Replace("cx", "ecx")
|
||||
.Replace("dx", "edx")
|
||||
.Replace("sp", "esp")
|
||||
.Replace("bp", "ebp")
|
||||
.Replace("si", "esi")
|
||||
.Replace("di", "edi");
|
||||
|
||||
string reg32Name = destination
|
||||
.Replace("ax", "eax")
|
||||
.Replace("bx", "ebx")
|
||||
.Replace("cx", "ecx")
|
||||
.Replace("dx", "edx")
|
||||
.Replace("sp", "esp")
|
||||
.Replace("bp", "ebp")
|
||||
.Replace("si", "esi")
|
||||
.Replace("di", "edi");
|
||||
|
||||
instruction.Operands = $"{reg32Name}, 0x{immediate:X4}";
|
||||
}
|
||||
else
|
||||
@ -117,7 +106,7 @@ public class SubImmFromRm16Handler : InstructionHandler
|
||||
// For memory operands, keep the memory operand as is
|
||||
instruction.Operands = $"{destination}, 0x{immediate:X4}";
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user