If条件分支语句的实现机制
2010-08-06 22:11
836 查看
(1)
(2)
(3)
If(表达式1)语句和else if(表达式2)语句生成的汇编代码形式一样,每一段分支代码都有一条无条件跳转指令jmp到分支结尾的地方,这样就保证只执行一个分支,最后一个else语句不需要进行表达式的计算和判断。
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语句不需要进行表达式的计算和判断。
相关文章推荐
- 第23讲-用if语句实现分支结构-练习
- 23 用if语句实现分支结构
- 2013C++-第8周项目——用if语句实现分支结构程序设计
- if条件语句实战单分支结构
- 用于多分支的条件语句(if-else语句)
- 23 用if语句实现分支结构 项目(1: 被3或者5整除的数) (2:乱玩数字 ) (3:两段函数求值 ) (4:三角公式求值 ) (5:我的加班费 ) (6:前导0的数字
- 第二十三讲 用if语句实现分支结构
- python的条件分支语句(if)
- 编写一个计算某个月份的天数程序,请用if-else条件语句实现。要求根据用户输入的月份,判断出月份所包含的天数。
- if条件语句---双分支/多分支
- 实现1+2+3+...+n。要求不能使用乘除法,for,while,if,else,switch,case,等关键字及条件判断语句。
- 25_Shell语言――――if条件判断之组合判断(与、或、非)和多分支if语句
- 不使用乘除法,for,while,if,else,switch,case,条件判断语句(A?B:C) 实现:1+2+....+n
- lesson 23 if语句实现分支结构
- Week8-用if语句实现分支结构程序设计
- 自制编程语言CXScript(三) 如何实现if语句、else语句并实现分支跳转
- JAVA if条件语句,switch多分支结构,与逻辑运算符
- Python 【条件分支】(if 语句) 学习笔记
- 第二十三课 用if语句实现分支结构 【项目1-6】
- 用单分支条件语句(if语句)求最值