您的位置:首页 > 其它

If条件分支语句的实现机制

2010-08-06 22:11 836 查看
(1)
int  main()
{
	int a=3,b=5;
	if (a<b) printf("%s","yes");
	return 0;
}

int a=3,b=5;
00CE13BE  mov         dword ptr [a],3 
00CE13C5  mov         dword ptr [b],5 
	if (a<b) printf("%s","yes");
00CE13CC  mov         eax,dword ptr [a] 
00CE13CF  cmp         eax,dword ptr [b]           ;a<b吗?
00CE13D2  jge         main+50h (0CE13F0h)         ;如果a>=b就跳转
00CE13D4  mov         esi,esp 
00CE13D6  push        offset string "yes" (0CE5740h) ;下面输出yes
00CE13DB  push        offset string "%s" (0CE573Ch) 
00CE13E0  call        dword ptr [__imp__printf (0CE82BCh)] 
00CE13E6  add         esp,8                          ;从堆栈弹出8字节


(2)

int  main()
{
	int a=3,b=5;
	if (a<b) printf("%s","yes");
	else printf("%s","no");
	return 0;
}


int a=3,b=5;
013313BE  mov         dword ptr [a],3 
013313C5  mov         dword ptr [b],5 
	if (a<b) printf("%s","yes");
013313CC  mov         eax,dword ptr [a] 
013313CF  cmp         eax,dword ptr [b]                   ;a<b吗
013313D2  jge         main+52h (13313F2h)                 ;a>=b则跳转
013313D4  mov         esi,esp 
013313D6  push        offset string "yes" (1335744h)      ;下面输出yes
013313DB  push        offset string "%s" (1335740h) 
013313E0  call        dword ptr [__imp__printf (13382BCh)] 
013313E6  add         esp,8 
013313E9  cmp         esi,esp 
013313EB  call        @ILT+310(__RTC_CheckEsp) (133113Bh) 
013313F0  jmp         main+6Eh (133140Eh)                 ;无条件跳转到结束
	else printf("%s","no");
013313F2  mov         esi,esp 
013313F4  push        offset string "no" (133573Ch)       ;下面输出no
013313F9  push        offset string "%s" (1335740h) 
013313FE  call        dword ptr [__imp__printf (13382BCh)] 
01331404  add         esp,8
01331407  cmp         esi,esp 
01331409  call        @ILT+310(__RTC_CheckEsp) (133113Bh) 
	return 0;
0133140E  xor         eax,eax
}


(3)



int  main()
{
	int a=3,b=5;
	if (a<b) printf("%s","yes");
	else if (a>b) printf("%s","no");
	else if (a==3) printf("%s","3");
	else if (a==5) printf("%s","5");
	else printf("%s","default");
	return 0;
}

int a=3,b=5;
00DC13BE  mov         dword ptr [a],3 
00DC13C5  mov         dword ptr [b],5 
	if (a<b) printf("%s","yes");
00DC13CC  mov         eax,dword ptr [a] 
00DC13CF  cmp         eax,dword ptr [b]                       ;a<b吗
00DC13D2  jge         main+55h (0DC13F5h)                     ;如果a>=b则跳转到0DC13F5h(下一个判断语句)
00DC13D4  mov         esi,esp 
00DC13D6  push        offset string "yes" (0DC5758h)          ;输出yes
00DC13DB  push        offset string "%s" (0DC5754h) 
00DC13E0  call        dword ptr [__imp__printf (0DC82BCh)] 
00DC13E6  add         esp,8 
00DC13E9  cmp         esi,esp 
00DC13EB  call        @ILT+310(__RTC_CheckEsp) (0DC113Bh) 
00DC13F0  jmp         main+0DFh (0DC147Fh)                    ;直接跳到结束
	else if (a>b) printf("%s","no");
00DC13F5  mov         eax,dword ptr [a] 
00DC13F8  cmp         eax,dword ptr [b]                       ;a b 比较
00DC13FB  jle         main+7Bh (0DC141Bh)                     ;a<=b则跳转到下一个判断语句
00DC13FD  mov         esi,esp 
00DC13FF  push        offset string "no" (0DC5750h)           ;输出no
00DC1404  push        offset string "%s" (0DC5754h) 
00DC1409  call        dword ptr [__imp__printf (0DC82BCh)] 
00DC140F  add         esp,8 
00DC1412  cmp         esi,esp 
00DC1414  call        @ILT+310(__RTC_CheckEsp) (0DC113Bh) 
00DC1419  jmp         main+0DFh (0DC147Fh)                    ;直接跳到结束
	else if (a==3) printf("%s","3");
00DC141B  cmp         dword ptr [a],3                         ;a和3 比较
00DC141F  jne         main+9Fh (0DC143Fh)                     ;a!=3则跳转
00DC1421  mov         esi,esp 
00DC1423  push        offset string "3" (0DC574Ch)            ;输出3
00DC1428  push        offset string "%s" (0DC5754h) 
00DC142D  call        dword ptr [__imp__printf (0DC82BCh)] 
00DC1433  add         esp,8 
00DC1436  cmp         esi,esp 
00DC1438  call        @ILT+310(__RTC_CheckEsp) (0DC113Bh) 
00DC143D  jmp         main+0DFh (0DC147Fh) 
	else if (a==5) printf("%s","5");
00DC143F  cmp         dword ptr [a],5                         ;a和5比较
00DC1443  jne         main+0C3h (0DC1463h)                    ;a!=5则跳转到下一个判断
00DC1445  mov         esi,esp 
00DC1447  push        offset string "5" (0DC5748h)            ;输出5
00DC144C  push        offset string "%s" (0DC5754h) 
00DC1451  call        dword ptr [__imp__printf (0DC82BCh)] 
00DC1457  add         esp,8 
00DC145A  cmp         esi,esp 
00DC145C  call        @ILT+310(__RTC_CheckEsp) (0DC113Bh) 
00DC1461  jmp         main+0DFh (0DC147Fh) 
	else printf("%s","default");
00DC1463  mov         esi,esp 
00DC1465  push        offset string "default" (0DC573Ch)        ;输出default
00DC146A  push        offset string "%s" (0DC5754h) 
00DC146F  call        dword ptr [__imp__printf (0DC82BCh)] 
00DC1475  add         esp,8 
00DC1478  cmp         esi,esp 
00DC147A  call        @ILT+310(__RTC_CheckEsp) (0DC113Bh) 
	return 0;
00DC147F  xor         eax,eax 
}


If(表达式1)语句和else if(表达式2)语句生成的汇编代码形式一样,每一段分支代码都有一条无条件跳转指令jmp到分支结尾的地方,这样就保证只执行一个分支,最后一个else语句不需要进行表达式的计算和判断。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: