-- 作者:admin
-- 发布时间:11/9/2004 2:25:00 AM
-- C#快餐-3
发信人: walts (休息一会), 信区: DotNET 标 题: C#快餐-3 发信站: BBS 水木清华站 (Thu Jul 26 01:14:38 2001) Lesson 3. Hacker's introduction to MSIL Part2. Let's recall garbage collection rules that we have learned in Lesson2 No data or local variables can be left on stack when program terminates. add adds data and the variable on top of the stack. The data is popped from the stack, and the result is stored inside the variable, which remains on stack. Function calls such as Console::WriteLine(...) print and pop a variable from the stack. They cannot print data directly.. Garbage collection of variables which are not stored on the stack is automatic. Before we look at a much more interesting example of OO programing, let's learn how to pass parameters to a function. The simplest example. .assembly hello{} .class Test{ .method private static void hello(int32) il managed { ldarg.0 call void [mscorlib]System.Console::WriteLine(int32) ret } .method public static void Main() il managed { .entrypoint ldc.i4.3 call void Test::hello(int32) ret } } Note the use of keyword ldarg.0 to load an integer argument of void hel lo(int). This argument is then stored on the stack and is later passed to WriteLine . Let's look at a more interesting C# example involving creation of a cla ss. using System; class mInt { public int x=3; } class Test{ public static void Main(){ mInt mint=new mInt(); Console.WriteLine(mint.x); } } This program translates into the following MSIL code: .assembly hello{} .class mInt { .field public int32 x .method public instance void .ctor() il managed { ldarg.0 //null argument needs to be passed to the constructor or //InvalidProgramExecution exception is thrown ldc.i4.3 stfld int32 mInt::x ret } } .class Test { .method static public void main(){ .entrypoint newobj instance void mInt::.ctor() //create a new instance of mInt on the heap ldfld int32 mInt::x call void [mscorlib]System.Console::WriteLine(int32) ret } } .method public instance void .ctor() il managed declares a constructor of class mInt. As we learned in our first OO class, the compiler is providing a default constructor if we have not written one yourself. When programming MSIL we are doing some of the compiler work. So, welcome to .ctor() notation! .ctor() is responsible for explicit initialization of all data members of class mInt. The declaration of int x is done with .field keyword. The .ctor() is loading data into mInt fields with with a call stfld. All constructors are designated with .ctor keyword. newobj is MSIL's equivalen of new. Usually constructors return void. The syntax of MSIL seem to allow non void constructors. Let's first look at a simpler problem of returning by value in MSIL. ldfld field Push on the stack field of an object newobj ctor Create a new instance of the object, initialize its fields to 0 or null and push it on the stack stfld field Replace the value of a field of an object ldarg Load first argument of the method into the stack The program below shows how to return a value. The value is simply left on the stack in a local variable of mtest(). When mtest() is called, the local variable of mtest() is transferred to a local variable of Main, which in turn is pushed on stack of Main. .assembly hello{} .class Test{ .method private static int32 mtest() il managed { .locals (int32 V_0) ldc.i4.3 stloc.0 ldloc.0 //push variable on stack ret } .method public static void Main() il managed { .entrypoint call int32 Test::mtest() call void [mscorlib]System.Console::WriteLine(int32) ret } } Exercises 1. Which identifiers are not allowed in C#: $Test, ?hi, @weird,_down.? 2. Find a mistake in this page. 3. Write a really cool program and send it to me. Do not forget to put a lot of comments. -- A great poem is a fountain forever overflowing with the waters of wisdom and delight. —— Shelley ※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.142.118] 上一篇 返回上一页 回到目录 回到页首 下一篇
|