static void Main(string[] args)
{
DynamicMethod add = new DynamicMethod("add", typeof(int), new Type[] { typeof(int), typeof(int) });
ILGenerator il = add.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); // ldarg.0
il.Emit(OpCodes.Ldarg_1); // ldarg.1
il.Emit(OpCodes.Add); // add
il.Emit(OpCodes.Ret); // ret
int num = (int)add.Invoke(add, new object[] { 1, 2 });
}<span style="font-size:12px;">int Add(int x, int y)
{
int ret;
_asm
{
mov eax, dword ptr[x]
add eax, dword ptr[y]
mov dword ptr[ret], eax
}
return ret;
}</span>
static void Main(string[] args)
{
DynamicMethod _while = new DynamicMethod("while", typeof(int), null);
ILGenerator il = _while.GetILGenerator();
il.DeclareLocal(typeof(int)); // int i
Label IL_0004 = il.DefineLabel(); // IL_0004
Label IL_0008 = il.DefineLabel(); // IL_0008
il.Emit(OpCodes.Ldc_I4_0); // ldc.i4.0
il.Emit(OpCodes.Stloc_0); // stloc.0
il.Emit(OpCodes.Br_S, IL_0008); // br.s IL_0008
il.MarkLabel(IL_0004);
il.Emit(OpCodes.Ldloc_0); // ldloc.0
il.Emit(OpCodes.Ldc_I4_1); // ldc.i4.1
il.Emit(OpCodes.Add); // add
il.Emit(OpCodes.Stloc_0); // stloc.0
il.MarkLabel(IL_0008);
il.Emit(OpCodes.Ldloc_0); // ldloc.0
il.Emit(OpCodes.Ldc_I4_S, 100); // ldc.i4.s 100
il.Emit(OpCodes.Blt_S, IL_0004); // blt.s IL_0004
il.Emit(OpCodes.Ldloc_0); // ldloc.0
il.Emit(OpCodes.Ret); // ret
int num = (int)_while.Invoke(_while, null);
}void main(int argc, char* argv[])
{
__int32 i;
_asm
{
mov dword ptr [i],0
_loop_beige:
cmp dword ptr [i],64h
jge _loop_end
mov eax,dword ptr [i]
add eax,1
mov dword ptr [i],eax
jmp _loop_beige
_loop_end:
}
}
原文地址:http://blog.csdn.net/u012395622/article/details/46545349