#include <stdio.h>
int main()
{
int nNum = 0, nSum = 0;
for ( nNum = 1; nNum <= 100; nNum ++ )
{
nSum += nNum;
}
printf("nSum = %d \r\n", nSum);
return 0;
}
.text:00401028 mov [ebp+nNum], 0
.text:0040102F mov [ebp+nSum], 0
.text:00401036 mov [ebp+nNum], 1
.text:0040103D jmp short LOC_CMP
.text:0040103F ; ---------------------------------------------------------
.text:0040103F
.text:0040103F LOC_STEP: ; CODE XREF: _main+47j
.text:0040103F mov eax, [ebp+nNum]
.text:00401042 add eax, 1
.text:00401045 mov [ebp+nNum], eax
.text:00401048
.text:00401048 LOC_CMP: ; CODE XREF: _main+2Dj
.text:00401048 cmp [ebp+nNum], 64h
.text:0040104C jg short LOC_ENDFOR
.text:0040104E mov ecx, [ebp+nSum]
.text:00401051 add ecx, [ebp+nNum]
.text:00401054 mov [ebp+nSum], ecx
.text:00401057 jmp short LOC_STEP
.text:00401059 ; ---------------------------------------------------------
.text:00401059
.text:00401059 LOC_ENDFOR: ; CODE XREF: _main+3Cj
.text:00401059 mov edx, [ebp+nSum]
.text:0040105C push edx
.text:0040105D push offset Format ; "nSum = %d \r\n"
.text:00401062 call _printf
.text:00401067 add esp, 8
.text:0040106A xor eax, eax
初始化循环变量
jmp LOC_CMP
LOC_STEP:
; 修改循环变量
LOC_CMP:
; 循环变量的判断
jxx LOC_ENDFOR
; 循环体
jmp LOC_STEP
LOC_ENDOF:
#
int main()
{
int nNum = 1, nSum = 0;
do
{
nSum += nNum;
nNum ++;
} while ( nNum <= 100 );
printf("nSum = %d \r\n", nSum);
return 0;
}
.text:00401028 mov [ebp+nNum], 1
.text:0040102F mov [ebp+nSum], 0
.text:00401036
.text:00401036 LOC_DO: ; CODE XREF: _main+3Cj
.text:00401036 mov eax, [ebp+nSum]
.text:00401039 add eax, [ebp+nNum]
.text:0040103C mov [ebp+nSum], eax
.text:0040103F mov ecx, [ebp+nNum]
.text:00401042 add ecx, 1
.text:00401045 mov [ebp+nNum], ecx
.text:00401048 cmp [ebp+nNum], 64h
.text:0040104C jle short LOC_DO
.text:0040104E mov edx, [ebp+nSum]
.text:00401051 push edx
.text:00401052 push offset Format ; "nSum = %d \r\n"
.text:00401057 call _printf
.text:0040105C add esp, 8
.text:0040105F xor eax, eax
初始化循环变量
LOC_DO:
; 执行循环体
; 修改循环变量
; 循环变量的比较
Jxx LOC_DO
#
int main()
{
int nNum = 1, nSum = 0;
while ( nNum <= 100 )
{
nSum += nNum;
nNum ++;
}
printf("nSum = %d \r\n", nSum);
return 0;
}
.text:00401028 mov [ebp+nNum], 1
.text:0040102F mov [ebp+nSum], 0
.text:00401036
.text:00401036 LOC_WHILE: ; CODE XREF: _main+3Ej
.text:00401036 cmp [ebp+nNum], 64h
.text:0040103A jg short LOC_WHILEEND
.text:0040103C mov eax, [ebp+nSum]
.text:0040103F add eax, [ebp+nNum]
.text:00401042 mov [ebp+nSum], eax
.text:00401045 mov ecx, [ebp+nNum]
.text:00401048 add ecx, 1
.text:0040104B mov [ebp+nNum], ecx
.text:0040104E jmp short LOC_WHILE
.text:00401050 ; -----------------------------------------------------------
.text:00401050
.text:00401050 LOC_WHILEEND: ; CODE XREF: _main+2Aj
.text:00401050 mov edx, [ebp+nSum]
.text:00401053 push edx
.text:00401054 push offset Format ; "nSum = %d \r\n"
.text:00401059 call _printf
.text:0040105E add esp, 8
.text:00401061 xor eax, eax
初始化循环变量等
LOC_WHILE:
cmp xxx, xxx
jxx LOC_WHILEEND
; 循环体
jmp LOC_WHILE
LOC_WHILEEND:
对于for循环、do循环和while循环这3种循环而言,do循环的效率显然高些,而while循环相对来说比for循环效率又高些。