Finishing an Introductory Look at CIL
(Page 1 of 7 )
In this second part to a two-part article on CIL, we'll take the C# program with which we started and convert it into CIL. This will involve several steps, which we'll describe and discuss in full.
In the previous article, we looked at a C# program that finds prime numbers between two and one million. We started to convert it to CIL, recreating the class and method involved in the program, and then we took a look at method calls and the concept of a stack. Finally, we diverged a bit from the main task (converting the C# program) in order to create a simple “hello world” program in CIL, which we then assembled and executed.
In this final part, we'll complete the conversion of our C# prime number program into CIL. This will involve learning local variables, mathematical operations and loops in CIL, which we'll all take step-by-step.
For convenience, here is the C# program from the last article, in its entirety:
using System;
public class FindPrimes
{
public static void Main(string[] args)
{
for (int i = 2; i <= 1000000; i++)
{
bool prime = true;
int limit = (int)Math.Sqrt(i);
for (int f = 2; f <= limit; f++)
{
if (i % f == 0)
{
prime = false;
break;
}
}
if (prime)
{
Console.WriteLine(i);
}
}
Console.WriteLine("Done!");
}
}
In addition, here is the converted CIL program so far:
.assembly extern mscorlib {}
.assembly FindPrimes {}
.class public FindPrimes
{
.method public static void Main(string[] args)
{
.entrypoint
}
}
Next: Local Variables >>
More ASP.NET Articles
More By Peyton McCullough