【读书笔记】IL Getting Started
Abstract:
IL directives:
ld<xxx>: load xxx onto stack
st<xxx>: pop off stack into xxx
(1)
ldarg.<length> - load argument onto the stack
| Format | Assembly Format | Description |
| FE 09 <unsigned int16> | ldarg num | Load argument numbered num onto stack. |
| 0E <unsigned int8> | ldarg.s num | Load argument numbered num onto stack, short form. |
| 02 | ldarg.0 | Load argument 0 onto stack |
| 03 | ldarg.1 | Load argument 1 onto stack |
| 04 | ldarg.2 | Load argument 2 onto stack |
| 05 | ldarg.3 | Load argument 3 onto stack |
Description:
The ldarg num instruction pushes the num’th incoming argument, where arguments are numbered 0 onwards (see Partition I_alink_partitionI) onto the evaluation stack. The ldarg instruction can be used to load a value type or a built-in value onto the stack by copying it from an incoming argument. The type of the value is the same as the type of the argument, as specified by the current method’s signature.
The ldarg.0, ldarg.1, ldarg.2, and ldarg.3 instructions are efficient encodings for loading any of the first 4 arguments. The ldarg.s instruction is an efficient encoding for loading argument numbers 4 through 255.
For procedures that take a variable-length argument list, the ldarg instructions can be used only for the initial fixed arguments, not those in the variable part of the signature. (See the arglist instruction)
Arguments that hold an integer value smaller than 4 bytes long are expanded to type int32 when they are loaded onto the stack. Floating-point values are expanded to their native size (type F).
(2)
starg.<length> - store a value in an argument slot
| Format | Assembly Format | Description |
| FE 0B <unsigned int16> | starg num | Store a value to the argument numbered num |
| 10 <unsigned int8> | starg.s num | Store a value to the argument numbered num, short form |
Description:
The starg num instruction pops a value from the stack and places it in argument slot num (see Partition I_alink_partitionI). The type of the value must match the type of the argument, as specified in the current method’s signature. The starg.s instruction provides an efficient encoding for use with the first 256 arguments.
For procedures that take a variable argument list, the starg instructions can be used only for the initial fixed arguments, not those in the variable part of the signature.
Storing into arguments that hold an integer value smaller than 4 bytes long truncates the value as it moves from the stack to the argument. Floating-point values are rounded from their native size (typeF) to the size associated with the argument.
(3)
ldloc - load local variable onto the stack
| Format | Assembly Format | Description |
| FE 0C<unsigned int16> | ldloc indx | Load local variable of index indx onto stack. |
| 11 <unsigned int8> | ldloc.s indx | Load local variable of index indx onto stack, short form. |
| 06 | ldloc.0 | Load local variable 0 onto stack. |
| 07 | ldloc.1 | Load local variable 1 onto stack. |
| 08 | ldloc.2 | Load local variable 2 onto stack. |
| 09 | ldloc.3 | Load local variable 3 onto stack. |
Description:
The ldloc indx instruction pushes the contents of the local variable number indx onto the evaluation stack, where local variables are numbered 0 onwards. Local variables are initialized to 0 before entering the method only if the initialize flag on the method is true (see Partition I_alink_partitionI). The ldloc.0, ldloc.1, ldloc.2, and ldloc.3 instructions provide an efficient encoding for accessing the first four local variables. The ldloc.s instruction provides an efficient encoding for accessing local variables 4 through 255.
The type of the value is the same as the type of the local variable, which is specified in the method header. See Partition I_alink_partitionI.
Local variables that are smaller than 4 bytes long are expanded to type int32 when they are loaded onto the stack. Floating-point values are expanded to their native size (type F).
(4)
stloc - pop value from stack to local variable
| Format | Assembly Format | Description |
| FE 0E <unsigned int16> | stloc indx | Pop value from stack into local variable indx. |
| 13 <unsigned int8> | stloc.s indx | Pop value from stack into local variable indx, short form. |
| 0A | stloc.0 | Pop value from stack into local variable 0. |
| 0B | stloc.1 | Pop value from stack into local variable 1. |
| 0C | stloc.2 | Pop value from stack into local variable 2. |
| 0D | stloc.3 | Pop value from stack into local variable 3. |
Description:
The stloc indx instruction pops the top value off the evalution stack and moves it into local variable number indx (see Partition I_alink_partitionI), where local variables are numbered 0 onwards. The type of value must match the type of the local variable as specified in the current method’s locals signature. The stloc.0, stloc.1, stloc.2, and stloc.3 instructions provide an efficient encoding for the first four local variables; the stloc.s instruction provides an efficient encoding for local variables 4 through 255.
Storing into locals that hold an integer value smaller than 4 bytes long truncates the value as it moves from the stack to the local variable. Floating-point values are rounded from their native size (type F) to the size associated with the argument.
Example:
1: private static void Main(string[] args)
2: {3: string strA = "abcdef";
4: string strB = "abcdef";
5: Console.WriteLine(object.ReferenceEquals(strA, strB));
6: string strC = "abcdef";
7: Console.WriteLine(object.ReferenceEquals(strA, strC));
8: string strD = "abc";
9: string strE = strD + "def";
10: Console.WriteLine(object.ReferenceEquals(strA, strE));
11: strE = string.Intern(strE);
12: Console.WriteLine(object.ReferenceEquals(strA, strE));
13: }
The corresponding IL instructions are as below,
1: .method private hidebysig static void Main(string[] args) cil managed
2: { 3: .entrypoint 4: .maxstack 2 5: .locals init (6: [0] string strA,
7: [1] string strB,
8: [2] string strC,
9: [3] string strD,
10: [4] string strE)
11: L_0000: nop 12: L_0001: ldstr "abcdef"
13: L_0006: stloc.0 14: L_0007: ldstr "abcdef"
15: L_000c: stloc.1 16: L_000d: ldloc.0 17: L_000e: ldloc.1 18: L_000f: call bool [mscorlib]System.Object::ReferenceEquals(object, object)
19: L_0014: call void [mscorlib]System.Console::WriteLine(bool)
20: L_0019: nop 21: L_001a: ldstr "abcdef"
22: L_001f: stloc.2 23: L_0020: ldloc.0 24: L_0021: ldloc.2 25: L_0022: call bool [mscorlib]System.Object::ReferenceEquals(object, object)
26: L_0027: call void [mscorlib]System.Console::WriteLine(bool)
27: L_002c: nop 28: L_002d: ldstr "abc"
29: L_0032: stloc.3 30: L_0033: ldloc.3 31: L_0034: ldstr "def"
32: L_0039: call string [mscorlib]System.String::Concat(string, string)
33: L_003e: stloc.s strE 34: L_0040: ldloc.0 35: L_0041: ldloc.s strE36: L_0043: call bool [mscorlib]System.Object::ReferenceEquals(object, object)
37: L_0048: call void [mscorlib]System.Console::WriteLine(bool)
38: L_004d: nop 39: L_004e: ldloc.s strE40: L_0050: call string [mscorlib]System.String::Intern(string)
41: L_0055: stloc.s strE 42: L_0057: ldloc.0 43: L_0058: ldloc.s strE44: L_005a: call bool [mscorlib]System.Object::ReferenceEquals(object, object)
45: L_005f: call void [mscorlib]System.Console::WriteLine(bool)
46: L_0064: nop 47: L_0065: ret 48: }
Conclusion: It’s a must to understand IL to better understand .NET.
--End--
--------------------------------------
Regards,
FangwenYu

浙公网安备 33010602011771号