In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is IL in NET". In daily operation, I believe that many people have doubts about what is IL in NET. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what is IL in NET?" Next, please follow the editor to study!
I have been in contact with NET for about a year, and the internal implementation of NET has been very attractive to me. Personally, I think: if you can understand and be proficient in these bottom implementations, it will be of great help to write your own code in the future. all right, don't talk too much nonsense, please see below:
Both the .NET CLR and Java VM are stackable virtual machines (Stack-Based VM), that is, their instruction sets (Instruction Set) are stacked: the data that is executed is placed on the stack before the operation. JavaVM has about 200 instructions (Instruction), each of which is a 1 byte opcode (opcode), followed by a different number of arguments; .NET CLR has more than 220 instructions, but some instructions use the same opcode, so the number of opcode is slightly less than the number of instructions. Note that the opcode length of .NET is not fixed. Most opcode lengths are 1 byte and a few are 2 byte.
Here is a simple C # source code:
The copy code is as follows:
Using System
Public class Test {
Public static void Main (String [] args) {
Int iTunes 1
Int jung2
Int Kitchen 3
Int answer = i+j+k
Console.WriteLine ("iTunjroomk =" + answer)
}
}
After compiling this source code, you can get an EXE program. We can use ILDASM.EXE (figure-0) to decompile EXE to observe IL. I have listed the IL decompilation of Main () as follows. There are 18 IL instructions, 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 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 "iObjecjroomk ="
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 runs, there are three key types of memory, namely:
1. Managed Heap: this is the memory of dynamic configuration (Dynamic Allocation), which is automatically managed by Garbage Collector (GC) at execution time, and the whole Process shares one Managed Heap.
2. Call Stack: this is the memory automatically managed by the .NET CLR at execution time, and each Thread has its own Call Stack. Each time a method is called, there is an extra Record Frame; on the Call Stack, and the Record Frame is discarded. Generally speaking, the method parameter (Parameter), return address (Return Address), and regional variable (Local Variable) are recorded in the Record Frame. Both Java VM and .NET CLR use 0,1,2... The way of numbering to distinguish between variables.
3. Evaluation Stack: this is the memory automatically managed by the .NET CLR at execution time, and each Thread has its own Evaluation Stack. The stackable virtual machine mentioned above refers to this stack.
There is a series of diagrams at the back to explain the changes in these three kinds of memory during execution. First, after entering Main (), before any instructions are executed, the state of memory is shown in figure 1:
Figure 1
Next, execute the first instruction, ldc.i4.1. This instruction means to place a constant of 4 byte in Evaluation Stack with a value of 1. After executing this instruction, the change in memory is shown in figure 2:
Ldc.i4.1: indicates that a value of 1 is loaded into the stack. The syntax structure of this instruction is:
The ldc.typevalue:ldc instruction loads a constant of the specified type into stack.
The ldc.i4.number:ldc instruction is more efficient. It passes an integer value of-1 and an integer between 0 and 8 to the evaluation stack
Figure 2
The second instruction, stloc.0, is then executed. This instruction means: take a value from Evaluation Stack and put it in variable 0 (V0). The No. 0 variable here is actually the I in the original code. After executing this instruction, the change in memory is shown in figure 3:
Figure 3
The latter third instruction and the fifth instruction are similar to the first instruction, and the fourth instruction and the sixth instruction are similar to the second instruction. In order to save space, I will not repeat them here one by one. Remind you that variable 1 (V1) is actually j in the source code, and variable 2 (V2) is actually k in the source code. Figure 4: 7 shows the changes in memory after the execution of the third to sixth instructions:
Figure 4
Figure 5
Figure 6
Figure 7
Then execute the seventh instruction ldloc.0 and the eighth instruction ldloc.1: put the values of V0 (that is, I) and V1 (that is, j) into Evaluation Stack, respectively, which is a preparation action before addition. Figures 8 and 9 show the changes in memory after the execution of instructions 7 and 8, respectively.
Figure 8
Figure 9
Then execute the ninth instruction add. What this instruction means is to take the individual values (that is, I and j) from Evaluation Stack, add them together and put the results back into Evaluation Stack. After executing this instruction, the change in memory is shown in figure 10:
Figure 10
Next, execute the tenth instruction ldloc.2. This instruction means: separately put the value of V2 (that is, k) to Evaluation Stack, which is the preparation action before addition. After executing this instruction, the change in memory is shown in figure 11:
Figure 11
Then execute the eleventh instruction add. Take some values from Evaluation Stack, add them up and put the results back into Evaluation Stack, which is the value of i+j+k. After executing this instruction, the change in memory is shown in figure 12:
Figure 12
Next, execute the twelfth instruction stloc.3. Take a value from Evaluation Stack and put it in variable 3 (V3). The third variable here is actually the answer in the source code. After executing this instruction, the change in memory is shown in figure 13:
Figure 13
Next, execute the thirteenth instruction ldstr "iObjecjroomk =". The meaning of this instruction is to put the Reference of "iObjecjSecretk =" into the Evaluation Stack. After executing this instruction, the change in memory is shown in figure 14:
Figure 14
Next, execute the fourteenth instruction ldloc.3. Put the value of V3 into Evaluation Stack. After executing this instruction, the change in memory is shown in figure 15:
Figure 15
Then we need to execute the fifteenth instruction box [mscorlib] System.Int32, from which we can see that int to string actually carry out the boxing operation, so there will be performance loss, so we can reduce the boxing operation in the future coding to improve performance. This instruction means: take a value from the Evaluation Stack and change the Value Type package (box) to Reference Type. After executing this instruction, the change in memory is shown in figure 16:
Figure 16
Then execute the sixteenth instruction call string [mscorlib] System.String::Concat (object, object). This instruction means: take the instance value from the Evaluation Stack, these two values are both Reference Type, the following value is regarded as the first parameter, and the above value is regarded as the second parameter, call the System.String.Concat () method provided by mscorlib.dll to String Concatenation the two parameters, put the new string in Managed Heap, and put its Reference into Evaluation Stack. It is worth noting that because System.String.Concat () is static method, the instruction used here is call, not callvirt (call virtual). After executing this instruction, the change in memory is shown in figure 17:
Figure 17
Please note: at this time, Int32 (6) and String in Managed Heap are no longer referenced, so they become garbage, waiting for GC to be recycled.
Then execute the seventeenth instruction call void [mscorlib] System.Console::WriteLine (string). This instruction means: take a value from Evaluation Stack, this value is Reference Type, and call the System.Console.WriteLine () method provided by mscorlib.dll as a parameter to display the string on the Console window. System.Console.WriteLine () is also static method. After executing this instruction, the change in memory is shown in figure 18:
Figure 18
Then the eighteenth instruction ret will be executed. This command means: end this call (that is, the call from Main). At this time, the remaining data in the Evaluation Stack will be checked. Since Main () declares that the outgoing value (void) is not required, the Evaluation Stack must be empty. This example conforms to this situation, so the call can be terminated smoothly at this time. As soon as the call to Main ends, so does the program. After executing this instruction (and before the end of the program), the change in memory is shown in figure 19:
Figure 19
Through this example, readers should be able to have the most basic understanding of IL. Readers interested in IL should read Serge Lidin's Inside Microsoft .NET IL Assembler (published by Microsoft Press). I think it is a necessary knowledge for .NET programmers to be familiar with the function of each instruction of IL. Net programmers may not be able to write programs in IL Assembly, but at least understand the IL combination code decompiled by ILDASM.
At this point, the study on "what is IL in NET" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.