Exemplo n.º 1
0
        private static void MethodResolver(MethodContext context)
        {
            if (context.Method == GetMethod <ResolveNativeTests>(nameof(SimpleSum)))
            {
                Assembler assembler = new Assembler(64);

                int stackSize = 4;
                assembler.push(rbp);
                assembler.sub(rsp, stackSize);
                assembler.lea(rbp, __[rsp + stackSize]);
                assembler.mov(__dword_ptr[rbp - stackSize], 5);
                assembler.mov(rax, 5);
                assembler.imul(rax, __dword_ptr[rbp - stackSize]);
                assembler.lea(rsp, __[rbp]);
                assembler.pop(rbp);
                assembler.ret();

                using (MemoryStream stream = new MemoryStream())
                {
                    assembler.Assemble(new StreamCodeWriter(stream), 0);

                    context.ResolveNative(stream.ToArray());
                }
            }
            else if (context.Method == GetMethod <ResolveNativeTests>(nameof(LargeSum)))
            {
                Assembler assembler = new Assembler(64);

                int stackSize = 4;
                assembler.push(rbp);
                assembler.sub(rsp, stackSize);
                assembler.lea(rbp, __[rsp + stackSize]);
                assembler.mov(rdx, 10);
                assembler.mov(rax, 10);

                for (int i = 0; i < 999; i++)
                {
                    assembler.add(rax, rdx);
                }

                assembler.lea(rsp, __[rbp]);
                assembler.pop(rbp);
                assembler.ret();

                using (MemoryStream stream = new MemoryStream())
                {
                    assembler.Assemble(new StreamCodeWriter(stream), 0);

                    context.ResolveNative(stream.ToArray());
                }
            }
        }
Exemplo n.º 2
0
        private static void MethodResolver(MethodContext context)
        {
            if (context.Method.Name == "SimpleSum")
            {
                Assembler assembler = new Assembler(64);

                //Replace with fatorial number:
                //int sum = num1+num2;
                //int fatorial = 1;
                //for(int i = 2; i <= sum; i++){
                //    fatorial *= i;
                //}
                //return fatorial;
                assembler.add(edx, ecx);
                assembler.mov(eax, 1);
                assembler.mov(ecx, 2);
                assembler.cmp(edx, 0x02);
                assembler.jl(assembler.@F);
                assembler.AnonymousLabel();
                assembler.imul(eax, ecx);
                assembler.inc(ecx);
                assembler.cmp(ecx, edx);
                assembler.jle(assembler.@B);
                assembler.AnonymousLabel();
                assembler.ret();

                using MemoryStream ms = new MemoryStream();
                assembler.Assemble(new StreamCodeWriter(ms), 0);

                byte[] asm = ms.ToArray();

                context.ResolveNative(asm);
            }
        }