知识点: ?for循环生成代码1 ?for循环生成代码2 ?inc指令 一、一般情况下的for循环汇编代码分析 for (int i=1;i<=10;i++) { printf("%d,",i); } //00401003 |. 51 PUSH ECX ; sub esp,4;int i; //00401004 |. C745 FC 01000>MOV DWORD PTR SS:[EBP-4],1 ; i=1; //0040100B |. EB 09 JMP SHORT for.00401016 //0040100D |> 8B45 FC /MOV EAX,DWORD PTR SS:[EBP-4] //00401010 |. 83C0 01 |ADD EAX,1 //00401013 |. 8945 FC |MOV DWORD PTR SS:[EBP-4],EAX ; i++;i=i+1; //00401016 |> 837D FC 0A CMP DWORD PTR SS:[EBP-4],0A //0040101A |. 7F 14 |JG SHORT for.00401030 ; i>10 跳出for循环 //0040101C |. 8B4D FC |MOV ECX,DWORD PTR SS:[EBP-4] ; ecx=i //0040101F |. 51 |PUSH ECX ; /<%d> //00401020 |. 68 F4204000 |PUSH for.004020F4 ; |format = "%d," //00401025 |. FF15 A0204000 |CALL DWORD PTR DS:[<&MSVCR90.printf>] ; \printf //0040102B |. 83C4 08 |ADD ESP,8 ; esp=esp+8 //0040102E |.^ EB DD \JMP SHORT for.0040100D //00401030 |> 33C0 XOR EAX,EAX ; return 0; 二、速度优化下的for循环汇编代码分析 //最小化优化 //00401000 /$ 56 PUSH ESI //00401001 |. 33F6 XOR ESI,ESI ; i=0; //00401003 |. 46 INC ESI ; i=1; //00401004 |> 56 /PUSH ESI ; /<%d> //00401005 |. 68 F4204000 |PUSH for.004020F4 ; |format = "%d," //0040100A |. FF15 A0204000 |CALL DWORD PTR DS:[<&MSVCR90.printf>] ; \printf //00401010 |. 46 |INC ESI ; i++ //00401011 |. 83FE 0A |CMP ESI,0A ; if (i<=10) goto 401004 //00401014 |. 59 |POP ECX //00401015 |. 59 |POP ECX //00401016 |.^ 7E EC \JLE SHORT for.00401004 //00401018 |. 33C0 XOR EAX,EAX //0040101A |. 5E POP ESI //0040101B \. C3 RETN ; 33-1B //最大化速度优化 //00401000 /$ 56 PUSH ESI //00401001 |. 57 PUSH EDI //00401002 |. 8B3D A0204000 MOV EDI,DWORD PTR DS:[<&MSVCR90.printf>] ; MSVCR90.printf //00401008 |. BE 01000000 MOV ESI,1 ; int i=1; //0040100D |. 8D49 00 LEA ECX,DWORD PTR DS:[ECX] ; ecx=ecx //00401010 |> 56 /PUSH ESI //00401011 |. 68 F4204000 |PUSH for.004020F4 ; ASCII "%d," //00401016 |. FFD7 |CALL EDI ; call dword ptr [printf] //00401018 |. 46 |INC ESI ; i++; //00401019 |. 83C4 08 |ADD ESP,8 //0040101C |. 83FE 0A |CMP ESI,0A ; 0B //0040101F |.^ 7E EF \JLE SHORT for.00401010 ; if (i<=10) 继续循环goto 401010 //00401021 |. 5F POP EDI //00401022 |. 33C0 XOR EAX,EAX //00401024 |. 5E POP ESI //00401025 \. C3 RETN
时间: 2024-11-07 17:54:43