Common Intermediate Language - Example

Example

Below is a basic Hello, World program written in CIL. It will display the string "Hello, world!".

.assembly Hello {} .assembly extern mscorlib {} .method static void Main { .entrypoint .maxstack 1 ldstr "Hello, world!" call void System.Console::WriteLine(string) ret }

The following code is more complex in number of opcodes.

This code can also be compared with the corresponding code in the article about Java bytecode.

static void Main(string args) { for (int i = 2; i < 1000; i++) { for (int j = 2; j < i; j++) { if (i % j == 0) goto outer; } Console.WriteLine(i); outer: } }

In CIL syntax it looks like this:

.method private hidebysig static void Main(string args) cil managed { .entrypoint .maxstack 2 .locals init (int32 V_0, int32 V_1) ldc.i4.2 stloc.0 br.s IL_001f IL_0004: ldc.i4.2 stloc.1 br.s IL_0011 IL_0008: ldloc.0 ldloc.1 rem brfalse.s IL_001b ldloc.1 ldc.i4.1 add stloc.1 IL_0011: ldloc.1 ldloc.0 blt.s IL_0008 ldloc.0 call void System.Console::WriteLine(int32) IL_001b: ldloc.0 ldc.i4.1 add stloc.0 IL_001f: ldloc.0 ldc.i4 0x3e8 blt.s IL_0004 ret }

This is just a representation of how CIL looks like near VM-level. When compiled the methods are stored in tables and the instructions are stored as bytes inside the assembly, which is a Portable Executable (PE).

Read more about this topic:  Common Intermediate Language

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)