- Introduction
- What Is an Intermediate Language?
- Programming Fundamentals in Intermediate Language (IL)
- ILASM Compiler Options
- More on IL Programming Fundamentals
- Assemblies, Manifests, Metadata, and Modules
- Conclusion
Programming Fundamentals in Intermediate Language (IL)
In an IL, a line beginning with a period (.) is a directive to the assembler to perform a specific task such as creating a class, method, and so on. If a line starts with any other symbol, it's an instruction to the assembler.
Minimum programming requirements for writing and running an IL program are as follows:
- Entry and exit point defined
- Return type of the function defined
- Appropriate namespace to be used
- An assembly defined
- A method defined
The following table describes some of the important IL directives.
IL Directive |
Meaning |
.assembly |
Name of the deployment unit |
.class |
Creates a class |
.method |
Creates a method |
.entrypoint |
Shows the compiler the entry point into the program |
.module |
Could be either an .exe file or .dll |
.ctor |
Constructor of a class |
.hash |
Algorithm used for hashing |
.ver |
Major.minor.build.revision number |
The following table describes some of the important IL instructions.
IL Instruction |
Meaning |
ret |
Return from function |
call |
Calls a function |
hidebysig |
Derived class can't have two methods with the same signature |
specialname |
Implies that the function is a special case |
auto |
Layout memory to be decided at runtime |
extends |
Inherits from base class |
mscorlib |
Microsoft core library |
ldstr |
Loads a string onto the stack |
You can use any text editor to type your IL programs. For example, type the program shown in Listing 1 into a text editor such as Notepad or the VS.NET editor. The output of this program on the system console is the following:
Welcome to .NET Technology Group - PCS
Listing 1Welcome, IL Example
.assembly welcome {} .assembly extern mscorlib {} .method static public void main() cil managed { .entrypoint .maxstack 1 ldstr "Welcome to .NET Technology Group - PCS" call void [mscorlib]System.Console::WriteLine(class System.String) ret }
After typing your code, save it with an .il extension and compile it from the command line using the intermediate language compiler, ILASM (Intermediate Language assembler). Save the file in Listing 1 as welcome.il. Interestingly, you can save the file with any extension; the ILASM compiler will compile any file containing IL code. (Make sure that the file path is correct.)
Here's the syntax:
ilasm welcome.il
After compiling the code in Listing 1, ILASM creates an executable file called welcome.exe. Figure 1 shows the output in the command line.
Figure 1 Compiling your first IL program.
The following table examines the program in welcome.il and lists all the directives and instructions.
Code Line |
Description |
.assembly welcome{} |
This is the starting statement for any IL program. A program gets compiled even without this statement, but it will throw a runtime error: The module 'welcome.EXE' was expected to contain an assembly manifest. In this statement, welcome is the name of an assembly and it typically ends with {}. |
.assembly extern mscorlib {} |
This line contains a reference to an external assembly, mscorlib (Microsoft core library). |
.method static public void main() cil managed |
This statement declares the main method. .method static public void main() deviates slightly from the statement shown; it doesn't contain the cil managed statement after main(). Even then, the program gets compiled and adds cil managed instructions during runtime. This could be observed by opening the file welcome.exe in the ILDASM editor. Try changing the method name to xyz() instead of main(); the program still compiles and gets executed! The reason can be grasped from the explanation given in the following item. |
.entrypoint |
At least one entry point is required for the entire program. If you forget to include this statement, the compiler would throw the error No entry point declared for executable. This is the statement from which the program execution begins. |
.maxstack 1 |
This statement allocates one stack slot. If you doesn't specify this statement, ILASM by default allocates eight stack slots. As an optimization rule, it's always advised to specify the number of stack slots when possible. |
ldstr "Welcome to .NET Technology Group - PCS" |
This instruction pushes the string constant "Welcome to .NET Technology Group - PCS" onto the stack. |
call void [mscorlib]System. Console::WriteLine(class System.String) |
This instruction invokes the System.Console::WriteLine(class System.String). It's essential to call the methods with full signature. |
ret |
This instruction returns from the main() method. |
Intermediate language is strictly case-sensitive; hence, it's recommended to use the proper case for all directives and instructions.