CHAPTER 15 UNDERSTANDING (Web design software) CIL AND THE ROLE

CHAPTER 15 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES 499 This innocent-looking method has a lot to say in terms of CIL. First, the incoming arguments (a and b) must be pushed onto the virtual execution stack using the ldarg (load argument) opcode. Next, the add opcode will be used to pop the next two values off the stack and find the summation, and store the value on the stack yet again. Finally, this sum is popped off the stack and returned to the caller via the ret opcode. If you were to disassemble this C# method using ildasm.exe, you would find numerous additional tokens injected by csc.exe, but the crux of the CIL code is quite simple: .method public hidebysig static int32 Add(int32 a, int32 b) cil managed { .maxstack 2 ldarg.0 // Load ‘a’ onto the stack. ldarg.1 // Load ‘b’ onto the stack. add // Add both values. ret } The Hidden this Reference Notice that the two incoming arguments (a and b) are referenced within the CIL code using their indexed position (index 0 and index 1), given that the virtual execution stack begins indexing at position 0. One thing to be very mindful of when you are examining or authoring raw CIL code is that every (nonstatic) method that takes incoming arguments automatically receives an implicit additional parameter, which is a reference to the current object (think the C# this keyword). Given this, if the Add() method were defined as nonstatic // No longer static! public int Add(int a, int b) { return a + b; } the incoming a and b arguments are loaded using ldarg.1 and ldarg.2 (rather than the expected ldarg.0 and ldarg.1 opcodes). Again, the reason is that slot 0 actually contains the implicit this reference. Consider the following pseudo-code: // This is JUST pseudo-code! .method public hidebysig static int32 AddTwoIntParams( MyClass_HiddenThisPointer this, int32 a, int32 b) cil managed { ldarg.0 // Load MyClass_HiddenThisPointer onto the stack. ldarg.1 // Load ‘a’ onto the stack. ldarg.2 // Load ‘b’ onto the stack. … } Representing Iteration Constructs in CIL Iteration constructs in the C# programming language are represented using the for, foreach, while, and do keywords, each of which has a specific representation in CIL. Consider the classic for loop: public static void CountToTen() { for(int i = 0; i < 10; i++) ; }
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Leave a Reply