Library tutorials & articles

Common Intermediate Language

Variables

In this part we will look at the use of variables.

Rather than talk about it for a while I'm just going to show you some MSIL and then talk through what it does bit by bit.

// using variables
// this sample refers to variables by location
.assembly extern mscorlib { }

.assembly Blog
{
	.ver 1:0:0:0
}

.module Part2.exe

.method static void main()
{
	.entrypoint
	.maxstack 1
	.locals init (int32, string, int32)
	ldc.i4 9
	stloc.0
	ldstr "Granville"
	stloc.1
	ldc.i4 3
	stloc.2
	ldloc.0
	call void [mscorlib]System.Console::WriteLine(int32)
	ldloc.1
	call void [mscorlib]System.Console::WriteLine(string)
	ldloc.2
	call void [mscorlib]System.Console::WriteLine(int32)
	ret
}

Like always we have an entry point to the application which is main, and we declare that at most we only require one slot on the evaluation stack. Next, we define 3 variables - 2 of type int32, and the other of type string - these variables are initialized to their default values, so the int32's are initialized to 0 and the string null.

Because the variables have no name I refer to them with regards to location, first I load a byte integer and store than into location 0, then a string which is stored at location 1, and finally the last integer stored at location 2.

With the values of variables stored I know load each one onto the stack using ldloc, I then print each variables content out to the console popping each off of the stack.

Here is the same example, but this time I alias the variables with more readable names.

// using variables
// this sample refers to variables by name
.assembly extern mscorlib { }

.assembly Blog
{
	.ver 1:0:0:0
}

.module Part2.exe

.method static void main()
{
	.entrypoint
	.maxstack 1
	.locals init (int32 day, string name, int32 month)
	ldc.i4 9
	stloc day
	ldstr "Granville"
	stloc name
	ldc.i4 3
	stloc month
	ldloc day
	call void [mscorlib]System.Console::WriteLine(int32)
	ldloc name
	call void [mscorlib]System.Console::WriteLine(string)
	ldloc month
	call void [mscorlib]System.Console::WriteLine(int32)
	ret
}

Comments

  1. 16 Oct 2009 at 03:38

    can u please do sove this program.......and send it at my e-mail add...rach_dizon2002@yahoo.com 1. create a program called MathInteger.java that presents a menu tothe user nwith choices for (1) adding first n integer (2) multiplying the first n integer and (3) quitting.use do while loop.Also switch statement for option 1 and 2. 2. create a program called SumIntegers.java allows the user to enter exactly 10 n positive integers.If the user accidentally enters negative integer the program should use continue statement to skip the entry.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Common Intermediate Language.

Leave a comment

Sign in or Join us (it's free).

Granville Barnettt My name is Granville Barnett I have been a programmer now for quite some time mainly focusing on .NET technologies (C#), C++ and general research (algorithms, compilers etc)

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!