Use msil to write a "Hello world"

Source: Internet
Author: User
Tags mscorlib

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.

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.