Step by step teach you to understand the IL in NET

Source: Internet
Author: User

I have been in touch with NET for about a year, and the internal implementation of NET has become very attractive to me. I personally think: If you can understand and be proficient in the implementation of these bottom layers, it will be of great help to write your own code in the future. Well, I will not talk much about it. Please refer to the following:

. Net clr and Java VMS are both Stack-Based VMS. In other words, their Instruction sets are Stack-Based: during execution, all data is first placed in the stack before calculation. JavaVM has about 200 commands (Instruction), each of which is a 1 byte opcode, followed by an unequal number of parameters ;. net clr has over 220 commands, but some commands use the same opcode, so the number of opcode is slightly less than the number of commands. Note that the opcode length of. NET is not fixed. Most opcode lengths are 1 byte, and a few are 2 bytes.

The following is a simple C # original code:

Copy codeThe Code is as follows:
Using System;
Public class Test {
Public static void Main (String [] args ){
Int I = 1;
Int j = 2;
Int k = 3;
Int answer = I + j + k;
Console. WriteLine ("I + j + k =" + answer );
}
}


Compile the original code to obtain an EXE program. We can decompile the EXE through ILDASM. EXE (figure-0) to observe IL. I will decompile the Main () IL as follows. There are a total of 18 IL commands, some commands (such as ldstr and box) need to be followed by parameters, and some commands (such as ldc. i4.1 and add) are not followed by parameters.
Figure-0
Ldc. i4.1
Stloc.0
Ldc. i4.2
Stloc.1
Ldc. i4.3
Stloc.2
Ldloc.0
Ldloc.1
Add
Ldloc.2
Add
Stloc.3
Ldstr "I + j + k ="
Ldloc.3
Box [mscorlib] System. Int32
Call string [mscorlib] System. String: Concat (object, object)
Call void [mscorlib] System. Console: WriteLine (string)
Ret

When this program is executed, there are three key types of memory:

1. Managed Heap: This is the memory of the Dynamic configuration (Dynamic Allocation). It is automatically Managed by Garbage Collector (GC) during execution, and the entire Process shares a Managed Heap.

2. Call Stack: this memory is automatically managed by. net clr during execution. Each Thread has its own dedicated Call Stack. Each Call method adds a Record Frame to the Call Stack. After the Call is completed, the Record Frame is discarded. Generally, the Record Frame records the method Parameter, Return Address, and Local Variable ). Both Java VM and. net clr use 0, 1, 2... Number to identify different variables.

3. Evaluation Stack: this memory is automatically managed by. net clr during execution. Each Thread has its own exclusive Evaluation Stack. The stacked Virtual Machine mentioned above refers to this stack.

There is a string next to it to explain the changes in the three memories during execution. First, after entering Main () and not executing any commands, Memory limit 1 is shown as follows:

Figure 1

Then execute the First Command ldc. i4.1. This command indicates that a 4 byte constant is placed in the Evaluation Stack, and its value is 1. After this command is executed, the memory changes as follows:

Ldc. i4.1: Indicates loading a value from 1 to the stack. The syntax structure of this command is:
Ldc. typevalue: the ldc command loads a constant of the specified type to the stack.
Ldc. i4.number: the ldc command is more effective. It transmits an integer between-1 and 0 to 8 to the computing stack.

Figure 2

Then execute the second command stloc.0. This command is used to extract a value from the Evaluation Stack and put it in variable 0th (V0. The variable No. 0th here is actually the I in the original code. After this command is executed, memory changes 3:

Figure 3

The third and fifth commands are similar to the first, and the fourth and sixth commands are similar to the second. I will not describe it here to save space. Remind everyone that the variable No. 1st (V1) is actually j in the original code, and the variable No. 2nd (V2) is actually k in the source code. Figure 4 ~ 7 points are the third point after execution ~ Memory change diagram after six commands:

Figure 4

Figure 5


Figure 6


Figure 7

Then execute the seventh command ldloc.0 and eighth command ldloc.1: separately place the values of V0 (that is, I) and V1 (that is, j) to the Evaluation Stack, this is the preparation action before the addition. Figure 8 and Figure 9 show the memory changes after the seventh and eighth commands are executed:

Figure 8


Figure 9

Then execute the ninth command add. This command is used to retrieve two values (I and j) from the Evaluation Stack, and then add the results to the Evaluation Stack. After this command is executed, memory changes 10:


Figure 10

Then execute the tenth command ldloc.2. This command puts V2 (k) values in the Evaluation Stack, which is the preparation action before addition. After this command is executed, memory changes are shown in Figure 11:


Figure 11

Then execute the 11th command add. Extract two values from the Evaluation Stack, and then add the result to the Evaluation Stack. This is the value of I + j + k. After this command is executed, the memory changes by 12:


Figure 12

Then, execute the 12th command stloc.3. Extract A value from the Evaluation Stack and put it in variable 3rd (V3. The variable No. 3rd here is actually the answer in the original code. After this command is executed, memory changes 13:


Figure 13

Then run the 13th command ldstr "I + j + k = ". This command indicates that the Reference "I + j + k =" is put into the Evaluation Stack. After this command is executed, memory changes 14:


Figure 14

Then run the 14th command ldloc.3. Add the value of V3 to the Evaluation Stack. After this command is executed, the memory changes as shown in Figure 15:


Figure 15

Then run the 15th-channel command box [mscorlib] System. Int32. From this point, we can see that,Int to string is actually boxed, so there will be performance loss, you can reduce the number of boxed operations in future encoding to improve performance. This command is used to retrieve a Value from the Evaluation Stack and convert the Value Type package into Reference Type. After this command is executed, the memory changes as shown in 16:


Figure 16

Then, run the 16th command call string [mscorlib] System. String: Concat (object, object ). This command indicates that two values are taken from the Evaluation Stack, both of which are Reference Type. The following values are treated as the first parameter, and the preceding values are treated as the second parameter, call mscorlib. the System. string. concat () method: String Concatenation (String Concatenation) is used to concatenate a new String in Managed Heap and put its Reference in Evaluation Stack. It is worth noting that System. String. Concat () is a static method, so the command used here is call rather than callvirt (call virtual ). After this command is executed, memory changes 17:


Figure 17

Note:At this time, Int32 (6) and String ("I + j + k =") in Managed Heap are no longer tested by developers, so they become garbage and wait for GC to be recycled.

Then run the 17th command call void [mscorlib] System. Console: WriteLine (string ). This command is used to retrieve a value from the Evaluation Stack, which is a Reference Type parameter and calls mscorlib. the System. console. writeLine () method to display this string on the Console window. System. Console. WriteLine () is also static method. After this command is executed, memory changes are shown in 18:

Figure 18

Then execute the 18th command ret. This command ends the call (that is, the Main call ). At this time, the remaining resources in the Evaluation Stack will be checked. The Main () declares that no outgoing value (void) is required, so the Evaluation Stack must be empty, this example matches this situation, so you can end the call at this time. While the Main call ends, and the program ends. After this command is executed (and before the program end), memory changes 19 are shown:

Figure 19

Through this example, you should be able to have a basic understanding of IL. Readers interested in IL should read Inside Microsoft. net il interpreter Er (Microsoft Press) by Serge Lidin ). I think: familiar with the role of each command in IL is a necessary knowledge for. NET programmers .. NET programmers may not use IL Assembly to write programs, but at least they must understand the IL combination code decompiled by ILDASM.

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.