Evaluation Stack


"Hello World" Application

An example is a great place to begin an exploration of MSIL code programming. Following is a variation of the universally known Hello World application. It displays "Hello Name", where Name is read from the memory of a local variable.

 // Hello World Application .assembly extern mscorlib {} .assembly hello {} /* Starter class with entry point method */ .namespace Donis.CSharpBook {  .class Starter {      .method static public void Main() cil managed {          .maxstack 2          .entrypoint           .locals init (string name)          ldstr "Donis"          stloc.0          ldstr "Hello, {0}!"          ldloc name          call void [mscorlib] System.Console::WriteLine(          string, object)          ret          }      } } 

This is the command line that compiles the MSIL code to create a hello executable:

 ilasm /exe /debug hello.il 

The exe option indicates that the target is a console application, which is also the default. The dll option specifies a library target. The debug option asks the compiler to generate a debug file (pdb) for the application. A debug file is useful for a variety of reasons, including viewing source code in a debugger or disassembler.

The application begins with comments and three declaratives:

 // Hello World Application .assembly extern mscorlib{} .assembly hello{} /* Starter class with entry point method */ .namespace Donis.CSharpBook { 

MSIL supports C++ style comments—both single and multiline comments. The first declarative is an external reference to the mscorlib library. Mscorlib.dll contains the core of the .NET Framework class library (FCL), which includes the System.Console class. The assembly directive describes the executing assembly, in which the simple name of the assembly is hello. Notice that the simple name does not include the extension. The third directive defines a namespace.

The next two lines define a class and a method. This class directive introduces a public class named Starter, which implicitly inherits the System.Object class. The method directive defines Main as a member method. Main is a managed, public, and static function:

 .class Starter {  .method static public void Main() cil managed { 

The Main method starts with three directives. The .maxstack directive sets the size of the evaluation stack to two slots. The .entrypoint directive designates Main as the entry point of the application. By convention, Main is always the entry point of a C# executable. In MSIL, the entry point method is whatever method contains the .entrypoint directive, which could be a method other than Main. Finally, the .local directive declares a local string variable called name.

 .maxstack 2 .entrypoint  .locals init (string name) 

Table 11-1 describes the MSIL code of the Main method.

Table 11-1: Hello World MSIL Code
Open table as spreadsheet

Instruction

Description

ldstr "Donis"

Load the string "Donis" onto the evaluation stack.

stloc.0

Store "Donis" from the evaluation stack into the first local variable, which is called name.

ldstr "Hello, {0}!"

Load the "Hello" string prefix onto the evaluation stack.

ldloc name

Load the local variable onto the evaluation stack. (Local variables are referenced by index or by name.)

call void [mscorlib] System.Console::WriteLine(string, object)

Call the Console::WriteLine method, which consumes the two items on the evaluation stack as parameters from right to left. "Hello name" is displayed.

ret

Return from the method.




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

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