Study Notes on inside Microsoft il explorer 2: Make the Il code shorter

Source: Internet
Author: User
Tags mscorlib
The example in study Note 1 can be changed to the following: The function remains unchanged, but the resources occupied during running are smaller. I am in all and Note 1 Code Note the location of the change as follows: 1 // ----------- Program Header
2 . Assembly Extern Mscorlib {}
3 . Assembly oddoreven {}
4 . Module oddoreven.exe
5 // ----------- Class declaration
6 . Namespace Odd. or {
7 . Class   Public Auto ANSI even extends [mscorlib] system. Object {
8 // ----------- Field Declaration
9 . Field Public   Static Int32 Val
10 // ----------- Method declaration
11 . Method Public   Static   Void Check () cel managed {
12 . Entrypoint
13 . Locals Init (int32 retval)
14 Askfornumber:
15 Ldstr " Enter a number "
16 Call Void [Mscorlib] system. Console: writeline ( String )
17 Call String [Mscorlib] system. Console: Readline ()
18 Ldstr " % D "   // No special declaration is required for all string constants. After the Il assembler sees such a string, it will automatically add it to the metadata.
19 Ldsflda int32 odd. Or. Even: Val
20 Call vararg int32 sscanf ( String , String , Int32 * )
21 Stloc. 0   // Java virtual machines and. Net CLR both use numbers such as 0, 1, 2, and 3 to represent local variables. "0" points to a unique local variable retval.
22 Ldloc. 0   // Apply the value of the first local variable to the stack.
23 Brfalse. s Error // The brfalse. s command is a simplified command of brfalse. After the command is compiled into an operation code, brfalse accepts a 4-byte parameter,
24 // Brfalse. s only accepts one single-byte parameter, which means that only {-128 bytes ~~ Jump within the range of + 128 bytes} (relative to the current position.
25 Lds1_int32 odd. Or. Even: Val
26 LDC. i4. 1 // Press a single-byte constant to the top of the stack. The value is 1 (LDC. I4 1 is also pushed to the top of the stack by 1, but it must use 4 bytes)
27 And
28 Brfalse. s itseven
29 Ldstr " Odd! "
30 BR. s printandreturn
31 Itseven:
32 Ldstr " Even! "
33 BR. s printandreturn
34 Error:
35 Ldstr " How rude! "
36 Printandreturn:
37 Call Void [Mscorlib] system. Console: writeline ( String )
38 Ldloc. 0
39 Brtrue. s askfornumber
40 RET
41 }   // End of Method
42 }   // End of class
43 }   // End of namespace
44 // ----------- Calling unmanaged code
45 . Method Public   Static Pinvokeimpl ( " Msvcrt. dll " Cdecl)
46 Vararg int32 sscanf ( String , String ) Pencil managed {}
47

In addition, I did not understand the meaning of the ldloc.0 command mentioned above. Actually, executeProgramIn the process, there are three main types of memory usage involved. Apart from the managed heap, each thread has its own exclusive two stacks: The computing stack (Evaluation stack) and call stack ). These two stacks are often mentioned and both belong to CLR threads, so sometimes they will be mixed. In fact, their functions are completely different. The call stack is the most traditional stack used to record the call sequence of methods. Each thread will get 1 MB of call stack space, and each call to a new method will press the stack, all input parameters and temporary variables are pushed into the stack, and are completely released after method execution. "0" in ldloc.0 refers to the first temporary variable on the current call stack. The computing stack can be regarded as a register of the CLR Virtual Machine to some extent (this is simply because it is a storage unit that directly interacts with the computing functions on the virtual machine ), we often say that CLR virtual machines are completely based on "Stacks", which refer to computing stacks. All operations on virtual machines must be performed from the computing stack parameters, and the results will be pushed to the top of the computing stack. The unit of the computing stack is neither a word nor a byte, but a slot. The slot size varies depending on the Data Types installed in the slot. After the data is obtained in the slot at the top of the CLR stack, the data type is checked before the operation. If the data type cannot be converted to the expected type, an exception is thrown. Cai xueyong has an article on msdn, and a texture in it looks very clear. Let's borrow it here:

Reference: msil instruction table

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.