Major internal expert speaker:. Net intermediate Speaker (IL)

Source: Internet
Author: User
Tags mscorlib

Author: Cai xuanyuan

September 2003

. Net Clr and Java VMS are both stack-based VMS, that is, their instruction sets (instruction set) these are all calculation methods by using heap operation: the data in the initial row is first placed in the heap operation, and then run the calculation. Java VM has about 200 commands (Instruction), each of which is a 1 byte opcode (operation logic), followed by unequal numbers ;. net CLR has over 220 commands, but some commands use the same opcode, so the opcode number is slightly less than the command number. Note that the opcode length of. NET is not fixed. Most opcode lengths are 1 byte, and a few are 2 bytes.

BenArticleTaking a real-world example, you can understand the operating principle of the heap structured Vm and have a basic understanding of. Net il (intermediate language.

Below is a simple C # original example:

 
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 the original file is created, an EXE example can be obtained. We can reverse-organize the EXE through ildasm. EXE to detect Il. The main () Il reverse grouping condition is listed as follows. There are a total of 18 il commands, and some commands (such as ldstr and box) need to be connected to the numbers, some commands (such as LDC. i4.1 and add.

 
LDC. i4.1stloc. 0ldc. i4.2stloc. 1ldc. i4.3stloc. 2ldloc. 0ldloc. 1addldloc. 2addstloc. 3 ldstr "I + J + k =" ldloc.3box [mscorlib] system. int32call string [mscorlib] system. string: Concat (object, object) Call void [mscorlib] system. console: writeline (string) RET

When this program is running, there are three types of memory related to the program:

    • Managed heap:This is a dynamic configuration (Dynamic Allocation) record, which is automatically managed by garbage collector (GC) during row creation. The entire process shares a managed heap.
    • Call Stack:This is a notebook that is automatically managed by. Net CLR during runtime. Each thread has its own dedicated call stack. Each call to a method adds a record frame to the call stack. After the call is completed, the record frame is reset. Generally, the record frame contains the method parameter number, return address, and local variable ). Both Java Vm and. Net CLR use 0, 1, 2... Allows you to identify the region changes.
    • Evaluation Stack:This is a notebook that is automatically managed by. Net CLR during runtime. Each thread has its own evaluation stack. The heap camera above refers to the heap host.

There is a string behind the scenes to illustrate the changes of these three types of memory during the operation. First, after entering main (), before you begin to execute any commands, the memory usage of the memory is shown in Example 1:


Limit 1

The first command LDC. i4.1 is required. 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 changes of the memory are shown in Example 2:


Limit 2

The second line of command stloc.0 is to be merged. This command is used to extract a value from the evaluation stack and put it in the 0th degrees change (v0. In this case, the 0th changes are actually the I in the original phase. After this command is executed, the changes to the memory are shown in Example 3:


Limit 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 for the sake of saving space. We would like to remind you that the 1st metric change (V1) is actually J in the original metric, and the 2nd metric change (V2) is actually K in the source metric. Listen 4 ~ 7 points are the third day after the complete row ~ After six instructions, let's record the changes of the software:


Limit 4


Limit 5


Limit 6


Lifecycle 7

Next, run 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 dynamic operation before the addition. When the seventh and eighth commands are completed, let's record the changes of our system:


Listen 8


Listen 9

Next, add the ninth command line. This command is used to extract two values (I and j) from the evaluation stack and then return the result to the evaluation stack. After this command is executed, the changes to the memory are shown in Example 10:


Limit 10

Connect to the worker

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.