Read the IL code (1) and read the il code

Source: Internet
Author: User
Tags mscorlib reflector

Read the IL code (1) and read the il code

When I first started learning C #, some experts told me to learn about the IL code, after reading the code, you can better understand how the code you wrote runs and calls each other. However, at that time, I didn't go to the system, but it was not too late. When I first started reading the IL code, I felt very hard and had a lot to understand. Later, I began to look at it and finally I could understand it a little bit.

Let's talk about the IL code later.

1. What is IL code?

IL, also known as "pencil" and "MSIL", is short for Intermediate Language in the. NET Framework. As mentioned in the previous article, the C-producer compiler of Visual Studio can directly compile the source program written by C-producer into .exe or. dll files, which store the IL code. The CPU of these codes is not acceptable and can only be executed after JIT compilation.

2. How to Study IL

The code format of IL is special, and it seems difficult to understand it all. However, there is a law in this world called the "20% principle". 80% of people grasp of the world's wealth. This is the same in programming. 80% of the functions can be completed only by 20% of the technology, but another 20% may need 80% of the technology. The same is true for learning the IL code. It has more than 200 commands (you can see here: IL commands). We only need to learn the commonly used 20% commands to solve the 80% problem. In any case, you need to read more, and you will naturally understand it when you read more.

3. How to view the source code

(1) first write the normal program and compile

(2) Find the file with the extension exe in the Bin folder.

(3) drag Reflector (I am used to looking at the decompilation code here), or use other decompilation software, such as ILDasm and ILSpy. For beginners, I suggest using ILDasm, because it comes with Microsoft.

I found two images on the Internet using ILDasm. You can refer to them for reference.

The above two figures use ILDasm. I am still used to using Reflector.

On the right is the legendary IL code. Does it look complicated? It shouldn't be complicated. Let's take a few more minutes to explain it.

// Call Stack is a Call Stack, a list of local variables used to store. locals init ([0] int32 num, [1] int32 num2, [2] int32 num3) initialization variables.

// Evaluation Stack is also an Evaluation Stack used to store values. For example, commands such as ldc. i4.1 Push 1 to the Stack and wait for the operation.

// Stack is an advanced post-release data structure.

// The hidebysig command indicates that if the current class is a parent class, the method marked with this command will not inherit the quilt class.
// It indicates that the code in the method body is IL code and managed code, that is, the code running on the CLR Runtime Library.

. Method private hidebysig static voidMain(String [] args) cel managed {. entrypoint// This command represents the entry function of the function program. Each hosted application has only one entry function. When the CLR loads a program, it starts to execute the. entrypoint function.. Maxstack 2// When executing the constructor, the evaluation stack can accommodate the maximum number of data items. The evaluation stack is a memory area that stores the values of variables required in the method. This area is cleared at the end of the method execution, or a return value is stored.. Locals init ([0] int32Num, [1] int32Num2, [2] int32Num3)// Defines int type variables. The names of these variables are num, num2, and num3. Stored in the call stack.L_0000: nop// No operation means that No operation is performed.L_0001: ldc. i4.1// Press "1" into the evaluation stack. "1" is at the top of the evaluation stack.L_0002: stloc.0// This command pops up the value from the evaluation stack and assigns the value to the 0th variable num of the call stack.L_0003: ldc. i4.2 L_0004: stloc.1 L_0005: ldc. i4.3 L_0006: stloc.2// From. locals init to L_0006, it is equivalent to assigning values to I, j, and k in C # code.L_0007: ldloc.0// Push the element 0 in the call stack to the evaluation stack (take the value of I ).L_0008: ldloc.1// Push the element with the position 1 in the call stack to the evaluation stack (take the value of j ).L_0009: add// Perform addition operationsL_000a: ldloc.2// Push the element 2 in the call stack to the evaluation stack (take the value of k ).L_000b: add// Perform addition operationsL_000c: call void [mscorlib] System. Console: WriteLine (int32)// Call the Output MethodL_0011: nop // No Operation L_0012: call valuetype [mscorlib] System. ConsoleKeyInfo [mscorlib] System. Console: ReadKey ()// Call the ReadKey MethodL_0017: pop// Clear the content of the evaluation StackL_0018: ret// Return flag return Value}// The Main method ends.
Through the above code, we can summarize :. maxstack: the variables in the Code need to occupy several locations in the Call Stack ;. locals int (......): Define variable initialization and put it into the Call Stack (Call Stack); nop: No Operation, No Operation; ldstr: Load String, and press the String into the Evaluation Stack; ldc. i4.1: Push the value 2 to the Evaluation Stack in the form of a 4-byte integer; stloc: Assign the value in the Evaluation Stack (Evaluation) to the Call Stack; ldloc: copy the value at the specified position in the Call Stack and press it into the Evaluation Stack. call: Call the specified method. This command is generally used to call static methods; callvir is generally used to call the instance method; ret: return, marking the return.

The above IL code is a relatively simple piece of code, because there is no process control such as conditional judgment. But I think it is not difficult to remember every fixed operation of the IL command. Next, I will write the second part to gain an in-depth understanding of the IL code.

 

Related Article

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.