One of the IL series articles:
Say Hello to IL
.NET appears, many people have been able to use C # to develop software development, and some people like me like to study the internal operational mechanism of .NET. I will talk about IL (Intermediate Language), although we are unlikely to use IL to develop software, but IL is indeed understanding the door of the .NET internal mechanism.
Visual Studio.net provides us with two tools ILASM and ILDASM related to IL. They are IL compilers and IL anti-compilers. They can see Msnd documents for them. I will not say it here. However, I will talk about IL, I have to talk about the CLR foundation.
CLR (Common Language Runtime), MSDN document also has some descriptions of it, and I have added some additions to MSDN documents to make readers better understand CLR. The CLR is the core of the .NET framework, you can imagine it as a CPU that can run the IL Code. For a normal Intel CPU, it relies on Register and Stack to run the program, while CLR is a CPU that relies only by STACK to run the program. For example, if we want to perform 1 2 operation, the IL command will now move to the top of Stack to the top of Stack, and the add puts 1 and 2 from the top of the Stack, and put the calculation result 3 to Stack top. The CLR's Stack is not in the form of byte, nor is it in the form of Word, but Slot. Slot is a structure that can accommodate any type of entity. We can put int-> float-> Object A-> Float-> Object A-> Object B can be placed in Stack in Stack. -> Int, everything is possible. The process of moving to the top of the Stack is called Loading, which is called Storing from the process of removing the entity from the top of the Stack.
Ok, let's start with IL intimate contact! Look at our first IL program - "Hello World".
.assembly printstring {}
//Console.writeline ("hello, world) "
.method static public void main () il managed
{
.EntryPoint // IL does not need to define the main () function as the entry of the program, but use the .EntryPoint definition inlet
.maxstack 8 // Defines the maximum depth of 8 in STACK 8
// ******************************************************** *****
// console.writeline ("Hello, World)";
// ******************************************************** *****
LDSTR "Hello, World" // Moves "Hello World" this String to the top of Stack
// call static system.console.writeline function
// WriteLine () removes String from the top of the Stack, does not return any value
Call void [mscorlib] system.console :: WriteLine (class system.string)
// ******************************************************** ***** RET
}
The save program is Hello.il, then it is compiled. Open the Visual Studio .NET command prompt, switch to the directory where hello.il is located, enter "ILASM Hello.il", Enter. ILASM will prompt you to compile, enter "Hello", Enter. You can see the running result of our program.