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); } }
void At_most_one_anon_label_per_instruction() { var c = new Assembler(64); c.nop(); c.AnonymousLabel(); Assert.Throws <InvalidOperationException>(() => c.AnonymousLabel()); }
void Anonymous_labels() { var c = new Assembler(64); var lbl1 = c.CreateLabel(); var lbl2 = c.CreateLabel(); var lbl3 = c.CreateLabel(); var lbl4 = c.CreateLabel(); c.Label(ref lbl1); c.inc(eax); c.nop(); c.AnonymousLabel(); c.je(c.@B); c.nop(); c.Label(ref lbl2); c.je(c.@B); c.nop(); c.jmp(lbl1); c.nop(); c.jmp(lbl2); c.nop(); c.jmp(lbl3); c.nop(); c.jmp(lbl4); c.nop(); c.jne(c.@F); c.nop(); c.Label(ref lbl3); c.jne(c.@F); c.nop(); c.AnonymousLabel(); c.inc(eax); c.nop(); c.Label(ref lbl4); c.nop(); c.nop(); var expectedData = new byte[] { 0xFF, 0xC0, 0x90, 0x74, 0xFE, 0x90, 0x74, 0xFB, 0x90, 0xEB, 0xF5, 0x90, 0xEB, 0xF8, 0x90, 0xEB, 0x07, 0x90, 0xEB, 0x0A, 0x90, 0x75, 0x04, 0x90, 0x75, 0x01, 0x90, 0xFF, 0xC0, 0x90, 0x90, 0x90, }; var writer = new CodeWriterImpl(); c.Assemble(writer, 0); Assert.Equal(expectedData, writer.ToArray()); }
void Unused_anonymous_label_throws() { var c = new Assembler(64); c.nop(); c.AnonymousLabel(); Assert.Throws <InvalidOperationException>(() => c.Assemble(new CodeWriterImpl(), 0)); }
void Anonymous_label_and_named_label_cant_use_same_instruction() { { var c = new Assembler(64); var lbl = c.CreateLabel(); c.nop(); c.Label(ref lbl); c.AnonymousLabel(); Assert.Throws <InvalidOperationException>(() => c.nop()); } { var c = new Assembler(64); var lbl = c.CreateLabel(); c.nop(); c.AnonymousLabel(); c.Label(ref lbl); Assert.Throws <InvalidOperationException>(() => c.nop()); } }