Analysis of IL Code

Source: Internet
Author: User
Tags comparison table mscorlib

C # code

    Class program
    {
        static void Main (string[] args)
        {
            string b = getage ();

            Console.readkey ();
        }

        private static string Getage ()
        {
            var a = "a";
            var B = "a";
            var C = A;
            return c;
        }
    }




using reflector to decompile IL code

. class private Auto ANSI BeforeFieldInit program extends [mscorlib]System.Object {. method public hidebysig Specia LName rtspecialname instance void. ctor () cil managed {. maxstack 8 l_0000:ldarg.0 L_0001:c  All instance void [Mscorlib]system.object::.ctor () L_0006:ret}. Method private Hidebysig static string Getage () cil managed {. maxstack 1. Locals init ([0] String str, [1] string STR2, [2] string STR3, [3] string str4) L_0000:nop l_0001:ldstr "a" l_0 
        006:stloc.0 l_0007:ldstr "A" l_000c:stloc.1 l_000d:ldloc.0 l_000e:stloc.2 

    L_000f:ldloc.2 l_0010:stloc.3 l_0011:br.s l_0013 l_0013:ldloc.3 L_0014:ret}
        . method private Hidebysig static void Main (string[] args) cil managed {. entrypoint. maxstack 1
 . Locals Init (           [0] string str) L_0000:nop L_0001:call string ilsample.program::getage () L_0006:stl oc.0 L_0007:call valuetype [Mscorlib]system.consolekeyinfo [Mscorlib]system.console::readkey () l_000c:p

 Op L_000d:ret}}


keyword explanation

1. What does the Auto keyword in class mean.

Memory layout (Layout) and size (size) of struct instance fields in the. NET Managed Environment, "StructLayout characteristics"



2. What does the BeforeFieldInit keyword mean in class.

This keyword indicates when the runtime decides when to call a static constructor, or at the beginning of a call to a function, or just before the first time a static member is accessed or the first time it is instantiated.

Anyway, it's the runtime's decision, as long as it's done before you use the class.

Without this keyword, the runtime invokes the static constructor just before the first access to the static member or the first instantiation.

As for C # compiled Il what case has this keyword when there is no keyword, it is easy to judge, if there is an explicit static constructor, there is no this keyword, if there is no explicit constructor, there is this keyword.


There are a lot of interesting things about beforefieldinit, and you can refer to these garden masters in the following articles

Questions about type initializer and beforefieldinit, see if you can give us the right explanation.

"[You must know. NET] The 23rd time: taste details, in-depth." NET Type Builder

"Understanding of the BeforeFieldInit"

The execution time of static constructors



3.. entrypoint What does the keyword mean.

EntryPoint This is relatively simple, is the entry point of the program, if it is. EXE's assembly has and can only have one, if it is. The assembly of the DLL, of course, without an entry point.

C # 's main () function is added by the compiler. The Entrypoint,il compiler does not recognize main (), only entrypoint, so in Il you can make a function as an entry. No need to be main ().


4. What does the Hidebysig keyword in method mean.

Basically equivalent to the new keyword in C #


5. IL_00XX in front of each instruction: It is God horse thing.

Il_xxxx: In fact, is the label of the statement, where these markings are ildasm generated, in terms of the implementation of the program does not make sense.

We can also write labels according to our own situation, such as "Begin:end:" and so on.

can also be labeled to allow the program to jump, C # does not have this function.

The next question is il_xxxx after the number of XXXX, what is the number of. In fact, it is quite obvious that it is relative to the address offset of the method entry.

In order to prove this, I did not think of any good way, casually wrote a C # program, decompile the

il_0000:  ldstr      
IL_0005: Call
il_000a: ldstr
IL_000F: Call
il_0014: ldstr
il_0019: Call
IL_001E: ldc.i4.3
IL_001F: stloc.0
il_0020: ldc.i4.4
il_0021: stloc.1
il_0022: ldloc.0
il_0023: ldloc.1
il_0024: Add
il_0025: stloc.2 //0c
il_0026: ldloc.2
il_0027: Call
IL_002C: ret

Opened the. exe file with UltraEdit.

Find this IL code to represent the binary code (OPCODE), and of course find a way to find each instruction corresponding to the OpCode,

For example 0X72 Representative LDSTR;0X28 Representative call;0x19 representative ldc.i4.3 more need to refer to the MSIL instruction \ Operation \opcode Comparison table

It's easy to see the relationship between code address offsets, which is listed in the order of memory.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.