Mosa.Platform.x86.MachineCodeEmitter.CalculateModRM C# (CSharp) Method

CalculateModRM() private static method

Calculates the value of the modR/M byte and SIB bytes.
private static CalculateModRM ( byte regField, Operand op1, Operand op2 ) : byte?
regField byte The modR/M regfield value.
op1 Mosa.Compiler.Framework.Operand The destination operand.
op2 Mosa.Compiler.Framework.Operand The source operand.
return byte?
        private static byte? CalculateModRM(byte? regField, Operand op1, Operand op2)
        {
            byte? modRM = null;

            bool op1IsRegister = (op1 != null) && op1.IsCPURegister;
            bool op2IsRegister = (op2 != null) && op2.IsCPURegister;

            Debug.Assert(!(!op1IsRegister && op2IsRegister));

            if (regField != null)
                modRM = (byte)(regField.Value << 3);

            if (op1IsRegister && op2IsRegister)
            {
                // mod = 11b, reg = rop1, r/m = rop2
                modRM = (byte)((3 << 6) | (op1.Register.RegisterCode << 3) | op2.Register.RegisterCode);
            }
            else if (op1IsRegister)
            {
                modRM = (byte)(modRM.GetValueOrDefault() | (3 << 6) | op1.Register.RegisterCode);
            }

            return modRM;
        }