NET in Il understanding

Source: Internet
Author: User


The. NET CLR and Java VMs are stacked virtual machines (stack-based VMs), that is, their instruction set (instruction set) is a way of stacking operations: the data at the time of execution is placed in the stack first, and then the operation. JAVAVM has about 200 instructions (instruction), each instruction is 1 byte opcode (opcode), followed by unequal number of parameters; the. NET CLR has over 220 instructions, but some instructions use the same opcode, so the number of opcode Slightly less than the instruction number. It is important to note that the opcode length of. NET is not fixed, most of the opcode is 1 byte, and the smaller part is 2 byte.

here is a simple C # source 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);
}
}


After compiling this source code, you can get an EXE program. We can pass the ILDASM. EXE (figure 0) to decompile the EXE to observe IL. I will have the Il anti-compile bar of Main () as follows, there are 18 il instructions, and some instructions (such as ldstr and box) need to be followed by parameters, and some instructions (such as ldc.i4.1 and add) do not need to take the parameters after.


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 executes, there are three key memory types, each of which is:

1. Managed Heap: This is the memory of dynamic Allocation, which is managed by Garbage Collector (GC) at execution time, and a Managed heap is shared throughout the process.

2. Call stack: This is a memory that is managed by the. NET CLR at execution time, and each Thread has its own call stack. Each call to a method will cause a record frame to be created on the call Stack; In general, the method parameter (Parameter), return address, and regional variables (local Variable) are recorded inside the record Frame. Both the Java VM and the. NET CLR are using 0, 1, 2 ... The number of ways to distinguish between variables.

3. Evaluation stack: This is a memory that is managed by the. NET CLR at execution time, and each Thread has its own Evaluation Stack. The stacked virtual machine in front of you refers to this stack.

There is a string behind, which is used to explain the changes in the three memory types at the time of execution. First, after entering Main (), no instructions have been executed, the memory is shown in 1:

Figure 1

The first instruction ldc.i4.1 is then executed. This directive means: place a constant of 4 byte in the Evaluation Stack with a value of 1. After executing this instruction, the memory changes in 2 are as follows:

ldc.i4.1: Indicates that a value of 1 is loaded into the stack, and the syntax structure of the directive is:
The LDC.TYPEVALUE:LDC instruction loads a constant of the specified type to the stack.
The LDC.I4.NUMBER:LDC command is more efficient. It transmits an integer value of 1 and 0 to 8 integers to the evaluation stack

Figure 2

The second instruction stloc.0 is then executed. This directive means: take a value from the Evaluation Stack and place it in the No. 0 variable (V0). The NO. 0 variable here is actually the I in the original code. After executing this instruction, the memory changes in 3 are as follows:

Figure 3

The third and fifth directives are identical to the first instruction, and the fourth instruction and the sixth instruction are identical to the second instruction. In order to save space, I do not describe this in one by one baggage. To remind you that the 1th variable (V1) is actually the original code of J, and the 2nd variable (V2) is actually the source of K. Figure 4~7 is the memory change graph after the execution of the third to sixth instruction:

Figure 4

Figure 5


Figure 6


Figure 7

The seventh directive ldloc.0 and the Eighth command Ldloc.1 are then executed: Divide the values of V0 (i.e. i) and V1 (that is, j) into the Evaluation Stack, which is the preparation before adding. Figure 8 and Figure 9 are performed after the completion of the seventh and eighth instructions, the memory of the change diagram:

Figure 8


Figure 9

The nineth command add is then executed. This directive means: take two values (i.e. I and J) from the Evaluation stack and add the results back to the Evaluation stack. After executing this instruction, the memory changes in 10 are as follows:


Figure 10

The tenth directive LDLOC.2 is then executed. This directive means: divide the value of V2 (that is, K) into the Evaluation Stack, which is the preparation before adding. After executing this instruction, the memory changes in 11 are as follows:


Figure 11

The 11th command add is then executed. Remove the two values from the Evaluation stack and add the results back to the Evaluation stack, which is the value of i+j+k. After executing this instruction, the memory changes in 12 are as follows:


Figure 12

The 12th directive STLOC.3 is then executed. Remove a value from the Evaluation Stack and place it in the 3rd variable (V3). The 3rd variable here is actually the answer in the original code. After executing this instruction, the memory changes in 13 are as follows:


Figure 13

The 13th Directive ldstr "i+j+k=" will then be executed. This directive means: put the "i+j+k=" Reference into the Evaluation Stack. After executing this instruction, the memory changes in 14 are as follows:


Figure 14

The 14th directive Ldloc.3 is then executed. Put the V3 value into the Evaluation Stack. After executing this instruction, the memory changes in 15 are as follows:


Figure 15

followed by the 15th command box [Mscorlib]system.int32, from where it can be seen thatint to string is actually a boxing operation, so there will be a performance loss, you can reduce the boxing operation in the subsequent encoding to improve performance . This directive means: take a value from the Evaluation Stack, and wrap this value type (box) into the Reference type. After executing this instruction, the memory changes in 16 are as follows:


Figure 16

Follow the 16th instruction call string [mscorlib] System.string::concat (object, Object). This directive means: Take two values from the Evaluation Stack, both of which are Reference Type, the value below as the first parameter, the value above as the second parameter, the call mscorlib.dll provides the System.String.Concat () method puts the two parameters into a string join (string concatenation), placing the new string in the Managed Heap, Reference it into Evaluation Stack. It is important to note that because System.String.Concat () is a static method, the instructions used here are call, not callvirt (called virtual). After executing this instruction, the memory changes in 17 are as follows:


Figure 17

Please note: Int32 (6) and String ("i+j+k=") in the Managed Heap are no longer involved, so they become garbage and wait for GC to be recycled.

The 17th command call void [mscorlib] System.console::writeline (string) is then executed. This directive means: take a value from the Evaluation Stack, this value is Reference Type, take this value as a parameter, call mscorlib.dll the System.Console.WriteLine () method provided by To display this string on the Console window. System.Console.WriteLine () is also a static method. After executing this instruction, the memory changes in 18 are as follows:

Figure 18

The 18th instruction RET is then executed. This command means: End the call (that is, the call to Main). This will check the remainder of the Evaluation stack, because the Main () declaration does not require an outgoing value (void), so the Evaluation stack must be empty, this example conforms to this situation, so you can Li this call. And the end of the call of Main, the program is also the end of the bundle. After executing this instruction (and before the end of the program), the memory changes are shown in 19:

Figure 19

Through this example, the reader should be able to have the most basic understanding of IL. Readers interested in IL should read "Inside Microsoft. NET IL Assembler" (Microsoft Press Publishing) Serge Lidin. I think: knowing the role of IL per instruction is an essential knowledge of. NET programmers.. NET programmers can not write programs with Il Assembly, but at least you have to understand the ILDASM anti-compilation Il code.

NET in Il understanding (GO)

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.