private byte[] Encode(OpCode opCode, ModRegRM modReg = null, Constant constant = null)
        {
            List <byte> bytes = new List <byte>();

            bytes.AddRange(opCode.GetBytes());

            if (opCode.Type.HasModRegRM)
            {
                bytes.AddRange(modReg.Encode());
            }

            if (constant != null)
            {
                var label = constant as Label;

                if (label != null)
                {
                    var size = 0;
                    size += opCode.Size;
                    if (opCode.Type.HasModRegRM)
                    {
                        size += modReg.Size;
                    }

                    if (opCode.Is32Bit)
                    {
                        bytes.AddRange(BitConverter.GetBytes((uint)0));
                    }
                    else
                    {
                        bytes.AddRange(BitConverter.GetBytes((ushort)0));
                    }

                    label.References.Add((uint)(Offset + size));
                }
                else
                {
                    if (opCode.Is32Bit)
                    {
                        bytes.AddRange(BitConverter.GetBytes((uint)constant));
                    }
                    else
                    {
                        bytes.AddRange(BitConverter.GetBytes((ushort)constant));
                    }
                }
            }

            return(bytes.ToArray());
        }
Ejemplo n.º 2
0
        public byte[] Encode()
        {
            var bytes = new List <byte>();

            bytes.AddRange(OpCode.GetBytes());
            if (OpCode.Type.HasModRegRM && ModRegRM != null)
            {
                bytes.AddRange(ModRegRM.Encode());
            }

            if (Constant != null)
            {
                var label = Constant as Label;
                if (label != null)
                {
                    var size = 0;
                    size += OpCode.Size;
                    if (OpCode.Type.HasModRegRM)
                    {
                        size += ModRegRM.Size;
                    }

                    label.References.Add((uint)size);
                    if (OpCode.Is32Bit)
                    {
                        bytes.AddRange(BitConverter.GetBytes((uint)0));
                    }
                    else
                    {
                        bytes.AddRange(BitConverter.GetBytes((ushort)0));
                    }
                }
                else
                {
                    if (OpCode.Is32Bit)
                    {
                        bytes.AddRange(BitConverter.GetBytes((uint)Constant));
                    }
                    else
                    {
                        bytes.AddRange(BitConverter.GetBytes((ushort)Constant));
                    }
                }
            }

            return(bytes.ToArray());
        }