[You must know. Net] 13th back: Get to know Il from Hello, world

Source: Internet
Author: User
Tags mscorlib sha1 hash

Released on: 2007.7.22 by anytao

2007 anytao.com: original works. Please refer to the author and source for the post.

This article introduces the following:

    • IlCodeAnalysis Method
    • Hello, world history
    • . Net learning methodology

 

1. Introduction

In 1988, Brian W. kernighan and Dennis M. Ritchie co-authored the classic masterpiece "the C programming language" in the software history. I recommend allProgramPeople have the opportunity to review this historical classic. Since then, the hello and world examples have served as the opening code for almost all practical programming books. They have continued so far. In addition to expressing respect for giants and history, this article also uses hello, the world example serves as the starting point for us to start our step-by-step understanding of the Il language.

2. Starting from Hello, world

First of all, of course, it is to show our Hello, world code and start a useful sharing.

Using System;
Using System. Data;

Public ClassHelloworld
{
Public Static VoidMain ()
{
Console. writeline ("Hello, world.");
}
}

This code executes the simplest process and says hello to the unfamiliar world. What is the truth behind the advanced language? Next we will start to analyze the Il code based on the above example.

3. Il experience center

The Decompilation tool of the compiled Executable File helloworld.exeapplies ildasm.exe to restore helloworld to text msil encoding. As for its working principle, we hope to follow up in the SeriesArticleTo explain, we can view it:

 

 

It can be seen that the compiled il structure contains the manifest and helloworld classes. manifest is an additional information list, which mainly contains some attributes of the Assembly, such as the Assembly name, version number, and hash.Algorithm, Assembly module, and reference items for the externally referenced assembly. The helloworld class is the main character we will introduce below.

3.1 manifest list Analysis

Open the manifest list and we can see

From this section of IL code, our analysis is as follows:

    • The. Assembly command is used to define the compilation target or load an external library. In the Il list, we can see that. Assembly extern mscorlib indicates that the external core library mscorlib is loaded, while. Assembly helloworld indicates the defined compilation target. It is worth noting that ,. assembly will only display the list of actually applied assemblies in the program. For the assembly that is added to the using reference, if it is not referenced in the program, the compiler will ignore the multi-loaded assemblies, such as system. data will be ignored, which effectively avoids code expansion caused by excessive loading.
    • We know that the mscorlib. dll Assembly defines the core data type of the managed code dependency, which is a required add-on. For example. the ctor command indicates the constructor. From the code, we know that no displayed constructor is provided for the helloword class, so we can be sure that it inherits from the base class system. object, and the system. objects are included in the mscorlib assembly.
    • The referenced version (. ver); the actual public key of the application (. publickeytoken). The public key token is the reverse order (as shown in) of the sha1 hash code, which is used to uniquely identify an assembly. It also includes other information, such as language and culture.

 

    • The helloworld Assembly includes. hash Algorithm command, indicating the hash algorithm used to implement security. The default value is 0x00008004, indicating the sha1 algorithm ;. ver indicates the version number of the helloworld assembly;
    • An assembly is composed of modules. module is the assembly instruction, indicating the metadata of the defined module to specify the current module.
    • Other Commands include: imagebase is the image base address ;. file alignment is the object alignment value ;. subsystem is the connection system type, and 0x0003 indicates running from the console ;. corflags is used to set the Runtime Library header file flag. The default value is 1. These commands are not the focus of our research. For details, refer to the relevant msdn information.

3.2 helloworld Analysis

The first is the helloworld class, the code is:

. Class   Public Auto ANSI beforefieldinit helloworld
Extends [mscorlib] system. Object
{
} // End of class helloworld
    • . Class indicates that helloworld is a public class, which inherits from the system. Object Class of the external Assembly mscorlib.
    • Public is the access control permission, which is easy to understand.
    • Auto indicates that the memory layout during program loading is determined by CLR, rather than the program itself.
    • ANSI properties are designed to achieve seamless conversion between unmanaged and managed code. Unmanaged code refers to the code that is not running on the CLR Runtime Library, such as the original C and C ++ code.
    • The beforefieldinit attribute provides an additional information for helloworld to mark that the runtime can execute the Type constructor method at any time, as long as the method is executed before the first access to its static field. If beforefieldinit is not available, the Runtime Library must execute the Type constructor method at a specific time to affect performance optimization. Details can be related to msdn.

The. ctor method is followed and the code is:

. Method Public Hidebysig specialname rtspecialname
Instance Void . Ctor () cel managed
{
// Code size 7 (0x7)
. Maxstack 8
Il_0000: ldarg. 0
Il_0001: Call instance Void [Mscorlib] system. Object:. ctor ()
Il_0006: Ret
} // End of method helloworld:. ctor
    • In the method body, the pencil managed indicates the Il code, which indicates that the compiler compiles the managed code.
    • . Maxstack indicates that the evaluation stack during the execution of the constructor can accommodate the maximum number of data items. The evaluation stack is used to save the value of the variable required by the method, clear the value at the end of the method execution, or store a return value.
    • Il_0000 is the beginning of a code line. Generally, the part before il _ is the declaration and initialization of the variable.
    • Ldarg.0 indicates loading the first member parameter. In the instance method, it indicates the reference of the current instance. This reference will be used for calling in the base class constructor.
    • The call command is generally used to call a static method, because the static method is specified during the compilation period, and the constructor is called here. ctor () is also specified during the compilation period, while callvirt, the other command, indicates that the instance method is called. Its calling process is different from call, and the function is called at runtime, first, check whether the called function is a virtual function. If not, call the function directly. If yes, check whether the subclass has been overwritten. If yes, call the rewrite implementation, if you haven't called the original function, and so on until you find the latest rewrite implementation.
    • RET indicates that the execution is complete and return.

The last is the main method, and the code is:

. Method Public Hidebysig Static   Void Main () cel managed
{
. Entrypoint
// Code size 11 (0xb)
. Maxstack 8
Il_0000: ldstr " Hello, world. "
Il_0005: Call Void [Mscorlib] system. Console: writeline ( String )
Il_000a: Ret
} // End of method helloworld: Main
    • .Entrypoint indicates that the clr program helloworld.exe is first executed from the. entrypoint method, that is, the main method will be used as the entry function of the program. Each hosting program must have only one entry point. This is different from using the main function as the program entry sign.
    • The ldstr command indicates that the string is pressed to the stack, and the "Hello, world." string is moved to the top of the stack. CLR constructs a String object by obtaining a text constant from the metadata table. It is worth noting that the string object constructed here does not appear in the fifth return: let's get down to the keyword-The newobj command mentioned in the new statement. We will make a brief analysis of this explanation in the next example.
    • The hidebysig attribute indicates that if the current class acts as the parent class, the methods in the class are not inherited by the quilt class, so the main method is not seen in the helloworld subclass.

The following is a supplement:

    • For comments, the comments in the Il code are the same as those in C # and other advanced languages. In fact, the compiler has removed all comments when compiling the Il code, so any comments to the program are invisible in the Il code.

3.3 concise Regression

We can simplify our il code, which is based on the above analysis and displays the helloworld version of IL code in a more concise way, for more information, see annotations.

4.Conclusion

In this article, we have come to contact il from a point of view. In addition to understanding the meaning of several important commands, we have entered the world of Il. By scanning helloworld's il code in an all-in-one manner, we are not enough to understand il from the global perspective, but the first close contact at least makes us too unfamiliar, in addition, as the series of articles go deeper, we will gradually establish this cognition to improve our understanding. net underlying effective tools. This series will also be used in subsequent articles to gradually establish this method of using tools, so stay tuned.

 

 

References

(USA) Joe duy, professional. NET Framework 2.0

(USA) David Chappell, understanding. net

 

CLR team announcement

The CLR basic research team was established to study and explore the underlying knowledge of Clr and. NET Framework. We warmly welcome friends from the blog community. If you are interested in Clr and. Net underlying research, apply to join the CLR basic research team and share your technology in a dedicated environment. Before applying, please read the team program. The CLR team information is as follows:

Team address: http://clr.cnblogs.com/
Team program: View
Team application: Enter

Recent activities of the Team: Collect the CLR series of articles from the team members and publish them to the team.

Learn more

[Opening useful]
[First: resentment: Is and as]
[Second: Abstract programming: interfaces and abstract classes]
[Third: Historical entanglement: Characteristics and attributes]
[Forth: Back-to-Back: class and struct]
[Fifth: Let's look at the keywords-New]
[Sixth: Let's look at the keywords-base and this]
[7: taste type-starting from general type system]
[Eighth time: taste type-value type and reference type (top)-memory rational]
[Ninth: taste type-value type and reference type (medium)-unlimited Rules]
[10: taste type-value type and reference type (lower)-Application journey]
[11th: confusing parameters-art of transmission (I)]
[12th: confusing parameters-art of transmission (lower)]

2007 anytao.com

For original works, please refer to the author and source for the post. Leave this information.

This post uses"Status quo"No warranties are provided and no rights are granted.
This posting is provided "as is" with no warranties, and confers no rights.

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.