namespace X86Disassembler.X86;
using Operands;
///
/// Handles mapping between register indices and register enums
///
public static class RegisterMapper
{
///
/// Maps the register index from the ModR/M byte to the RegisterIndex enum value
///
/// The register index from the ModR/M byte (0-7)
/// The corresponding RegisterIndex enum value
public static RegisterIndex MapModRMToRegisterIndex(int modRMRegIndex)
{
// The mapping from ModR/M register index to RegisterIndex enum is:
// 0 -> A (EAX)
// 1 -> C (ECX)
// 2 -> D (EDX)
// 3 -> B (EBX)
// 4 -> Sp (ESP)
// 5 -> Bp (EBP)
// 6 -> Si (ESI)
// 7 -> Di (EDI)
return modRMRegIndex switch
{
0 => RegisterIndex.A, // EAX
1 => RegisterIndex.C, // ECX
2 => RegisterIndex.D, // EDX
3 => RegisterIndex.B, // EBX
4 => RegisterIndex.Sp, // ESP
5 => RegisterIndex.Bp, // EBP
6 => RegisterIndex.Si, // ESI
7 => RegisterIndex.Di, // EDI
_ => RegisterIndex.A // Default to EAX
};
}
///
/// Maps the register index from the ModR/M byte to the RegisterIndex8 enum value
///
/// The register index from the ModR/M byte (0-7)
/// The corresponding RegisterIndex8 enum value
public static RegisterIndex8 MapModRMToRegisterIndex8(int modRMRegIndex)
{
// The mapping from ModR/M register index to RegisterIndex8 enum is direct:
// 0 -> AL, 1 -> CL, 2 -> DL, 3 -> BL, 4 -> AH, 5 -> CH, 6 -> DH, 7 -> BH
return (RegisterIndex8)modRMRegIndex;
}
///
/// Maps a RegisterIndex8 enum value to the corresponding RegisterIndex enum value for base registers
///
/// The RegisterIndex8 enum value
/// The corresponding RegisterIndex enum value
public static RegisterIndex MapRegister8ToBaseRegister(RegisterIndex8 regIndex8)
{
// Map 8-bit register indices to their corresponding 32-bit register indices
return regIndex8 switch
{
RegisterIndex8.AL => RegisterIndex.A,
RegisterIndex8.CL => RegisterIndex.C,
RegisterIndex8.DL => RegisterIndex.D,
RegisterIndex8.BL => RegisterIndex.B,
RegisterIndex8.AH => RegisterIndex.A,
RegisterIndex8.CH => RegisterIndex.C,
RegisterIndex8.DH => RegisterIndex.D,
RegisterIndex8.BH => RegisterIndex.B,
_ => RegisterIndex.A // Default to EAX
};
}
///
/// Gets the register name based on the register index and size
///
/// The register index as RegisterIndex enum
/// The register size (16, 32, or 64 bits)
/// The register name
public static string GetRegisterName(RegisterIndex regIndex, int size)
{
return size switch
{
16 => Constants.RegisterNames16[(int)regIndex],
32 => Constants.RegisterNames32[(int)regIndex],
64 => Constants.RegisterNames32[(int)regIndex], // For now, reuse 32-bit names for 64-bit
_ => "unknown"
};
}
///
/// Gets the 8-bit register name based on the RegisterIndex8 enum value
///
/// The register index as RegisterIndex8 enum
/// The 8-bit register name
public static string GetRegisterName(RegisterIndex8 regIndex8)
{
return regIndex8.ToString().ToLower();
}
}