Hello IL

I l @ ve RuBoard

Using IL to develop .NET applications and components is probably not something you'll be doing a lot. However, to truly dig into the .NET framework, a basic understanding of the IL structure is important. The .NET SDK does not ship with the underlying source code. This does not mean you can't dig into the implementation of the various components . However, it does require some knowledge of IL.

The traditional, almost required, first step in learning a new language is to display the infamous "Hello World" message on the console. Accomplishing this task with only IL is not as painful as it might sound. IL is not a truly low-level, assembly-like language. As with most modern languages, there is a fair amount of abstraction built into the language. Listing 1.3.1 shows the now famous "Hello World" program written in MSIL.

Listing 1.3.1 Hello World
 1: //File            :Hello.cil  2: //Author        :Richard L. Weeks  3: //  4: //  5:  6: //define some basic assembly information  7: .assembly hello 8: {  9:     .ver  1:0:0:0 10: } 11: 12: 13: //create an entry point for the exe 14: .method public static void main( ) il managed 15: { 16: 17:     .entrypoint 18:     .maxstack  1 19: 20: 21:     //load string to display 22:     ldstr "Hello World IL style\ n" 23: 24:     //display to console 25:     call void [mscorlib]System.Console::WriteLine( class System.String ) 26: 27:     ret 28: } 29: 

To compile Listing 1.3.1, issue the following command line:

 ilasm hello.cil 

The IL assembler will produce a .NET hello.exe .

This IL code displays the message "Hello World IL style" by using the static Write method of the System.Console object. The Console class is part of the .NET framework and, as its use implies, its purpose is to write to the standard output.

Notice that there is no need to create a class. IL is not an object-oriented language, but it does provide the constructs necessary to describe objects and use them. IL was designed to accommodate a variety of language constructs and programming paradigms . Such diversity and openness in its design will allow for many different languages to be targeted at the .NET runtime.

There are a number of directives that have special meanings within IL. These directives are shown in Table 1.3.1.

Table 1.3.1. IL Directives used in "Hello World"
Directive Meaning
.assembly Name of the resulting assembly to produce
.entrypoint Start of execution for an .exe assembly
.maxstack Number of stack slots to reserve for the current method or function

Notice that the .entrypoint directive defines the start of execution for any .exe assembly. Although our function has the name main , it could have any name we want. Unlike C, C++, and other high-level languages, IL does not define a function to serve as the beginning of execution. Rather, IL makes use of the .entrypoint directive to serve this purpose.

Another directive of interest is the .assembly directive. IL uses this directive to name the output of the assembly. The .assembly directive also contains the version number of the assembly.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net