win10 + vs2017
源码如下:
int main()
{
vector< int > numbers = { 1, 2, 3, 4, 5 };
for (auto num : numbers)
{
printf( "num = %d\n", num );
}
return 0;
}
汇编理解如下:
int main()
{
# 入栈
00933F63 sub esp,150h
00933F69 push ebx
00933F6A push esi
00933F6B push edi
00933F6C lea edi,[ebp-150h]
00933F72 mov ecx,54h
00933F77 mov eax,0CCCCCCCCh
00933F7C rep stos dword ptr es:[edi]
00933F7E mov eax,dword ptr [__security_cookie (093E004h)]
00933F83 xor eax,ebp
00933F85 mov dword ptr [ebp-4],eax
vector< int > numbers = { 1, 2, 3, 4, 5 };
// memset( &numbers, 0x00, sizeof ( numbers ) );
// sizeof ( numbers ) == 10h
00933F88 push 10h
00933F8A lea ecx,[numbers]
00933F8D call std::vector<int,std::allocator<int> >::__autoclassinit2 (09314ABh)
// [ebp-14Ch, ebp-138h) 这段内存是5个整形(20个字节),分别赋值为1,2,3,4,5
00933F92 mov dword ptr [ebp-14Ch],1
00933F9C mov dword ptr [ebp-148h],2
00933FA6 mov dword ptr [ebp-144h],3
00933FB0 mov dword ptr [ebp-140h],4
00933FBA mov dword ptr [ebp-13Ch],5
// 创建allocator对象,供后面vector构造使用,地址为 ebp-111h
00933FC4 lea ecx,[ebp-111h]
00933FCA call std::allocator<int>::allocator<int> (0931163h)
00933FCF push eax
// std::initializer_list<int> 构造
// initializer_list(const _Elem *_First_arg, const _Elem *_Last_arg)
00933FD0 lea eax,[ebp-138h]
00933FD6 push eax
00933FD7 lea ecx,[ebp-14Ch]
00933FDD push ecx
00933FDE lea ecx,[ebp-124h]
00933FE4 call std::initializer_list<int>::initializer_list<int> (09314A6h)
// vector 构造
// vector(initializer_list<_Ty> _Ilist, const _Alloc& _Al = _Alloc())
// _Al参数是在 00933FCF 位置压栈的
// _Ilist参数是按值传递,将成员_First和_Last分两次压栈,对应下面4行
00933FE9 mov edx,dword ptr [eax+4]
00933FEC push edx
00933FED mov eax,dword ptr [eax]
00933FEF push eax
00933FF0 lea ecx,[numbers]
00933FF3 call std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> > (0931168h)
for (auto num : numbers)
// std::vector<int,std::allocator<int> >::begin() 结果保存在 dword ptr [ebp-30h]
00933FF8 lea eax,[numbers]
00933FFB mov dword ptr [ebp-24h],eax
00933FFE mov ecx,dword ptr [ebp-24h]
00934001 call std::vector<int,std::allocator<int> >::_Unchecked_begin (09311FEh)
00934006 mov dword ptr [ebp-30h],eax
// std::vector<int,std::allocator<int> >::end() 结果保存在 dword ptr [ebp-3Ch]
00934009 mov ecx,dword ptr [ebp-24h]
0093400C call std::vector<int,std::allocator<int> >::_Unchecked_end (09312C1h)
00934011 mov dword ptr [ebp-3Ch],eax
// 跳转 for 循环的条件比较
00934014 jmp main+0BFh (093401Fh)
// 迭代器加1
00934016 mov eax,dword ptr [ebp-30h]
00934019 add eax,4
0093401C mov dword ptr [ebp-30h],eax
// eax = dword ptr [ebp-30h]
// eax 和 vector::end() 比较,如果相等则跳出循环
0093401F mov eax,dword ptr [ebp-30h]
00934022 cmp eax,dword ptr [ebp-3Ch]
00934025 je main+0E2h (0934042h)
// 将 dword ptr [ebp-30h] 迭代器指向的整形数值取出来,放到 dword ptr [ebp-48h]
00934027 mov eax,dword ptr [ebp-30h]
0093402A mov ecx,dword ptr [eax]
0093402C mov dword ptr [ebp-48h],ecx
{
printf( "num = %d\n", num );
// 从 dword ptr [ebp-48h] 取出整形数值,压栈
// 将 "num = %d\n" 压栈
// 调用 printf
0093402F mov eax,dword ptr [ebp-48h]
00934032 push eax
00934033 push offset string "num = %d\n" (093BC88h)
00934038 call _printf (093153Ch)
0093403D add esp,8
}
// 跳转到迭代器加1位置
00934040 jmp main+0B6h (0934016h)
return 0;
00934042 mov dword ptr [ebp-130h],0
// vecotr析构
0093404C lea ecx,[numbers]
0093404F call std::vector<int,std::allocator<int> >::~vector<int,std::allocator<int> > (0931078h)
// 将返回值0放入eax寄存器
00934054 mov eax,dword ptr [ebp-130h]
}
// 出栈
0093405A push edx
0093405B mov ecx,ebp
0093405D push eax
0093405E lea edx,ds:[93408Ch]
00934064 call @_RTC_CheckStackVars@8 (09313F7h)
00934069 pop eax
0093406A pop edx
0093406B pop edi
0093406C pop esi
0093406D pop ebx
0093406E mov ecx,dword ptr [ebp-4]
00934071 xor ecx,ebp
00934073 call @__security_check_cookie@4 (0931415h)
00934078 add esp,150h
0093407E cmp ebp,esp
00934080 call __RTC_CheckEsp (0931212h)
00934085 mov esp,ebp
00934087 pop ebp
00934088 ret