1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| 1.常量下标显示: printf("%d\r\n",ary[2][3]);
debug版本汇编: 00401091 mov eax,dword ptr [ebp-8] 00401094 push eax 00401095 push offset string "%d\n" (0042201c) 0040109A call printf (00401160) 0040109F add esp,8
分析:1.跟常量赋值一样,无法确定是数组 2.确定是数组了,公式公式: ary+sizeof(int[5])*2+sizeof(int)*3(ebp‐3c)+(14*2)+(4*3)
2.前面是变量下标 debug汇编: 30: printf("%d\n",Array[argc][3]); 004010A2 mov ecx,dword ptr [ebp+8] ;出现乘法比例因子,没能搞这么大 004010A5 imul ecx,ecx,14h 004010A8 mov edx,dword ptr [ebp+ecx-30h] ;常量折叠,误算数组首地址,可以改 004010AC push edx 004010AD push offset string "%d\n" (0042201c) 004010B2 call printf (00401160) 004010B7 add esp,8 公式: ary+sizeof(int[5])*argc+sizeof(int)*3
3.后面是变量下标 debug版本: 31: printf("%d\n",Array[2][argc]); 004010BA mov eax,dword ptr [ebp+8] ;常量折叠优化 004010BD mov ecx,dword ptr [ebp+eax*4-14h] 004010C1 push ecx 004010C2 push offset string "%d\n" (0042201c) 004010C7 call printf (00401160) 004010CC add esp,8 公式: ary+sizeof(int[5])*2+sizeof(int)*argcebp‐3c+14*2+argc*4
4.两个都是表达式 debug版本: 32: printf("%d\n",Array[argc % -8][argc / 8]); 004010CF mov eax,dword ptr [ebp+8] 004010D2 cdq 004010D3 xor eax,edx 004010D5 sub eax,edx 004010D7 and eax,7 004010DA xor eax,edx 004010DC sub eax,edx 004010DE imul eax,eax,14h 004010E1 lea ecx,[ebp+eax-3Ch] 004010E5 mov eax,dword ptr [ebp+8] 004010E8 cdq 004010E9 and edx,7 004010EC add eax,edx 004010EE sar eax,3 004010F1 mov edx,dword ptr [ecx+eax*4] 004010F4 push edx 004010F5 push offset string "%d\n" (0042201c) 004010FA call printf (00401160) 004010FF add esp,8 公式: ecx=(ebp‐3c)+(eax*14)ecx+(eax*4)
5.优化遍历二维数组 vc6下优化: .text:00401000 ; int __cdecl main(int argc, const char **argv, const char **envp) .text:00401000 _main proc near ; CODE XREF: start+AF↓p .text:00401000 .text:00401000 ary = word ptr -3Ch .text:00401000 var_1C = dword ptr -1Ch .text:00401000 var_18 = dword ptr -18h .text:00401000 var_14 = dword ptr -14h .text:00401000 var_10 = dword ptr -10h .text:00401000 var_C = dword ptr -0Ch .text:00401000 var_8 = dword ptr -8 .text:00401000 var_4 = dword ptr -4 .text:00401000 argc = dword ptr 4 .text:00401000 argv = dword ptr 8 .text:00401000 envp = dword ptr 0Ch .text:00401000 .text:00401000 sub esp, 3Ch .text:00401003 push ebx .text:00401004 mov ebx, 3 .text:00401009 push esi .text:0040100A push edi .text:0040100B mov dword ptr [esp+48h+ary], 1 .text:00401013 mov dword ptr [esp+48h+ary+4], 2 .text:0040101B mov dword ptr [esp+48h+ary+8], ebx .text:0040101F mov dword ptr [esp+48h+ary+0Ch], 4 .text:00401027 mov dword ptr [esp+48h+ary+10h], 5 .text:0040102F mov dword ptr [esp+48h+ary+14h], 0Ah .text:00401037 mov dword ptr [esp+48h+ary+18h], 14h .text:0040103F mov dword ptr [esp+48h+ary+1Ch], 1Eh .text:00401047 mov [esp+48h+var_1C], 28h .text:0040104F mov [esp+48h+var_18], 32h .text:00401057 mov [esp+48h+var_14], 64h .text:0040105F mov [esp+48h+var_10], 0C8h .text:00401067 mov [esp+48h+var_C], 12Ch .text:0040106F mov [esp+48h+var_8], 190h .text:00401077 mov [esp+48h+var_4], 1F4h .text:0040107F lea esi, [esp+48h+ary] .text:00401083 .text:00401083 loc_401083: ; CODE XREF: _main+9F↓j .text:00401083 mov edi, 5 .text:00401088 .text:00401088 loc_401088: ; CODE XREF: _main+9C↓j .text:00401088 mov eax, [esi] .text:0040108A push eax .text:0040108B push offset unk_407030 .text:00401090 call sub_4010B0 .text:00401095 add esp, 8 .text:00401098 add esi, 4 .text:0040109B dec edi .text:0040109C jnz short loc_401088 .text:0040109E dec ebx .text:0040109F jnz short loc_401083 .text:004010A1 pop edi .text:004010A2 pop esi .text:004010A3 xor eax, eax .text:004010A5 pop ebx .text:004010A6 add esp, 3Ch .text:004010A9 retn .text:004010A9 _main endp
2019下优化: .text:00401040 _main proc near ; CODE XREF: __scrt_common_main_seh+F5↓p .text:00401040 .text:00401040 Array = dword ptr -3Ch .text:00401040 .text:00401040 push ebp .text:00401041 mov ebp, esp .text:00401043 sub esp, 3Ch .text:00401046 movaps xmm0, ds:__xmm@00000004000000030000000200000001 .text:0040104D movups xmmword ptr [ebp+Array], xmm0 .text:00401051 push ebx .text:00401052 movaps xmm0, ds:__xmm@0000001e000000140000000a00000005 .text:00401059 mov ebx, 3 .text:0040105E push esi .text:0040105F movups xmmword ptr [ebp+Array+10h], xmm0 .text:00401063 lea esi, [ebp+Array] .text:00401066 mov [ebp+Array+30h], 12Ch .text:0040106D movaps xmm0, ds:__xmm@000000c8000000640000003200000028 .text:00401074 push edi .text:00401075 movups xmmword ptr [ebp+Array+20h], xmm0 .text:00401079 mov [ebp+Array+34h], 190h .text:00401080 mov [ebp+Array+38h], 1F4h .text:00401087 nop word ptr [eax+eax+00000000h] .text:00401090 .text:00401090 loc_401090: ; CODE XREF: _main+7A↓j .text:00401090 mov edi, 5 .text:00401095 db 66h, 66h .text:00401095 nop word ptr [eax+eax+00000000h] .text:004010A0 .text:004010A0 loc_4010A0: ; CODE XREF: _main+75↓j .text:004010A0 push dword ptr [esi] .text:004010A2 push offset _Format ; "%d\n" .text:004010A7 call _printf .text:004010AC add esp, 8 .text:004010AF add esi, 4 .text:004010B2 sub edi, 1 .text:004010B5 jnz short loc_4010A0 .text:004010B7 sub ebx, 1 .text:004010BA jnz short loc_401090 .text:004010BC pop edi .text:004010BD pop esi .text:004010BE xor eax, eax .text:004010C0 pop ebx .text:004010C1 mov esp, ebp .text:004010C3 pop ebp .text:004010C4 retn .text:004010C4 _main endp .text:004010C4
|