is there such thing as a 'while' loop opcode.
what are the opcodes i use to emit this loop:
int i = 0;
while(true)
{
if(i == 3)
{
break;
}
i++;
}
looking for something performance specific,
thanks in advance,
Jer,
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Bechir BejaouiRank #144 · 15.9K reputationPosted Dec 1, 2008, 1:45 AM EST
J A13 reputationPosted Dec 1, 2008, 9:04 AM EST
using Reflection.Emit, dotnet api, you can code performance critical operations using opcodes from csharp.
I am trying to learn how to use opcodes, and I would like to see how do take this standard csharp code, and code it using Emit opcodes.
I have been scouring the net trying to find if there is a specific opcode for a 'while' expression.
I hope this gives you enough info, so you can help.
thanks in advance,
Jer
AlanRank #669 · 3.6K reputationPosted Dec 1, 2008, 11:44 AM EST
There isn't a specific opcode for a while loop. It's coded in much the same as it would be in assembler.
If you put that code in a method and then examine the output with a disassembler such as ildasm or Redgate Reflector you'll see which opcodes are actually generated in the MSIL.
J A13 reputationPosted Dec 1, 2008, 7:08 PM EST
I have attempted to write this using emit.
Thanks in advance for any help.
-Jer
int i = 0;
while(true)
{
i++;
if(i >= 100)
{
break;
}
}
I get:
Unhandled Exception: System.InvalidProgramException: JIT Compiler encountered an
internal limitation.
at Hello()
at test.Main(String[] d)
test.cs
//----------------------------------------
using System;
using System.Reflection;
using System.Reflection.Emit;
public class test {
private delegate int HelloDelegate();
public static void Main (string [] d)
{
DynamicMethod hello = new DynamicMethod("Hello", typeof(int), null, typeof(string).Module);
Type[] writeStringArgs = {typeof(string)};
MethodInfo writeString = typeof(Console).GetMethod("WriteLine",
writeStringArgs);
ILGenerator il = hello.GetILGenerator(256);
Label label1 = il.DefineLabel();
Label label2 = il.DefineLabel();
il.Emit(OpCodes.Ldc_I4_0); // 1 on stack
il.MarkLabel(label2);
il.Emit(OpCodes.Ldstr,"test"); // 2 on stack
il.EmitCall(OpCodes.Call, writeString,null); // pop
il.Emit(OpCodes.Ldc_I4_1); // 2 on stack
il.Emit(OpCodes.Add); // 1 on stack
il.Emit(OpCodes.Dup); // 2 on stack
il.Emit(OpCodes.Ldc_I4,100); // 3 on stack
il.Emit(OpCodes.Bge, label1); // if true, no stack
il.Emit(OpCodes.Pop); // 1 popped
il.Emit(OpCodes.Pop); // 1 popped
il.Emit(OpCodes.Br_S,label2); // loop
il.MarkLabel(label1);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Ret);
HelloDelegate hi =
(HelloDelegate) hello.CreateDelegate(typeof(HelloDelegate));
hi();
}
}