How to Learn IL on the CLR
.net, clr, il
Solution
Microsoft standardized the CLR and published those standards. Partition III contains a wealth of information about IL/CIL and is suitable for learning. It is an excellent document.
You can also learn IL by example. Compile a few simple methods in C# and look at the IL in reflector (it has an IL-mode).
Problem
Since these IL codes what I see more, I like to learn how to interpret them correctly. I couldn't find a documentation like C# Compiler or any other so I think I can pretty much take care of the rest after I learn this common ones: Below are some sample IL codes containing what I need to know : Sample 1: ``` .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 15 (0xf) .maxstack 1 .locals init ([0] class EnumReflection.DerivedClass derivedClass) IL_0000: nop IL_0001: newobj instance void EnumReflection.DerivedClass::.ctor() IL_0006: stloc.0 IL_0007: ldloc.0 IL_0008: callvirt instance void EnumReflection.DerivedClass::WriteOutput() IL_000d: nop IL_000e: ret } // end of method Program::Main ``` Sample 2: ``` .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // Code size 38 (0x26) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldstr "Hello" IL_0006: stfld string EnumReflection.DerivedClass::hello IL_000b: ldarg.0 IL_000c: ldstr "World" IL_0011: stfld string EnumReflection.DerivedClass::world IL_0016: ldarg.0 IL_0017: ldc.i4.s 123 IL_0019: stfld int32 EnumReflection.DerivedClass::age IL_001e: ldarg.0 IL_001f: call instance void EnumReflection.BaseClass::.ctor() IL_0024: nop IL_0025: ret } // end of method DerivedClass::.ctor ``` I know what these codes do since I produced them :-) however I'd like to learn more about corresponding IL code. These samples contain IL codes like, and could you please explain command with question marks? and also what do those command stand for? So we can memorize them easily. - nop (for debugging - no operation) - newobj (it seems it is creating new object in heap) - stloc.0 ? - ldloc.0 ? - ret ? - ldarg.0 ? - ldstr ? - stfld ? - ldc.i4.s ? - .ctor - constructor Understanding IL is important as it exposes how particular compiler produces codes and act in specific cases. However, I couldn't find a nice docs which contain examples as well about IL. CLR with C# 3.0 is a good book however eventually it isn't a IL book so it doesn't explain everything about IL. EDIT: I've found the specs and they tell these: Thanks to @usr. - nop (for debugging - no operation) - newobj - create a new object - stloc.0 - pop value from stack to local variable - ldloc.0 ? - load local variable onto the stack - ret - return from method - ldarg.0 - Load argument 0 onto the stack. - ldstr - load a literal string - stfld - store into a field of an object - ldc.i4.s - Push num onto the stack as int32 , short form. - .ctor - constructor