Understanding IL code (2) and understanding il code

Source: Internet
Author: User
Tags mscorlib

Understanding IL code (2) and understanding il code

The most basic IL Code mentioned in the previous article should be relatively easy to understand. Therefore, with the foundation of the previous article, this article will be explained in depth.

First, I must introduce some important concepts:

Evaluation Stack: This is caused. net clr automatically manages the memory during execution. Every thread has its own evaluation stack. That is to say, it is a thread stack used to store temporary variables (which should be understandable ). Value Type storage data, reference type storage address.

Call Stack: This is also caused. net clr automatically manages the memory during execution. Each thread has its own call stack. Each call to the method generates a stack frame ), when the method ends, the stack frame will be discarded.

You must be sure to know how the stacks are. Where is the stack going? Don't worry. Let's say:

Managed Heap: this memory is dynamically configured and is automatically Managed when Garbage Collector (GC) is executed. The whole program shares one. I understand this as a managed heap to store reference type values.

This article mainly describes the IL code based on the reference type.

As we all know, The binning operation is used to change the value type to the reference type. So let's talk about the packing process here:

(1) memory allocation: allocate a memory space in the Managed Heap;

(2) copy a value type field to the allocated memory;

(3) return the object address in the managed heap to the new object, Over.

 

The above are some basic knowledge points. Now we will mainly explain the IL code:

The regular code is as follows:

I use Reflector to view the IL code:

 

This is the IL code. After the previous introduction, I think everyone should understand it here. I will explain this IL code below.

. Method private hidebysig static voidMain(String [] args) cilmanaged {. entrypoint// Entry. Maxstack 2// The maximum number of data items that the evaluation stack can accommodate.. Locals init ([0] stringStr, [1] int32Num)// Define and initialize parameters and store them in the Call Stack.L_0000: nop// No OperationL_0001: ldstr "Helius"// Press the string "Helius" into the evaluation stack.L_0006: stloc.0// Pop up the string from the evaluation stack and assign it to The 0th variables in the local variable table.L_0007: ldc. i4.s 0x1b// Resolution: the value of the int type is different in size and the IL code is also different. For example, int I =-1; IL code is ldc. i4.M1; when the I value is greater than or equal to 9, the IL code is ldc. i4.s (hexadecimal representation of I). When I is less than or equal to-2, the IL code is represented as ldc. i4.s (I ).L_0009: stloc.1// Pop up the value from the evaluation stack and assign it to the 1st variables in the local variable table.L_000a: ldloca. s num// Extract the num value "27" from the local variable table and press it into the evaluation stack.L_000c: call instance string [mscorlib] System. Int32: ToString ()// Retrieve the value "27" from the evaluation stack, call the ToString method to convert the value to the string type, and store the reference in the evaluation stack.L_0011: ldloc.0// Extract the 0th position element value "Helius" from the local variable table and press it into the evaluation stack. (at this time, there are two values in the evaluation stack, the reference addresses of the strings "Helius" and "27 ).L_0012: call string [mscorlib] System. String: Concat (string, string)// Call the Concat method of the String class to splice the String and store it in the managed heap, and return the reference address to the evaluation stack.L_0017: call void [mscorlib] System. Console: WriteLine (string)// Call the output method. After calling the output method, evaluate the value in the stack (pointing to the character address in the managed heap) and recycle it.L_001c: nop
------------------------------------------ Split line MAID: ldloc.1// Obtain the 1st location parameter num value in the local variable table and store it in the evaluation stack.L_001e: box int32// Pack the num value 27 and return the address in the managed heap to the evaluation stack.L_0023: ldloc.0// Remove the 0th parameters from the local variable table and press them into the evaluation stack.L_0024: call string [mscorlib] System. String: Concat (object, object)// Pop up two values in the evaluation stack, and call the Concat method of String to concatenate the characters, store them in the managed stack, and return the reference address to the evaluation stack.L_0029: call void [mscorlib] System. Console: WriteLine (string)// Call the output method.L_002e: nop L_002f: call valuetype [mscorlib] System. ConsoleKeyInfo [mscorlib] System. Console: ReadKey ()// Call the ReadKey MethodL_0034: pop// Clear the content of the evaluation StackL_0035: ret// Return flag returned}

The above example may be explained in an image. (It is very important for programmers to learn to draw pictures. I remember that during the interview, the interviewer asked me to draw the structural diagram of the project ):
Figure 1



Figure 2




Figure 3

The preceding figure shows the call method and packing process. The reference types are stored in the hosting heap, while the stack only stores the address of the reference type. This is worth special attention.

In the next article, I will write methods, delegate and class IL code parsing.

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.