一段代码,从中能看到些东西.关于数组,字符串,指针,编译器自动初始化,数据对齐.
0040B4E0 push ebp
0040B4E1 mov ebp,esp
0040B4E3 sub esp,8Ch
0040B4E9 push ebx
0040B4EA push esi
0040B4EB push edi
0040B4EC lea edi,[ebp-8Ch]
0040B4F2 mov ecx,23h
0040B4F7 mov eax,0CCCCCCCCh
0040B4FC rep stos dword ptr [edi]
5: int ret = 0;
0040B4FE mov dword ptr [ebp-4],0
6: char a ; //这里没有给他初始化,编译器为其保留了8个字节的空间,应该是对齐的原因
7: char b = 'b';
0040B505 mov byte ptr [ebp-0Ch],62h
8: char c[2]; //同样没有初始化,一共是2个字节,但是对齐,保留了8字节
9: char d[2] = {'\0'}; //编译器为第一个字节设值,后续的值全初始为0
0040B509 mov byte ptr [ebp-14h],0
0040B50D xor eax,eax
0040B50F mov byte ptr [ebp-13h],al
10: char e[3] = {'a', 'b', 'c'}; //初始化为立即值,由于对齐,也是4字节空间
0040B512 mov byte ptr [ebp-18h],61h
0040B516 mov byte ptr [ebp-17h],62h
0040B51A mov byte ptr [ebp-16h],63h
11: char f[4] = "aaa"; //初始化指向一段字符串,最后一值默认为0,逐个复制到栈中
0040B51E mov ecx,dword ptr [string "aa" (0041f10c)]
0040B524 mov dword ptr [ebp-1Ch],ecx
12: char h[5] = "bbbb"; //同上,但是由于对齐,其大小为8字节
0040B527 mov edx,dword ptr [string "bbbb" (0041ff44)]
0040B52D mov dword ptr [ebp-24h],edx
0040B530 mov al,[string "bbbb"+4 (0041ff48)]
0040B535 mov byte ptr [ebp-20h],al
13: char i[] = {'d'}; //数组大小为1字节,但是对齐使其为4字节
0040B538 mov byte ptr [ebp-28h],64h
14: char j[] = {'\0'}; //数组大小为1字节,对齐为4字节
0040B53C mov byte ptr [ebp-2Ch],0
15: char k[] = "ccccc"; //数组大小为6字节,因为编译器为字符串最后加0,对齐为8字节
0040B540 mov ecx,dword ptr [string "ccccc" (0041ff3c)]//逐个拷贝到栈
0040B546 mov dword ptr [ebp-34h],ecx
0040B549 mov dx,word ptr [string "ccccc"+4 (0041ff40)]
0040B550 mov word ptr [ebp-30h],dx
16: char l[] = {'a', 'b', 'c', 'd', 'e', 'f'};//数组长度为6,注意这里为立即数而非字符串
0040B554 mov byte ptr [ebp-3Ch],61h //对齐为8字节
0040B558 mov byte ptr [ebp-3Bh],62h
0040B55C mov byte ptr [ebp-3Ah],63h
0040B560 mov byte ptr [ebp-39h],64h
0040B564 mov byte ptr [ebp-38h],65h
0040B568 mov byte ptr [ebp-37h],66h
17: char m[6]; //树组长度为6,未初始化,编译器为其保留8字节
18: char n[7] = {'\0'}; //数组长度为7,第一个值赋值为0,其后编译器自动初始化为0
0040B56C mov byte ptr [ebp-4Ch],0 //对齐为8字节
0040B570 xor eax,eax
0040B572 mov dword ptr [ebp-4Bh],eax
0040B575 mov word ptr [ebp-47h],ax
19:
20:
21:
22:
23: return ret;
0040B579 mov eax,dword ptr [ebp-4]
24: }
0040B57C pop edi
0040B57D pop esi
0040B57E pop ebx
0040B57F mov esp,ebp
0040B581 pop ebp
0040B582 ret
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!