Beispiel #1
0
        /// <summary>
        /// 在循环体內,[esp]即迭代的值
        /// 会影响ecx寄存器
        /// </summary>
        /// <param name="body"></param>
        /// <param name="times"></param>
        /// <param name="regProtection"></param>
        /// <returns></returns>
        public static AssemblySnippet Loop(AssemblySnippet body, int times, bool regProtection)
        {
            byte[] lA = new byte[16];
            byte[] lB = new byte[16];
            random.NextBytes(lA);
            random.NextBytes(lB);
            AssemblySnippet s      = new AssemblySnippet();
            string          labelA = "lab_" + string.Concat(lA.Select(t => t.ToString("x2")));
            string          labelB = "lab_" + string.Concat(lB.Select(t => t.ToString("x2")));

            if (regProtection)
            {
                s.Content.Add(Instruction.Create("push ecx"));
            }
            s.Content.Add(Instruction.Create("mov ecx,0"));
            s.Content.Add(Instruction.Create("" + labelA + ":"));
            s.Content.Add(Instruction.Create("cmp ecx," + times + ""));
            s.Content.Add(Instruction.Create("jge " + labelB + ""));
            s.Content.Add(Instruction.Create("push ecx"));
            s.Content.Add(body);
            s.Content.Add(Instruction.Create("pop ecx"));
            s.Content.Add(Instruction.Create("inc ecx"));
            s.Content.Add(Instruction.Create("jmp " + labelA + ""));
            s.Content.Add(Instruction.Create("" + labelB + ":"));
            if (regProtection)
            {
                s.Content.Add(Instruction.Create("pop ecx"));
            }
            return(s);
        }
Beispiel #2
0
        public static AssemblySnippet FromCode(IEnumerable <AssemblyCode> code)
        {
            AssemblySnippet s = new AssemblySnippet();

            s.Content.AddRange(code);
            return(s);
        }
Beispiel #3
0
        public AssemblySnippet Copy()
        {
            AssemblySnippet ss = new AssemblySnippet();

            ss.Content.AddRange(Content);
            return(ss);
        }
Beispiel #4
0
        public static AssemblySnippet FromASMCode(string asm)
        {
            AssemblySnippet s = new AssemblySnippet();

            Instruction[] ss = asm.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).Select(t => Instruction.Create(t)).ToArray();
            s.Content.AddRange(ss);
            return(s);
        }
Beispiel #5
0
        /// <summary>
        /// 只适用于参数不含基础类型以外的值类型的函数
        /// </summary>
        /// <param name="targetAddr"></param>
        /// <param name="retAddr"></param>
        /// <param name="regProtection"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static AssemblySnippet FromDotNetCall(int targetAddr, int?retAddr, bool regProtection, params object[] arguments)
        {
            AssemblySnippet s = new AssemblySnippet();

            if (regProtection)
            {
                s.Content.Add(Instruction.Create("push ecx"));
                s.Content.Add(Instruction.Create("push edx"));
            }
            int i = 0;

            foreach (var v in arguments)
            {
                if (v.GetType() == typeof(byte) || v.GetType() == typeof(sbyte) || v.GetType() == typeof(int) || v.GetType() == typeof(uint) || v.GetType() == typeof(char) || v.GetType() == typeof(short) || v.GetType() == typeof(ushort) || v.GetType() == typeof(string))
                {
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, v)));
                    i++;
                }
                else if (v.GetType() == typeof(float))
                {
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, BitConverter.ToInt32(BitConverter.GetBytes((float)v), 0))));
                    i++;
                }
                else if (v.GetType() == typeof(double))
                {
                    ulong vv = BitConverter.ToUInt64(BitConverter.GetBytes((double)v), 0);
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, (UInt32)(vv & 0xFFFFFFFFUL))));
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, (UInt32)((vv & 0xFFFFFFFF00000000UL) >> 32))));
                    i += 2;
                }
                else if (v.GetType() == typeof(long) || v.GetType() == typeof(ulong))
                {
                    ulong vv = (ulong)v;
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, (UInt32)(vv & 0xFFFFFFFFUL))));
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, (UInt32)((vv & 0xFFFFFFFF00000000UL) >> 32))));
                    i += 2;
                }
                else if (v.GetType() == typeof(bool))
                {
                    s.Content.Add(Instruction.Create(GetArgumentPassing(i, (bool)v ? 1 : 0)));
                    i++;
                }
            }
            s.Content.Add(Instruction.Create("call " + ((int)targetAddr).ToString()));
            if (retAddr != null)
            {
                s.Content.Add(Instruction.Create("mov [" + ((int)retAddr).ToString() + "],eax"));
            }
            if (regProtection)
            {
                s.Content.Add(Instruction.Create("pop edx"));
                s.Content.Add(Instruction.Create("pop ecx"));
            }
            return(s);
        }
Beispiel #6
0
 public Assembler()
 {
     InternalData = AssemblySnippet.FromEmpty();
 }