A preface
Il series after the first write to get expert guidance, timely corrected the error in the article, also made me write this article more cautious, I know the relevant knowledge points, but also more detailed. Personally feel that since the article is written out, it must be guaranteed relatively high quality, and the correct rate. Thanks @ Ice Lin Light Wu's guidance
You didn't read the first article? Click here to see the first read the IL code is so simple (a)
Il instruction Daquan: il instruction detailed
Il Anti-compilation tool: ILDasm
Knowledge Point review:
Managed Heap (Managed heap): values that are used to hold reference types
Evaluation statck (calculation stack): temporary storage value type data, reference type address of the stack (this is the stack, so follow the operation characteristics of the stack, advanced post-out)
Call stack: Where the record frame is used to hold the. Locals init (int32 v_0) directive parameter values such as: V_0 (the record frame is a local variable table, so the filo principle is not observed)
Two instructions (Basic introduction)
2.1 Knowledge Point Introduction
In the first article, I only wrote the IL instruction of the value type in detail, this one will mainly refer to the type of the main, this one will have boxing operations, so first write about how the boxing operation in memory operation
Boxing operations: 1 memory allocations, allocating memory space in the managed heap, 2 copying fields of value types into newly allocated memory, 3 returning object addresses from the managed heap to new objects
Operating procedures such as
C # code
1 /* 2 Author:zery-zhang 3 BLOGADDRESS:HTTP://WWW.CNBLOGS.COM/ZERY/4 * /5 static void Main ( String[] args) 6 {7 string name = "Zery"; 8 int age = 9 Console.WriteLine ToString () + name),//The operation of ToString ( Console.WriteLine) (age+name);//No ToString operation
Il code
1/* 2 Author:zery-zhang 3 BLOGADDRESS:HTTP://WWW.CNBLOGS.COM/ZERY/4 */5. Method Private Hi Debysig static void Main (string[] args) cil managed 6 {7. entrypoint 8//code size (0x30) 9 10//The following code is complete Operations for initializing variables in C # code 11 12//The maximum number of data items that can be accommodated by the compute stack (Evaluation stack). Maxstack 2 14//define and initialize the parameters and deposit the local variable table (Record F Rame) Locals init (string v_0,int32 v_1) il_0000:nop17//string into the calculation stack (Evaluation stack) il_0001:ldstr "Zery" 19//Eject ("Zery") character from the calculation stack and assign to the element in the No. 0 position in the local variable table v_020 il_0006:stloc.0 21//Put the integer 22 into the calculation stack. il_0007: LDC.I4.S 22 23//The integer 22 is ejected and assigned to the element in the local variable table 1th position v_124 il_0009:stloc.1 25 26//The following code completes the output operation in C # 27 28 Take out the value of the v_1 element in the local variable table (copy) and press it into the calculation stack il_000a:ldloca.s v_1 30//Eject the value just pressed ("22") call the ToString method to the string type and deposit the reference into the evaluation stack 31 Il_000c:call instance string [Mscorlib]system.int32::tostring () 32//Take out the value of the No. 0 position element (V_0) in the local variable table ("Zery") into the calculation stack (this There are two values in the calculation stack, pointing to the reference address of "22" in the push pipe heap andString "Zery") il_0011:ldloc.0 34//pop-up calculation stack two values call the Concat method of string to put character concatenation into the managed heap (Managed heap) and return the address into the calculation stack 35 Il_0012:call string [Mscorlib]system.string::concat (string,string) 36//Call the output method, the value in the evaluation stack after the call to the output method (the address that points to the managed heap character) is recycled 。 PNs il_0017:call void [Mscorlib]system.console::writeline (string) 38 39//Not ToString operation Max IL_001C:NOP41/ /Take the value of element v_1 of the 1th position in the local variable table ("22") into the calculation stack Il_001d:ldloc.1 43//Put the integer that was just pressed in 22 boxed and return the address to the managed heap in the calculation stack, il_001e: box [Mscorlib]system.int32 45//Take the value of the F element v_0 of the No. 0 position in the local variable table ("Zery") and press it into the calculation stack il_0023:ldloc.0 47 The two values in the pop-up stack call the Concat method of the string to make a concatenation of the characters into the managed heap (Managed heap) and return the address pressed into the calculation stack il_0024:call string [Mscorlib]system.stri Ng::concat (Object,object) 49//Call output method Il_0029:call void [Mscorlib]system.console::writeline (String) Wuyi il_002e:nop52//Mark return Il_002f:ret//end of method Program::main
2.2 IL instruction detailed
. maxstack: The maximum number of data items that the compute stack (Evaluation stack) can hold
. Locals init (int32 v_0,int32 v_1,int32 v_2): Define variables and deposit in the record frame in the call stack
NOP: No Operation no action, we don't have to worry about it,
ldstr.: Load string to compress strings into the evaluation stack
stloc.: Assigns values from the evaluation stack to the record frame in the call stack
ldloc.: Remove the value of the specified position in the record frame in the call stack (copy) into the Evaluation stack above two instructions for each other operation stloc assignment, ldloc value
Call: Invokes the specified method
Box: Performing a boxing operation
RET: The return tag returns
Three instructions (in-depth introduction)
If you don't understand the comments in the code, take a look at the diagram below, and if you draw every step, the project is too big, so I'll combine the simple steps into a picture and annotate it.
First draw the initialization of the code in detail diagram Note: In order to reduce the picture so the stack of pop-up and press-in operation is omitted, all only draw the results
Il_0001:ldstr "Zery"
il_0006:stloc.0
Il_0007:ldc.i4.s 22
Il_0009:stloc.1
Because strings are reference types, they are stored in the managed heap, and only the addresses of the character references are saved in the stack, and you can see that the strings in the diagram are managed, and only the references are saved in the calculation stack
IL_000A:LDLOCA.S v_1
Il_000c:call instance string [Mscorlib]system.int32::tostring ()
il_0011:ldloc.0
Il_0012:call string [Mscorlib]system.string::concat (string,string)
Il_0017:call void [Mscorlib]system.console::writeline (String)
Il_001d:ldloc.1
Il_001e:box [Mscorlib]system.int32
il_0023:ldloc.0
Il_0024:call string [Mscorlib]system.string::concat (object,
Object
Il_0029:call void [Mscorlib]system.console::writeline (String)
The boxed process diagram is given above and only the results are drawn here.
Iv. Summary
1 in two articles the value type and the reference type in memory different locations and different operations in detail, I think it is necessary, because all the data types are composed of these two types, the two types of operations to understand the
Look at the other IL instructions is through the nature of the phenomenon.
2 about drawing, I think that drawing is the programmer must learn the knowledge, the Ox x Programmer draw out, System architecture diagram, System design diagram and so on are very structure very clear. There are too many problems with my drawing skills, but it's just a simple picture that I can't express my mind perfectly.
Citation to: http://www.cnblogs.com/zery/p/3376973.html
It's so easy to read the IL Code (ii)