It's so easy to read IL code (i)

Source: Internet
Author: User
Tags return tag

A preface

Thank you @ Ice Lin Light Wu pointed out the error of the article, has now been corrected

For the IL code does not understand the total feeling is magical, the first day to see completely unintelligible, only listen to the experts said, understand the IL code you can more clearly know how your code is running mutual calls, this is an unknown feeling.

Then began to contact IL, understand a period of time before discovering that the original read IL code is not difficult. Go to the Chase

1.1 What is Il

Il is an abbreviation for the intermediate language (intermediate Language) in the. NET Framework. Use. NET Framework provides a compiler that can directly compile the source program into an. exe or. dll file, but the compiled program code is not a machine code that the CPU can execute directly, but rather an intermediate language Il (intermediate Language) code (source Baidu)

1.2 Why to know IL

In a lot of times do not understand how the code operation can be explained by the IL command, for example, boxing, unpacking is just listen to others say or the book is how how to achieve, have you confirmed it? Understanding IL directives you can see clearly how each step is handled.

1.3 How to learn il

There is a law called "28 law", 80% of the function, as long as 20% of the technology can be completed, but to complete the other 20% may require 80% technology, for the IL Code is also the same, there are more than 200 instructions, we only need to use its 20% instructions to solve our 80% problem, So I will not write too much, just let everyone can read the ordinary program code compiled into IL code after the line, there is to see more, IL code each instruction is a specific meaning, see more natural understand, when the code in question when you try to see its corresponding IL code, perhaps you will know more.

Il command Daquan here

Il code compiler ILDasm DOT here

Two how to view IL code

 2.1 Steps

1 write code and compile through

2. exe file found under obj file for source file

3 Import into ildasm to decompile into IL code

1-2 Step 3 Import into ILDASM

Meaning of icons in ildasm

Three how to read Il (roughly understand)

After the above steps are completed, we can see the code is compiled with the IL Code, the lower part of each IL directive will be explained in detail

C # code

1         static void Main (string[] args) 2         {3             int i = 1;4             int j = 2;5             int k = 3;6             Console.WriteLine (i+j+k) ; 7         }

Il code

The call stack is a stack, and the record frame in the call stack is a list of local variables for storing. Locals init (int32 v_0,int32 v_1,int32 v_2) parameters after initialization v_0,v_1,v_ 2

The record frame was not marked in the figure, so I drew a picture of myself.

Evaluation stack is a stack ldc.i4.2 this instruction will first push the value into the stack to wait for the operation

In the fourth paragraph, you can understand a little more clearly.

In addition @learning hard pointed out that the IL Directive 9th 11 13 line is easy to misunderstand the value is loaded from the record frame

It is now emphasized that the ldc.i4.1,ldc.i4.2,ldc.i4.3 of the 9th 11 13 line in the IL instruction is not loaded into the record frame, but MSDN does not indicate where to load the

So only according to the individual's own idea, the program in the compiled value type data will be present in the thread stack, so I think the value of 9 11 13 rows at this time is taken from the thread stack

1  . Method private hidebysig static void  Main (string[] args) cil managed 2 {3   . entrypoint//  Program Entry 4   // Code size       0x13 5   . maxstack  3  //defines the maximum depth of the stack used by the function code, and also refers to a maximum of 3 values in evaluation Stackk
6 //Below we think of it as the initialization in the completion code 7 . Locals init (int32 v_0,int32 v_1,int32 v_2)//define int type parameter v_0,v_1,v_2 (now V_0,v_1,v _2 deposited in the record frame in call stack) 8 il_0000: NOP//No Operation no action, we don't have to take care of it
9 il_0001: ldc.i4.1 //load the value of the first variable "I" (pressed into the evaluation stack ) il_0002: stloc.0 POPs the value of "I" from the stack and assigns it to the No. 0 position in the record frame (V_0)
   one il_0003:  ldc.i4.2    //load the value of the second variable "J" (pressed into the evaluation stack)   il_0004:  stloc.1     // POPs the value of "J" from the stack and assigns it to the 1th position in the record frame (v_1)
   il_0005:  ldc.i4.3    //load the value of the third variable "K" (pressed into the evaluation stack)   il_0006:  stloc.2     // POPs the value of "K" from the stack and assigns it to the 2nd position in the record frame (v_2)
After the    initialization of the above code is completed, the output is started, so the data is removed from the record frame.   il_0007:  ldloc.0     //Take the element with position 0 in the record frame (V_0) Value (the value of "I") and pressed into the stack (equivalent to the value of V_0 in the call stack for copy one). The value of the v_0 itself is constant)   il_0008:  ldloc.1     //Take the value of the element (V_1) with position 1 in the record frame (the value of "J") and press it into the stack (IBID.)   il_0009:  Add         //Do addition operation   il_000a:  ldloc.2     //Take out the value of the element (V_2) in the record frame with position 2 (the value of "K") and press it into the stack of the   il_000b:  Add         //Do addition operation   il_000c:       call void [Mscorlib]system.console::writeline (Int32)//Invoke Output method 24   il_0011:  nop25   il_0012:  ret//         Returns the value of the return  token of  //end of method Program::main

Instruction detailed

. maxstack: The maximum number of data items that the evaluation stack (Evaluation stack) can hold

. Locals init (int32 v_0,int32 v_1,int32 v_2): Define variables and deposit in the record frame in the call stack

NOP: No Operation no action, we don't have to worry about it,

ldstr.: Load string to compress strings into the evaluation stack

stloc.: Assigns values from the evaluation stack to the record frame in the call stack

ldloc.: Remove the value of the specified position in the record frame in the call stack (copy) into the Evaluation stack above two instructions for each other operation stloc assignment, ldloc value

Call: Invokes the specified method

RET: The return tag returns

Each IL code is added to the comment, is not that the IL code is not really difficult, because each of its instructions are fixed, you just remember, look at the IL code is more relaxed.

Four how to read Il (in-depth understanding)

4.1 Ask questions

With a little bit of IL Foundation above, now we're going to dig a little bit,

Here are a few questions:

1 when the ldc.i4.1 specifies that the "i" variable is not immediately assigned to the element in the record frame, but is not assigned until the stloc.0 is executed, where does it exist before it is assigned?

2 ldloc.0 When the element is taken out, where does it exist?

3 Where does the value exist after the add operation is completed?

4.2 Concept Introduction

Managed Heap: This is the memory of the Dynamic Allocation, which is managed by the garbage Collector (GC), and the entire Process is shared with a

Managed Heap (which I understand as the managed heap, stores values of reference types ).

Evaluation Stack: This is a memory that is managed by the. NET CLR at run time, each thread has its own Evaluation Stack (which I understand as a line stacks that temporarily holds the value type data )

Call Stack: This is a memory that is managed by the. NET CLR at run time, with each Thread having its own call Stack. Each call to a method causes a record frame to be placed on the call Stack, and after that, the record frame is discarded ( I understand it as a local variable table for storing . Locals init (i Nt32 v_0) directive parameter values such as: V_0)

4.3 Il instruction detailed

After explaining the three nouns, let's take a closer look at how the corresponding variables are stored when the IL directive is executed.

il_0001:  ldc.i4.1    //Load First variable i  
When the-1 o'clock il instruction is ldc.i4.M1, when more than 8 o'clock is a unified directive LDC.I4.S

il_0001: ldc.i4.1 //Load First variable i

When executing this instruction, the value of the variable i is pressed into the evaluation stack for temporary storage.



il_0002:  stloc.0     //Assign I to No. 0 position in call stack




il_0007:  ldloc.0     //remove element with record frame position 0 (i)
When this instruction is executed, the value of the element at position 0 in the Record frame is removed (copy) and pressed into the evaluation Stack waiting for the add

IL_000B:  Add         //Do addition operation
When the add operation is complete, the result will be present in the evaluation stack waiting for the next instruction operation
4.4 Questions answered
The above content to read the beginning of the problem corresponding also resolved
1 ldc.i4.1 The value is taken out before it is placed in the element specified in the record frame after it has been executed in the Evaluation stack stloc.0
2 ldloc.0 will be pressed into the Evaluation Stack and other holding instructions after it is taken out.
3 when the Add operation is complete, the value is staged in the Evaluation stack.


The above is a very basic introduction to how the IL instruction operates in memory, so that you know how to manipulate the values in memory when you understand the IL directive. I think it might be a little more thorough to understand the IL directive.


Five summary


This article only writes the most basic instructions in IL, and then explains how the IL instruction operates in-memory data. The Ancients Cloud: water to take a mouthful of drink, the road to step after step, the pace of big easy to pull the egg, slowly to the content although less points, but there will be the next article. The next article or will write some basic instructions IL, I will combine my own understanding, as far as possible to write a bit more popular, so that people easier to understand.


Citation to: http://www.cnblogs.com/zery/p/3366175.html

It's so easy to read IL code (i)

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.