贴一段乘法的VC6.0的源码:
std::cout<<"Hello World"<<std::endl;//便于定位 int a,b,c,d,e,f; a=12; b=23; c=a*b; d=a*64; e=b*13; f=13*128; std::cout<<c<<std::endl; std::cout<<d<<std::endl; std::cout<<e<<std::endl; std::cout<<f<<std::endl; system("pause");//也可以看做一个定位字符
DEBUG版乘法运算反汇编分析
/* 0040158D . 68 24004700 PUSH OFFSET test1.??_C@_0M@FEIK@Hello?5World?$AA@ ; hello world 00401592 . 68 A0DE4700 PUSH OFFSET test1.std::cout 00401597 . E8 E9FCFFFF CALL test1.00401285 0040159C . 83C4 08 ADD ESP,8 0040159F . 8BC8 MOV ECX,EAX 004015A1 . E8 3AFCFFFF CALL test1.004011E0 004015A6 . C745 FC 0C000>MOV DWORD PTR SS:[EBP-4],0C ; a赋值为12 004015AD . C745 F8 17000>MOV DWORD PTR SS:[EBP-8],17 ; b赋值为23 004015B4 . 8B45 FC MOV EAX,DWORD PTR SS:[EBP-4] ; 将a值传到EAX中 004015B7 . 0FAF45 F8 IMUL EAX,DWORD PTR SS:[EBP-8] ; 将a值传到EAX中,并与b执行乘法运算 004015BB . 8945 F4 MOV DWORD PTR SS:[EBP-C],EAX ; 将EAX传到c中 004015BE . 8B4D FC MOV ECX,DWORD PTR SS:[EBP-4] ; 将a值传到ECX 004015C1 . C1E1 06 SHL ECX,6 ; 执行乘法操作,64是2的6次方 004015C4 . 894D F0 MOV DWORD PTR SS:[EBP-10],ECX ; 将ECX传到d中 004015C7 . 8B55 F8 MOV EDX,DWORD PTR SS:[EBP-8] ; 将b值传到EDX中 004015CA . 6BD2 0D IMUL EDX,EDX,0D ; 执行乘法运算 e=b*13 004015CD . 8955 EC MOV DWORD PTR SS:[EBP-14],EDX 004015D0 . C745 E8 80060>MOV DWORD PTR SS:[EBP-18],680 ; 将12*128所得结果传到 f中 004015D7 . 68 C8104000 PUSH test1.004010C8 004015DC . 8B45 F4 MOV EAX,DWORD PTR SS:[EBP-C] 004015DF . 50 PUSH EAX ; 输出c 004015E0 > . B9 A0DE4700 MOV ECX,OFFSET test1.std::cout 004015E5 . E8 15FBFFFF CALL test1.004010FF 004015EA . 8BC8 MOV ECX,EAX 004015EC . E8 EFFBFFFF CALL test1.004011E0 004015F1 . 68 C8104000 PUSH test1.004010C8 004015F6 . 8B4D F0 MOV ECX,DWORD PTR SS:[EBP-10] 004015F9 . 51 PUSH ECX ; 输出d 004015FA . B9 A0DE4700 MOV ECX,OFFSET test1.std::cout 004015FF . E8 FBFAFFFF CALL test1.004010FF 00401604 . 8BC8 MOV ECX,EAX 00401606 . E8 D5FBFFFF CALL test1.004011E0 0040160B . 68 C8104000 PUSH test1.004010C8 00401610 . 8B55 EC MOV EDX,DWORD PTR SS:[EBP-14] 00401613 . 52 PUSH EDX ; 输出e 00401614 . B9 A0DE4700 MOV ECX,OFFSET test1.std::cout 00401619 . E8 E1FAFFFF CALL test1.004010FF 0040161E . 8BC8 MOV ECX,EAX 00401620 > . E8 BBFBFFFF CALL test1.004011E0 00401625 . 68 C8104000 PUSH test1.004010C8 0040162A . 8B45 E8 MOV EAX,DWORD PTR SS:[EBP-18] 0040162D . 50 PUSH EAX ; 输出f 0040162E . B9 A0DE4700 MOV ECX,OFFSET test1.std::cout 00401633 . E8 C7FAFFFF CALL test1.004010FF 00401638 . 8BC8 MOV ECX,EAX 0040163A . E8 A1FBFFFF CALL test1.004011E0 0040163F 68 DB 68 ; pause 00401640 > . 1C004700 DD OFFSET test1.??_C@_05PBCN@pause?$AA@ ; ASCII "pause" 00401644 . E8 A7F30100 CALL test1.system ; \system */小结:
1 对于 为2的次幂的乘法,调用SHL指令
2 非2次幂的乘法 调用IMUL指令
Release版分析分开分析:
原文:http://blog.csdn.net/sunboyiris/article/details/23418329