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:
parent
a91d6af8fc
commit
193f9cd2d8
@ -22,8 +22,6 @@ public class ModRMDecoder
|
|||||||
_decoder = decoder;
|
_decoder = decoder;
|
||||||
_sibDecoder = new SIBDecoder(decoder);
|
_sibDecoder = new SIBDecoder(decoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
// These methods have been moved to the RegisterMapper class
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Decodes a ModR/M byte to get the operand
|
/// Decodes a ModR/M byte to get the operand
|
||||||
@ -32,22 +30,16 @@ public class ModRMDecoder
|
|||||||
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
||||||
/// <param name="is64Bit">True if the operand is 64-bit</param>
|
/// <param name="is64Bit">True if the operand is 64-bit</param>
|
||||||
/// <returns>The operand object</returns>
|
/// <returns>The operand object</returns>
|
||||||
public Operand DecodeModRM(byte mod, RegisterIndex rmIndex, bool is64Bit)
|
public Operand DecodeModRM(byte mod, RegisterIndex rmIndex, bool is64Bit) => DecodeModRMInternal(mod, rmIndex, is64Bit ? 64 : 32);
|
||||||
{
|
|
||||||
return DecodeModRMInternal(mod, rmIndex, is64Bit ? 64 : 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Decodes a ModR/M byte to get an 8-bit operand
|
/// Decodes a ModR/M byte to get an 8-bit operand
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="mod">The mod field (2 bits)</param>
|
/// <param name="mod">The mod field (2 bits)</param>
|
||||||
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
/// <param name="rmIndex">The r/m field as RegisterIndex</param>
|
||||||
/// <returns>The 8-bit operand object</returns>
|
/// <returns>The 8-bit operand object</returns>
|
||||||
public Operand DecodeModRM8(byte mod, RegisterIndex rmIndex)
|
public Operand DecodeModRM8(byte mod, RegisterIndex rmIndex) => DecodeModRMInternal(mod, rmIndex, 8);
|
||||||
{
|
|
||||||
return DecodeModRMInternal(mod, rmIndex, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal implementation for decoding a ModR/M byte to get an operand with specific size
|
/// Internal implementation for decoding a ModR/M byte to get an operand with specific size
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -57,7 +49,6 @@ public class ModRMDecoder
|
|||||||
/// <returns>The operand object</returns>
|
/// <returns>The operand object</returns>
|
||||||
private Operand DecodeModRMInternal(byte mod, RegisterIndex rmIndex, int operandSize)
|
private Operand DecodeModRMInternal(byte mod, RegisterIndex rmIndex, int operandSize)
|
||||||
{
|
{
|
||||||
|
|
||||||
switch (mod)
|
switch (mod)
|
||||||
{
|
{
|
||||||
case 0: // [reg] or disp32
|
case 0: // [reg] or disp32
|
||||||
@ -81,7 +72,7 @@ public class ModRMDecoder
|
|||||||
if (_decoder.CanReadByte())
|
if (_decoder.CanReadByte())
|
||||||
{
|
{
|
||||||
byte sib = _decoder.ReadByte();
|
byte sib = _decoder.ReadByte();
|
||||||
return DecodeSIB(sib, 0, operandSize);
|
return _sibDecoder.DecodeSIB(sib, 0, operandSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback for incomplete data
|
// Fallback for incomplete data
|
||||||
@ -99,7 +90,7 @@ public class ModRMDecoder
|
|||||||
{
|
{
|
||||||
byte sib = _decoder.ReadByte();
|
byte sib = _decoder.ReadByte();
|
||||||
sbyte disp8 = (sbyte)(_decoder.CanReadByte() ? _decoder.ReadByte() : 0);
|
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
|
// Fallback for incomplete data
|
||||||
@ -133,7 +124,7 @@ public class ModRMDecoder
|
|||||||
{
|
{
|
||||||
byte sib = _decoder.ReadByte();
|
byte sib = _decoder.ReadByte();
|
||||||
uint disp32 = _decoder.ReadUInt32();
|
uint disp32 = _decoder.ReadUInt32();
|
||||||
return DecodeSIB(sib, disp32, operandSize);
|
return _sibDecoder.DecodeSIB(sib, disp32, operandSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback for incomplete data
|
// Fallback for incomplete data
|
||||||
@ -197,28 +188,19 @@ public class ModRMDecoder
|
|||||||
/// Reads and decodes a ModR/M byte for standard 32-bit operands
|
/// Reads and decodes a ModR/M byte for standard 32-bit operands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
||||||
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM()
|
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM() => ReadModRMInternal(false);
|
||||||
{
|
|
||||||
return ReadModRMInternal(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads and decodes a ModR/M byte for 64-bit operands
|
/// Reads and decodes a ModR/M byte for 64-bit operands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
||||||
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM64()
|
public (byte mod, RegisterIndex reg, RegisterIndex rm, Operand operand) ReadModRM64() => ReadModRMInternal(true);
|
||||||
{
|
|
||||||
return ReadModRMInternal(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads and decodes a ModR/M byte for 8-bit operands
|
/// Reads and decodes a ModR/M byte for 8-bit operands
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
/// <returns>A tuple containing the mod, reg, rm fields and the decoded operand</returns>
|
||||||
public (byte mod, RegisterIndex8 reg, RegisterIndex8 rm, Operand operand) ReadModRM8()
|
public (byte mod, RegisterIndex8 reg, RegisterIndex8 rm, Operand operand) ReadModRM8() => ReadModRM8Internal();
|
||||||
{
|
|
||||||
return ReadModRM8Internal();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Reads and decodes a ModR/M byte for 16-bit operands
|
/// Reads and decodes a ModR/M byte for 16-bit operands
|
||||||
@ -331,38 +313,4 @@ public class ModRMDecoder
|
|||||||
|
|
||||||
return (mod, reg, rm, operand);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -28,7 +28,7 @@ public class BaseRegisterMemoryOperand : MemoryOperand
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var registerName = ModRMDecoder.GetRegisterName(BaseRegister, 32);
|
var registerName = RegisterMapper.GetRegisterName(BaseRegister, 32);
|
||||||
return $"{GetSizePrefix()}[{registerName}]";
|
return $"{GetSizePrefix()}[{registerName}]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ public class DisplacementMemoryOperand : MemoryOperand
|
|||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
string sign = Displacement >= 0 ? "+" : "-";
|
string sign = Displacement >= 0 ? "+" : "-";
|
||||||
var registerName = ModRMDecoder.GetRegisterName(BaseRegister, 32);
|
var registerName = RegisterMapper.GetRegisterName(BaseRegister, 32);
|
||||||
|
|
||||||
string formattedDisplacement = $"0x{Displacement:X2}";
|
string formattedDisplacement = $"0x{Displacement:X2}";
|
||||||
|
|
||||||
|
@ -26,6 +26,6 @@ public class Register8Operand : Operand
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return ModRMDecoder.GetRegisterName(Register);
|
return RegisterMapper.GetRegisterName(Register);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,6 @@ public class RegisterOperand : Operand
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return ModRMDecoder.GetRegisterName(Register, Size);
|
return RegisterMapper.GetRegisterName(Register, Size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,8 +50,8 @@ public class ScaledIndexMemoryOperand : MemoryOperand
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
string baseRegPart = BaseRegister != null ? $"{ModRMDecoder.GetRegisterName(BaseRegister.Value, 32)}+" : "";
|
string baseRegPart = BaseRegister != null ? $"{RegisterMapper.GetRegisterName(BaseRegister.Value, 32)}+" : "";
|
||||||
string indexPart = $"{ModRMDecoder.GetRegisterName(IndexRegister, 32)}*{Scale}";
|
string indexPart = $"{RegisterMapper.GetRegisterName(IndexRegister, 32)}*{Scale}";
|
||||||
string dispPart = "";
|
string dispPart = "";
|
||||||
|
|
||||||
if (Displacement != 0)
|
if (Displacement != 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user