Marco Dorantes' WebLog

"Computer science is no more about computers than astronomy is about telescopes" -Edsger W. Dijkstra

Including Common Intermediate Language to your .Net designer toolbox - the beginning

Including Common Intermediate Language to your .Net designer toolbox - the beginning

  • Comments 2

Include Common Intermediate Language to your .Net designer toolbox

 

Why?

See:

Abstraction stacks and multi-paradigm software design

http://blogs.msdn.com/marcod/archive/2004/02/19/76637.aspx

 

Simplistic program - straight start

Create a text file named uno.il

(“uno” has been the name of the very first program I have written for every programming language I have learned, uno means one in Spanish)

 

We want a .Net assembly, so start stating that:

 

.assembly uno {}

 

A .Net assembly contains interesting information known as metadata; also represent a logical group of .Net modules (separated files which contain actual CIL code), so we need at least one module containing our code:

 

.module uno.exe

 

This minimalist typical first program can be made of a single method that shows a string message on the screen, so state that with:

 

.method static void start()

 

As you see, individual methods can be used with CIL; the above line starts a method table in the assembly metadata, filled with:

 

Name of the method: start

Method arguments: none

Return type: void

Flag: static

 

There are many more entries in a metadata method table, which in out case get default values.

 

The grammar for a method definition is:

 

<method_def> ::=

.method <flags> <call_conv> <ret_type> <name>(<arg_list>) <impl>

{

   <method_body>

}

 

Following with our method body, we type:

 

{

 .entrypoint

 

To start the method body and to state that this method is the entry point for our assembly.

 

Now, CIL instructions for displaying a message on the screen, first we load the string message into the stack:

 

 ldstr "here uno is executing"

 

then we make a method call to another assembly:

 

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

 

to finish we state the end of our method and method body:

 

 ret

}

 

That’s it, we have our first CIL source, the complete listing is:

 

.assembly uno {}

.module uno.exe

 

.method static void start()

{

 .entrypoint

 ldstr "here uno is executing"

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

 ret

}

 

at this point you could save and compile with the following command line:

 

ilasm uno.il

 

the last line of the displayed output should be:

 

Operation completed successfully

 

And there must be a file named uno.exe, execute it and enjoy that special feeling that brought you to this profession of programming.

 

Just as Mr. Churchill said: "This is not the end, this is not even the beginning of the end; perhaps this is the end of the beginning"

 

Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post