0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-20 08:18:36 +03:00

nice big refactor

This commit is contained in:
bird_egop
2025-04-13 23:06:52 +03:00
parent 59df064ca4
commit 11a2cfada4
92 changed files with 981 additions and 1509 deletions

View File

@ -42,8 +42,7 @@ public class SubAlImm8Handler : InstructionHandler
}
// Read the immediate byte
byte imm8 = CodeBuffer[position++];
Decoder.SetPosition(position);
byte imm8 = Decoder.ReadByte();
// Set the instruction information
instruction.Mnemonic = "sub";

View File

@ -43,10 +43,10 @@ public class SubImmFromRm16Handler : InstructionHandler
}
// Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Check if this is a SUB instruction (reg field must be 5)
if (reg != 5)
if (reg != RegisterIndex.Di)
{
return false;
}
@ -54,9 +54,6 @@ public class SubImmFromRm16Handler : InstructionHandler
// 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)
@ -66,11 +63,8 @@ public class SubImmFromRm16Handler : InstructionHandler
}
else
{
// Get the memory operand string
destination = ModRMDecoder.DecodeModRM(mod, rm, false);
// Replace "dword" with "word" in the memory operand
destination = destination.Replace("dword", "word");
destination = destOperand.Replace("dword", "word");
}
// Get the current position after processing the ModR/M byte

View File

@ -43,10 +43,10 @@ public class SubImmFromRm16SignExtendedHandler : InstructionHandler
}
// Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Check if this is a SUB instruction (reg field must be 5)
if (reg != 5)
if (reg != RegisterIndex.Di)
{
return false;
}
@ -63,11 +63,8 @@ public class SubImmFromRm16SignExtendedHandler : InstructionHandler
}
else
{
// Get the memory operand string
destination = ModRMDecoder.DecodeModRM(mod, rm, false);
// Replace "dword" with "word" in the memory operand
destination = destination.Replace("dword", "word");
destination = destOperand.Replace("dword", "word");
}
// Get the current position after processing the ModR/M byte

View File

@ -77,10 +77,6 @@ public class SubImmFromRm32Handler : InstructionHandler
// Format the immediate value
string immStr = $"0x{imm:X8}";
// Advance the position past the immediate value
position += 4;
Decoder.SetPosition(position);
// Set the operands
instruction.Operands = $"{destOperand}, {immStr}";

View File

@ -70,9 +70,8 @@ public class SubImmFromRm32SignExtendedHandler : InstructionHandler
return false;
}
// Read the immediate value as a signed byte and sign-extend it to 32 bits
sbyte imm8 = (sbyte) Decoder.ReadByte();
int imm32 = imm8; // Automatic sign extension from sbyte to int
// Read the immediate value as a signed byte and sign-extend it to 32 bits with sign extension from sbyte to int
int imm32 = (sbyte) Decoder.ReadByte();
// Format the immediate value based on the operand type and value
string immStr;
@ -81,12 +80,12 @@ public class SubImmFromRm32SignExtendedHandler : InstructionHandler
if (mod != 3) // Memory operand
{
// For memory operands, use the actual value as specified in the test
immStr = $"0x{(byte) imm8:X2}";
immStr = $"0x{(byte) imm32:X2}";
}
else // Register operand
{
// For register operands, format based on whether it's negative or not
if (imm8 < 0)
if (imm32 < 0)
{
// For negative values, show the full 32-bit representation with 8-digit padding
immStr = $"0x{(uint) imm32:X8}";
@ -94,7 +93,7 @@ public class SubImmFromRm32SignExtendedHandler : InstructionHandler
else
{
// For positive values, just show the value with 2-digit padding for consistency
immStr = $"0x{(byte) imm8:X2}";
immStr = $"0x{(byte) imm32:X2}";
}
}

View File

@ -49,7 +49,7 @@ public class SubImmFromRm8Handler : InstructionHandler
instruction.Mnemonic = "sub";
// Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Read the immediate byte
var position = Decoder.GetPosition();
@ -58,8 +58,7 @@ public class SubImmFromRm8Handler : InstructionHandler
return false;
}
byte imm8 = CodeBuffer[position++];
Decoder.SetPosition(position);
byte imm8 = Decoder.ReadByte();
// Set the instruction information
// For mod == 3, the operand is a register
@ -71,8 +70,7 @@ public class SubImmFromRm8Handler : InstructionHandler
else // Memory operand
{
// Get the memory operand string
string memOperand = ModRMDecoder.DecodeModRM(mod, rm, false);
instruction.Operands = $"byte ptr {memOperand}, 0x{imm8:X2}";
instruction.Operands = $"byte ptr {destOperand}, 0x{imm8:X2}";
}
return true;

View File

@ -46,7 +46,7 @@ public class SubR16Rm16Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name (16-bit)
string regName = ModRMDecoder.GetRegisterName(reg, 16);
@ -60,9 +60,9 @@ public class SubR16Rm16Handler : InstructionHandler
else // Memory operand
{
// Replace "dword" with "word" in the memory operand
memOperand = memOperand.Replace("dword", "word");
destOperand = destOperand.Replace("dword", "word");
instruction.Operands = $"{regName}, {memOperand}";
instruction.Operands = $"{regName}, {destOperand}";
}
return true;

View File

@ -42,22 +42,22 @@ public class SubR32Rm32Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Set the mnemonic
instruction.Mnemonic = "sub";
// Get the register name
string regName = GetRegister32(reg);
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand
if (mod != 3) // Memory operand
{
instruction.Operands = $"{regName}, {operand}";
instruction.Operands = $"{regName}, {destOperand}";
}
else // Register operand
{
string rmName = GetRegister32(rm);
string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{regName}, {rmName}";
}

View File

@ -45,7 +45,7 @@ public class SubR8Rm8Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
@ -58,7 +58,7 @@ public class SubR8Rm8Handler : InstructionHandler
}
else // Memory operand
{
instruction.Operands = $"{regName}, byte ptr {memOperand}";
instruction.Operands = $"{regName}, byte ptr {destOperand}";
}
return true;

View File

@ -46,7 +46,7 @@ public class SubRm16R16Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name (16-bit)
string regName = ModRMDecoder.GetRegisterName(reg, 16);
@ -60,9 +60,9 @@ public class SubRm16R16Handler : InstructionHandler
else // Memory operand
{
// Replace "dword" with "word" in the memory operand
memOperand = memOperand.Replace("dword", "word");
destOperand = destOperand.Replace("dword", "word");
instruction.Operands = $"{memOperand}, {regName}";
instruction.Operands = $"{destOperand}, {regName}";
}
return true;

View File

@ -42,7 +42,7 @@ public class SubRm32R32Handler : InstructionHandler
}
// Read the ModR/M byte
// Extract the fields from the ModR/M byte
var (mod, reg, rm, operand) = ModRMDecoder.ReadModRM();
@ -50,7 +50,7 @@ public class SubRm32R32Handler : InstructionHandler
instruction.Mnemonic = "sub";
// Get the register name
string regName = GetRegister32(reg);
string regName = ModRMDecoder.GetRegisterName(reg, 32);
// For memory operands, set the operand
if (mod != 3) // Memory operand
@ -59,10 +59,10 @@ public class SubRm32R32Handler : InstructionHandler
}
else // Register operand
{
string rmName = GetRegister32(rm);
string rmName = ModRMDecoder.GetRegisterName(rm, 32);
instruction.Operands = $"{rmName}, {regName}";
}
return true;
}
}
}

View File

@ -45,7 +45,7 @@ public class SubRm8R8Handler : InstructionHandler
}
// Read the ModR/M byte
var (mod, reg, rm, memOperand) = ModRMDecoder.ReadModRM();
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// Get register name
string regName = ModRMDecoder.GetRegisterName(reg, 8);
@ -58,7 +58,7 @@ public class SubRm8R8Handler : InstructionHandler
}
else // Memory operand
{
instruction.Operands = $"byte ptr {memOperand}, {regName}";
instruction.Operands = $"byte ptr {destOperand}, {regName}";
}
return true;