0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 03:41:18 +03:00

refactor modrm decoder more

This commit is contained in:
bird_egop 2025-04-16 19:14:11 +03:00
parent a91d6af8fc
commit 193f9cd2d8
6 changed files with 16 additions and 68 deletions

View File

@ -23,8 +23,6 @@ public class ModRMDecoder
_sibDecoder = new SIBDecoder(decoder);
}
// These methods have been moved to the RegisterMapper class
/// <summary>
/// Decodes a ModR/M byte to get the operand
/// </summary>
@ -32,10 +30,7 @@ public class ModRMDecoder
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
/// <param name="is64Bit">True if the operand is 64-bit</param>
/// <returns>The operand object</returns>
public Operand DecodeModRM(byte mod, RegisterIndex rmIndex, bool is64Bit)
{
return DecodeModRMInternal(mod, rmIndex, is64Bit ? 64 : 32);
}
public Operand DecodeModRM(byte mod, RegisterIndex rmIndex, bool is64Bit) => DecodeModRMInternal(mod, rmIndex, is64Bit ? 64 : 32);
/// <summary>
/// Decodes a ModR/M byte to get an 8-bit operand
@ -43,10 +38,7 @@ public class ModRMDecoder
/// <param name="mod">The mod field (2 bits)</param>
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
/// <returns>The 8-bit operand object</returns>
public Operand DecodeModRM8(byte mod, RegisterIndex rmIndex)
{
return DecodeModRMInternal(mod, rmIndex, 8);
}
public Operand DecodeModRM8(byte mod, RegisterIndex rmIndex) => DecodeModRMInternal(mod, rmIndex, 8);
/// <summary>
/// Internal implementation for decoding a ModR/M byte to get an operand with specific size
@ -57,7 +49,6 @@ public class ModRMDecoder
/// <returns>The operand object</returns>
private Operand DecodeModRMInternal(byte mod, RegisterIndex rmIndex, int operandSize)
{
switch (mod)
{
case 0: // [reg] or disp32
@ -81,7 +72,7 @@ public class ModRMDecoder
if (_decoder.CanReadByte())
{
byte sib = _decoder.ReadByte();
return DecodeSIB(sib, 0, operandSize);
return _sibDecoder.DecodeSIB(sib, 0, operandSize);
}
// Fallback for incomplete data
@ -99,7 +90,7 @@ public class ModRMDecoder
{
byte sib = _decoder.ReadByte();
sbyte disp8 = (sbyte)(_decoder.CanReadByte() ? _decoder.ReadByte() : 0);
return DecodeSIB(sib, (uint)disp8, operandSize);
return _sibDecoder.DecodeSIB(sib, (uint)disp8, operandSize);
}
// Fallback for incomplete data
@ -133,7 +124,7 @@ public class ModRMDecoder
{
byte sib = _decoder.ReadByte();
uint disp32 = _decoder.ReadUInt32();
return DecodeSIB(sib, disp32, operandSize);
return _sibDecoder.DecodeSIB(sib, disp32, operandSize);
}
// Fallback for incomplete data
@ -197,28 +188,19 @@ public class ModRMDecoder
/// Reads and decodes a ModR/M byte for standard 32-bit operands
/// </summary>
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM()
{
return ReadModRMInternal(false);
}
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM() => ReadModRMInternal(false);
/// <summary>
/// Reads and decodes a ModR/M byte for 64-bit operands
/// </summary>
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM64()
{
return ReadModRMInternal(true);
}
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM64() => ReadModRMInternal(true);
/// <summary>
/// Reads and decodes a ModR/M byte for 8-bit operands
/// </summary>
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
public (byte mod, RegisterIndex8 reg, RegisterIndex8 rm, Operand operand) ReadModRM8()
{
return ReadModRM8Internal();
}
public (byte mod, RegisterIndex8 reg, RegisterIndex8 rm, Operand operand) ReadModRM8() => ReadModRM8Internal();
/// <summary>
/// Reads and decodes a ModR/M byte for 16-bit operands
@ -331,38 +313,4 @@ public class ModRMDecoder
return (mod, reg, rm, operand);
}
/// <summary>
/// Decodes a SIB byte
/// </summary>
/// <param name="sib">The SIB byte</param>
/// <param name="displacement">The displacement value</param>
/// <param name="operandSize">The size of the operand in bits (8, 16, 32, or 64)</param>
/// <returns>The decoded SIB operand</returns>
private Operand DecodeSIB(byte sib, uint displacement, int operandSize)
{
// Delegate to the SIBDecoder
return _sibDecoder.DecodeSIB(sib, displacement, operandSize);
}
/// <summary>
/// Gets the register name based on the register index and size
/// </summary>
/// <param name="regIndex">The register index as RegisterIndex enum</param>
/// <param name="size">The register size (16 or 32 bits)</param>
/// <returns>The register name</returns>
public static string GetRegisterName(RegisterIndex regIndex, int size)
{
return RegisterMapper.GetRegisterName(regIndex, size);
}
/// <summary>
/// Gets the 8-bit register name based on the RegisterIndex8 enum value
/// </summary>
/// <param name="regIndex8">The register index as RegisterIndex8 enum</param>
/// <returns>The 8-bit register name</returns>
public static string GetRegisterName(RegisterIndex8 regIndex8)
{
return RegisterMapper.GetRegisterName(regIndex8);
}
}

View File

@ -28,7 +28,7 @@ public class BaseRegisterMemoryOperand : MemoryOperand
/// </summary>
public override string ToString()
{
var registerName = ModRMDecoder.GetRegisterName(BaseRegister, 32);
var registerName = RegisterMapper.GetRegisterName(BaseRegister, 32);
return $"{GetSizePrefix()}[{registerName}]";
}
}

View File

@ -36,7 +36,7 @@ public class DisplacementMemoryOperand : MemoryOperand
public override string ToString()
{
string sign = Displacement >= 0 ? "+" : "-";
var registerName = ModRMDecoder.GetRegisterName(BaseRegister, 32);
var registerName = RegisterMapper.GetRegisterName(BaseRegister, 32);
string formattedDisplacement = $"0x{Displacement:X2}";

View File

@ -26,6 +26,6 @@ public class Register8Operand : Operand
/// </summary>
public override string ToString()
{
return ModRMDecoder.GetRegisterName(Register);
return RegisterMapper.GetRegisterName(Register);
}
}

View File

@ -27,6 +27,6 @@ public class RegisterOperand : Operand
/// </summary>
public override string ToString()
{
return ModRMDecoder.GetRegisterName(Register, Size);
return RegisterMapper.GetRegisterName(Register, Size);
}
}

View File

@ -50,8 +50,8 @@ public class ScaledIndexMemoryOperand : MemoryOperand
/// </summary>
public override string ToString()
{
string baseRegPart = BaseRegister != null ? $"{ModRMDecoder.GetRegisterName(BaseRegister.Value, 32)}+" : "";
string indexPart = $"{ModRMDecoder.GetRegisterName(IndexRegister, 32)}*{Scale}";
string baseRegPart = BaseRegister != null ? $"{RegisterMapper.GetRegisterName(BaseRegister.Value, 32)}+" : "";
string indexPart = $"{RegisterMapper.GetRegisterName(IndexRegister, 32)}*{Scale}";
string dispPart = "";
if (Displacement != 0)