.NET framework programming reading note (3) - Execute assembly code

zhaozj2021-02-16  49

1.4 Execute the assembly code

The hosted module includes metadata and IL code. IL is a machine language that Microsoft has been developed after consulting some business and academic language compiler authors. IL is much more advanced than most CPU machine languages, it can understand object types, and have many advanced instructions that can create and initialize objects, call the false method on the object to operate array elements directly. It even throws and captures an unusual instruction. We can treat IL as a machine language facing the object.

Normally, developers use a high-level language, such as: C # or Visual Basic. The compiler of these languages ​​will be IL code. Of course, we can also write IL programs directly in assembly language. Microsoft also provides an IL assembler: ILASM.EXE, and there is another reverse: 编: ildsm.exe.

Advanced languages ​​such as C # or Visual Basic are only a subset of all CLR full features. IL assembly language allows developers to get all the functions of CLR.

The characteristics of the summary IL are as follows:

1, facing the object-oriented characteristics, different from other assembly.

2, IL can get all the functions of CLR

3, IL does not bind to any particular CPU platform, that is, he can also praise the platform.

The .NET program execution process is as follows:

1 Before the method is executed, the CLR first detects all types of MAIN, and the CLR assigns an internal data structure that manages access to the reference type.

2. When the data structure is initialized, the CLR will set each entry to a function in the CLR inside, and we call the function as Jitcompiler.

3. When the MAIN method first calls the reference type of method member, the JitCompiler function will be called, which is responsible for compiling the IL code of a method costs CPU instructions.

1. JITCompiler replaces the address of the real way to call the data structure in the previous step 2 to the memory block address that contains the just compiled CPU instruction.

2, JitCompiler jumps to the code in the memory block, start executing.

Note: All methods of a type will only be compiled. When this type of method is called again, the previously compiled code will be used, so that performance loss can only be generated when the first call is called.

That is to say that the hosted code is very small than the non-hosting code, the performance is very small, and it is too short.

Advantages of hosting code in performance:

1. On the new type of 4CPU, the JIT compiler can generate local code that utilizes the special instructions provided by the new CPU. The non-hosting application is usually compiled into a CPU platform with minimal universal functions, generally avoiding special instructions provided using the new CPU. These special instructions tend to bring high performance boosts to applications on new CPUs.

2. The JIT compiler can detect some Boolean tests that are always returning on the machine running. E.g:

IF (NumberOfcpus> 1)

{

}

If the host machine has only one CPU, the JIT compiler will not produce any CPU instructions for this segment code. Local code for host machines will get better adjustment: The amount of code will become smaller, and the execution speed will be faster.

Of course, we can use the NGEN.exe tool to convert the IL code to a local code, and generate a file. When executing the program, the CLR will automatically check if there is a pre-compiled version exist. If there is, the CLR will load pre-compiled Code does not require additional runtime compilation.

1.4.1 IL and code verification

1, IL is a stack-based language

2, IL does not provide instructions for operating registers, developers can easily generate IL code.

3, the instructions needed by IL are also relatively small.

4, IL instructions are not type.

5, IL achieves abstraction to the CPU. The biggest advantage of IL is to improve the robustness of the application. When the IL code is compiled into a local CPU directive, the CLR will perform a process called verification.

The verification process checks the advanced IL code to make sure that everything it does is "safe". The following is some entries for the test:

1. Data cannot be read from the underestinated memory.

2, each method must pass the correct parameter number, and the type of each parameter must match correctly.

3. The return value of each method must be used correctly.

4, each method must have a return statement

. . . .

If the verification is not passed, there will be a system.security.VerificationException exception thrown, and the method will continue to execute.

The advantages of verification:

Through the verified code, we can make sure they do not access the memory they should not access, so they do not interfere with the code of another application. This means we can run multiple hosting applications in a separate Windows virtual address space.

转载请注明原文地址:https://www.9cbs.com/read-27016.html

New Post(0)