您的位置:首页 > 其它

MISL深入学习(2) 简单数组的使用

2008-08-18 17:02 393 查看
Code

1 .assembly extern mscorlib{}

2 .assembly Arr

3 {

4 .ver 1:0:1:0

5 }

6

7 .module Arr.exe

8

9 .method static void Main() cil managed

10 {

11 .maxstack 30

12 .entrypoint

13 .locals init(int32 Counter,int32[] intArr)

14

15

16 ldc.i4 10

17 dup /*dup指令"复制"计算栈中顶端的值,并存贮于计算栈顶*/

18 stloc Counter

19 newarr int32 /*于托管栈中分配新数组,数组引用类型.并将引用地址返回栈顶*/

20 stloc intArr /*将引用地址赋予intArr*/

21

22 /*

23 allocate and zero-initialize a zero-based, one-dimensional

24 array. The top of the evaluation stack specifies the total

25 number of elements in the array, and the instruction itself

26 specifies the data type of the elements.

27 */

28

29 //循环

30 Loop:

31 ldloc Counter

32 brfalse finished /**/

33

34 //自减

35 ldloc Counter

36 ldc.i4.1

37 sub

38 dup

39 call void[mscorlib]System.Console::WriteLine(int32)

40 stloc Counter

41

42 ldloc intArr //数组引用

43 ldloc Counter //数组索引

44 dup

45 ldc.i4.4

46 mul

47 stelem.i4

48 br.s Loop

49

50 /*

51 stloc是store local variable

52 ldloc是load local variable

53 */

54

55

56 /*两个数组操作指令的说明*/

57 /*

58 stelem.<typeSuffix> stelem 是 store element store

59 an item into an element of an array, with type and

60 range checking. The type is specified by the suffix

61 of the instruction and (for stelem.ref) the object

62 itself; any mismatch results in a System.ArrayTypeMismatchException.

63 The top of the stack contains the value to be stored.

64 The second item on the stack is the index, an unsigned

65 integer. A System.IndexOutOfRangeException will be thrown

66 if the index is larger than the size of the array. The

67 third item on the stack is the array itself, which will

68 result in a System.NullReferenceException if it is null.

69 To store an unboxed value type into an array, use ldelema

70 and stobj rather than stelem.<typeSuffix>.

71 */

72

73 /*

74 ldelem.<exTypeSuffix> ldelem 是 load element

75 load an element out of a zero-based, one-dimensional

76 array,with range and type checking. The type of the

77 array must match the suffix of the instruction or a

78 System.ArrayTypeMismatchException is raised. An out of

79 range subscript results in a System.IndexOutOfRangeException,

80 while an attempt to access an element of the null array results

81 in a System.NullReferenceException.

82 栈顶储存将提取数组的元素位置索引,栈顶次位储存数组引用地址

83 */

84

85

86 finished:

87 ldstr "finished!"

88 call void[mscorlib]System.Console::WriteLine(string)

89

90 /*输出数组intArr中索引为3的值*/

91 ldloc intArr

92 ldc.i4.3

93 ldelem.i4 /*看前面的注释*/

94 call void[mscorlib]System.Console::WriteLine(int32)

95

96 ldloc intArr

97 ldc.i4.2

98 ldelem.i4 /*看前面的注释*/

99 call void[mscorlib]System.Console::WriteLine(int32)

100

101

102 ldloc intArr

103 ldc.i4.1

104 ldelem.i4 /*看前面的注释*/

105 call void[mscorlib]System.Console::WriteLine(int32)

106

107 ret

108 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: