Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 6,070 times

Contents

Related Categories

Common Intermediate Language - Variables

gbarnett

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
}

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)

Comments