Functions

I l @ ve RuBoard

The Hello World IL example encompasses only one method. If only all software development requirements were that simple! Because IL is a stack based language, function parameters are pushed onto the stack and the function is called. The called function is responsible for removing any parameters from the stack and for pushing any return value back onto the stack for the caller of the function.

The process of pushing and popping values to and from the call stack can be visualized as shown in Figure 1.3.3.

Figure 1.3.3. The call stack.

graphics/0103fig03.gif

Although Figure 1.3.3. depicts a very simplified view of the actual process, enough information is provide to gain an understanding of the overall steps involved. Translating Figure 1.3.3 into IL produces the source in Listing 1.3.2.

Listing 1.3.2 IL Function Calls
 1: //File     :function.cil  2:  3: .assembly function_call 4: {  5:     .ver 1:0:0:0  6: }  7:  8:  9: //Add method 10: .method public static int32 'add'( int32 a, int32 b ) il managed 11: { 12: 13:    .maxstack   2 14:    .locals     (int32 V_0, int32 V_1) 15: 16:    //pop the parameters from the call stack 17:    ldarg.0 18:    ldarg.1 19: 20:    add 21: 22:    //push the result back on the call stack 23:    stloc.0 24:    ldloc.0 25: 26:    ret 27: } 28: 29: .method public static void main( ) il managed 30: { 31:    .entrypoint 32:    .maxstack 2 33:    .locals ( int32 V_0 ) 34: 35:    //push two parameters onto the call stack 36:    ldc.i4.s 10 37:    ldc.i4.s 5 38: 39:    call int32 'add'(int32,int32) 40: 41:    stloc.0              //pop the result in the local variable V_0 42: 43:    //Display the result 44:    ldstr "10 + 5 = { 0} " 45:    ldloc  V_0 46:    box       [mscorlib]System.Int32 47:    call void [mscorlib]System.Console::WriteLine(class System.String, class graphics/ccc.gif System.Object ) 48: 49:    ret 50: } 51: 

The function.cil code in Listing 1.3.2 introduces a new directive: .locals . This directive is used to reserve space for local variables used within the current function. The add method defined on line 10 declares space for two local variables. The parameters that were passed to the add method are loaded into the reserved space using the ldarg statement. After the add instruction has been issued, the next step is to store the result and then to push the result back onto the call stack.

The main method only allocates space for a single local variable. This local variable will be used to hold the result returned from the user -defined add method. The process of pushing the values 10 and 5 onto the stack, calling the add method, and popping the result correspond to the depiction in Figure 1.3.3.

You've no doubt noticed the box instruction on line 46. Boxing is something new to .NET. Boxing allows for value-types, such as primitives, to be treated as objects.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net