在vs2005下建立vc++的win32 控制台应用程序,设置stdafx.h不必要包含,设置多字符集,设置汇编输出/FAs。代码和asm解析如下:
实际产生9500多行asm代码,只取main函数直接对应的代码和模版函数实例化后编译获得的asm代码,间接代码省略。
#include <iostream>
#include <vector>
template<typename T>
T max_test(T &t1, T &t2)
{
return (t1 > t2)? t1:t2;
}
int main(int argc, char* argv[])
{
std::cout << "hello world!/n";
int i;
std::cin >> i;
std::vector<int> vi;
std::vector<double> vd;
vi.push_back(i);
std::cin >> i;
vi.push_back(i);
std::cout << "max is " << max_test(vi[0], vi[1]) << std::endl;
std::cin >> i;
return 0;
}
; 10 : std::cout << "hello world!/n";
push OFFSET ??_C@_0O@OBPALAEI@hello?5world?$CB?6?$AA@
mov eax, DWORD PTR __imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A
push eax
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char> >
add esp, 8
; 11 : int i;
; 12 : std::cin >> i;
mov esi, esp
lea eax, DWORD PTR _i$[ebp]
push eax
mov ecx, DWORD PTR __imp_?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A
call DWORD PTR __imp_??5?$basic_istream@DU?$char_traits@D@std@@@std@@QAEAAV01@AAH@Z
cmp esi, esp
call __RTC_CheckEsp
; 13 : std::vector<int> vi;
lea ecx, DWORD PTR _vi$[ebp]
call ??0?$vector@HV?$allocator@H@std@@@std@@QAE@XZ ; std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> >
mov DWORD PTR __$EHRec$[ebp+8], 0
; 14 : std::vector<double> vd;
lea ecx, DWORD PTR _vd$[ebp]
call ??0?$vector@NV?$allocator@N@std@@@std@@QAE@XZ ; std::vector<double,std::allocator<double> >::vector<double,std::allocator<double> >
mov BYTE PTR __$EHRec$[ebp+8], 1
; 15 : vi.push_back(i);
lea eax, DWORD PTR _i$[ebp]
push eax
lea ecx, DWORD PTR _vi$[ebp]
call ?push_back@?$vector@HV?$allocator@H@std@@@std@@QAEXABH@Z ; std::vector<int,std::allocator<int> >::push_back
; 16 : std::cin >> i;
mov esi, esp
lea eax, DWORD PTR _i$[ebp]
push eax
mov ecx, DWORD PTR __imp_?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A
call DWORD PTR __imp_??5?$basic_istream@DU?$char_traits@D@std@@@std@@QAEAAV01@AAH@Z
cmp esi, esp
call __RTC_CheckEsp
; 17 : vi.push_back(i);
lea eax, DWORD PTR _i$[ebp]
push eax
lea ecx, DWORD PTR _vi$[ebp]
call ?push_back@?$vector@HV?$allocator@H@std@@@std@@QAEXABH@Z ; std::vector<int,std::allocator<int> >::push_back
; 18 : std::cout << "max is " << max_test(vi[0], vi[1]) << std::endl;
mov esi, esp
mov eax, DWORD PTR __imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z
push eax
push 1
lea ecx, DWORD PTR _vi$[ebp]
call ??A?$vector@HV?$allocator@H@std@@@std@@QAEAAHI@Z ; std::vector<int,std::allocator<int> >::operator[]
push eax
push 0
lea ecx, DWORD PTR _vi$[ebp]
call ??A?$vector@HV?$allocator@H@std@@@std@@QAEAAHI@Z ; std::vector<int,std::allocator<int> >::operator[]
push eax
call ??$max_test@H@@YAHAAH0@Z ; max_test<int>
add esp, 8
mov edi, esp
push eax
push OFFSET ??_C@_07FPFPDICH@max?5is?5?$AA@
mov ecx, DWORD PTR __imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A
push ecx
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char> >
add esp, 8
mov ecx, eax
call DWORD PTR __imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z
cmp edi, esp
call __RTC_CheckEsp
mov ecx, eax
call DWORD PTR __imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z
cmp esi, esp
call __RTC_CheckEsp
_TEXT SEGMENT
tv65 = -196 ; size = 4
_t1$ = 8 ; size = 4
_t2$ = 12 ; size = 4
??$max_test@H@@YAHAAH0@Z PROC ; max_test<int>, COMDAT;实例化后的模版函数与一般函数没有区别
; 5 : {
push ebp
mov ebp, esp
sub esp, 196 ; 000000c4H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-196]
mov ecx, 49 ; 00000031H
mov eax, -858993460 ; ccccccccH
rep stosd
; 6 : return (t1 > t2)? t1:t2;
mov eax, DWORD PTR _t1$[ebp]
mov ecx, DWORD PTR _t2$[ebp]
mov edx, DWORD PTR [eax]
cmp edx, DWORD PTR [ecx]
jle SHORT $LN3@max_test
mov eax, DWORD PTR _t1$[ebp]
mov ecx, DWORD PTR [eax]
mov DWORD PTR tv65[ebp], ecx
jmp SHORT $LN4@max_test
$LN3@max_test:
mov edx, DWORD PTR _t2$[ebp]
mov eax, DWORD PTR [edx]
mov DWORD PTR tv65[ebp], eax
$LN4@max_test:
mov eax, DWORD PTR tv65[ebp]
; 7 : }
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
??$max_test@H@@YAHAAH0@Z ENDP ; max_test<int>
_TEXT ENDS