[Original] in-depth C # kernel il assembly language (1) by Eddy

Source: Internet
Author: User
Tags mscorlib

First, let's take a look at several terms.

CLR: Common Language Runtime public Language Runtime Library

It is applied in. netProgramAnd the underlying operating system. This operation layer is applicable to runtime environments used by. NET applications.

For example, we want to writeC LanguageThe C language is compiled into an assembly language, and the Assembly Language is compiling into a machine language CPU command execution. Then the assembly language is similar to this CLR, but this metaphor is just a metaphor, the difference is that CLR is not an interpreter.

CLS: Common Language Specification

It ensures that. NET application interoperability, known as the common language operation specification, CLS limits naming conventions, data types, function types, and some other elements, providing a public standard for different languages.

For example, the names of myfield, myfield, and myfield in VB. NET are actually a variable, which is case-insensitive and can be written into global variables outside the class. But C # cannot. This is what the specifications do. Different Languages have different specifications, C #, VB. net, F #, J #, and so on. every member of the net family has its own specification. After the final explanation of the specification, the same language will be compiled. As for what the language is, you know.

CLI: Basic Structure of Common Language infrastrucure public language

It is part of ECMA's internationalization and ISO standards and describes the language infrastructure.

Metadata: Metadata

Managed code: managedCode

The above two are components of CLR.

Metadata is a system composed of descriptors of all structural item classes, class members, class features, global items, and their relationships.

Managed code represents the method functions of an application, which are encoded in the abstract binary form of Microsoft's intermediate language or public intermediate language. Now, we finally throw the center of this article:

Msil: Microsoft intermediate language

Pencil: Common intermediate language common intermediate language

These are collectively referred to as the Il intermediate language.

 

The IL code is managed by CLR ". CLR hosting includes at least three main activities: Type Control, structured exception handling, and garbage collection.

Type control involves the verification and conversion of item types during execution.

Managed exception handling is similar in functionality to unmanaged structured exception handling, but it is executed by CLR rather than by the operating system.

Garbage collection involves automatic identification and release of objects that are no longer in use. (Except for smart devices, the garbage collection function is not available for the time being)

In the CLR environment ,.. Net Applications consist of one or more managed executable bodies, each of which carries metadata and managed code. The managed code is optional, but must contain at least metadata, if none. Then you can eat it. The hosted. NET application is called an assembly. The managed executable is called a module. Each Assembly contains a main module. What is this module? You know.

Two main components hosting the executable body: Metadata and IL code. The main subsystems for CLR to process these two components are the loader and JIT (just-in-time instant) editors.

 

All right, the hands are sour, and it's a waste of steamed buns. Now let's talk about il. It's not nonsense. It's about providing your background ideas of Il.

 

Code

. Assembly Extern Mscorlib {auto}
. Assembly andcount {}
. Module andcount.exe
. Namespace Andcount {
. Class   Public Andcount extends [mscorlib] system. object {
. Method Public   Static   Void Addcount () cel managed {
. Entrypoint
. Locals Init (int32 retvalue)
. Try {
Askafternum:

Ldstr " --------------------------------------------------------- \ N addition Loading "
Call Void [Mscorlib] system. Console: Write ( String )
Ldstr " \ N enter the first number: "
Call Void [Mscorlib] system. Console: Write ( String )
Call String [Mscorlib] system. Console: Readline ()
Call int32 [mscorlib] system. Convert: toint32 ( String )
Ldstr " \ N enter the second number: "
Call Void [Mscorlib] system. Console: Write ( String )
Call String [Mscorlib] system. Console: Readline ()
Call int32 [mscorlib] system. Convert: toint32 ( String )
Add
Stloc retvalue
Ldloc retvalue
Brfalse Error
Ldstr " After calculation, the result is "
Call Void [Mscorlib] system. Console: writeline ( String )
Ldloc retvalue
BR printcount
Error:
Ldstr " Error "
Printcount:
Call Void [Mscorlib] system. Console: writeline (int32)
Ldloc retvalue
Brtrue askafternum
}
Catch [Mscorlib] system. Exception
{
Pop
Ldstr " \ N temporary error! \ N "
Call Void [Mscorlib] system. Console: Write ( String )
BR askafternum
}
RET
}
}
}
. Field Public   Static Valuetype chararray8 format at formatdata
. Data formatdata = Bytearray ( 25   64   00   00   00   00   00   00 )
. Class   Public   Explicit Chararray8
Extends [mscorlib] system. valuetype {. Size 8 }
. Method Public   Static Pinvokeimpl ( " Msvcrt. dll " Cdecl)
Vararg int32 sscanf ( String , Int8 * ) Pencil managed {}

 

 

Save it as test. Il, save the compilation, and execute

For example, save it in D: \ test. Il. Open CMD and execute ilasm D: \ test. Il to generate test.exe. Run the command to check the result.

The Code is a huge strawberry. I will talk about the Il assembly language in the next article.

 

BenArticleOnly for learning. Do not reprint it! Remember to remember (repost also writes a source, thank you)

By Eddy 2010.05.30

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.