Use msil to write a "hello World" program
C # is an easy-to-use programming language. However, with the continuous development of Clr and various class libraries, the encapsulation location is getting farther and farther away from the "bottom layer", which makes us unable to know its operation
The essence of a row. Next I will try to write a "hello World" program in the CLR language msil to gain a glimpse of its internal mechanism. Since we are studying its "bottom layer", we naturally don't need vs. We can directly use NotePad to edit it.
Write the following program in Notepad:
. Assembly extern mscorlib {auto}
. Assembly MyApp {}
. Module myapp.exe
. Namespace MyApp
{
. Class public program extends [mscorlib] system. Object
{
. Method static private void main (string [] ARGs)
{
. Entrypoint
Ldstr "Hello world! "
Call void [mscorlib] system. Console: writeline (string)
RET
}
}
}
Save as helloworld. il. Note that the extension is the specific extension of the Il file "Il ". (Using the Framework tool ildasm.exe, You Can decompile the dynamic link library to obtain its il code, and then use
Code ).
Run cmd.exeand find the ilasm.exe file under framework. For example, on my machine (the Framework is installed in the default directory)
Is "C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727 ". Switch the prefix folder to the folder where ilasm.exe is located. Then use ilasm.exe to compile helloworld. il. For example
:
Then a helloworld.exe file will be generated in the directory helloworld.il( of course, the prerequisite is that there is no error :)).
In this way, we use msil to write a "hello World" program. Although msil does not mean understanding the operating mechanism of. Net CLR, it helps to understand the operating mechanism of CLR.