Common Intermediate Language/Getting started

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Installation[edit]

To compile CIL code, you'll need a program called ilasm, which comes in .NET Framework SDK installations.

There are two frameworks available: Microsoft's and Mono's.


Microsoft[edit]

This is a Windows-only framework and can be downloaded from Microsoft's .NET Framework Developer Center. The default installation path is C:\Windows, and the framework and ilasm will be placed in a folder relating to the version installed.

C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705 for version 1.0

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 for version 1.1

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 for version 2.0

C:\WINDOWS\Microsoft.NET\Framework\v3.0 for version 3.0

C:\WINDOWS\Microsoft.NET\Framework\v3.5 for version 3.5

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 for version 4.0+


A terminal, such as command prompt, can then execute ilasm from one of these folders. So, if version 3.0 is installed:

cd C:\WINDOWS\Microsoft.NET\Framework\v3.0

ilasm


Mono[edit]

This is a cross-platform framework and can be downloaded from the Mono website.

Windows and Mac[edit]

Installers are available on the website. On Window, the default installation path is C:\Program Files (x86)\Mono.

Linux[edit]

Mono is available through package managers:

  • Debian, Ubuntu, and derivatives
    • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    • echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
    • sudo apt-get update



After installation, a terminal, such as command prompt, can execute ilasm: ilasm

Hello, World![edit]

Below is the code for a simple "Hello, World!" program. Any text editor can be used to write CIL.

.assembly Hello {}
.assembly extern mscorlib {}

.method static void Main()
{
	.entrypoint
	.maxstack 1

	ldstr "Hello, World!"
	call void [mscorlib]System.Console::WriteLine(string)
	ret
}

First, save the following code to a file called Hello.il. Then, compile the code using ilasm: ilasm Hello.il. This will create an executable file called Hello.exe.

If you're on Windows, just enter Hello.exe to run the file. However, on other platforms, you'll need to use the command mono Hello.exe to run it.

Hello, World! should be outputted. You're done!