汇编:C语言IF转汇编
if关键在在C语言中起着分支语句的作用,根据条件的判断,进而执行不同的程序语句块。
#include
int main(){
int a = 1;
int b = 2;
if (a > b){
printf("YYY");
}else{
printf("GGGG");
}
return 0;
}
对应汇编代码如下
00AE1A10 push ebp ;栈提升
00AE1A11 mov ebp,esp
00AE1A13 sub esp,0D8h
00AE1A19 push ebx ;保存现场
00AE1A1A push esi
00AE1A1B push edi
00AE1A1C lea edi,[ebp+FFFFFF28h] ;对新提升的栈初始化
00AE1A22 mov ecx,36h
00AE1A27 mov eax,0CCCCCCCCh
00AE1A2C rep stos dword ptr es:[edi]
00AE1A2E mov dword ptr [ebp-8],1 ;将数值保存到提升栈中
00AE1A35 mov dword ptr [ebp-14h],2
00AE1A3C mov eax,dword ptr [ebp-8] ;获取提升栈中的值
00AE1A3F cmp eax,dword ptr [ebp-14h] ;两个值进行比较
00AE1A42 jle 00AE1A5D ;如果第一个值小于等于第二个值,那么将跳转到00AE1A5D继续执行,也就是else处
00AE1A44 mov esi,esp
00AE1A46 push 0AE58A8h ;函数变量入栈
00AE1A4B call dword ptr ds:[00AE9114h] ;函数调用
00AE1A51 add esp,4 ;结束函数调用后,堆栈平衡
00AE1A54 cmp esi,esp
00AE1A56 call 00AE113B
00AE1A5B jmp 00AE1A74 ;跳转到else代码之后,在00AE1A74处继续执行指令
00AE1A5D mov esi,esp
00AE1A5F push 0AE58ACh
00AE1A64 call dword ptr ds:[00AE9114h]
00AE1A6A add esp,4
00AE1A6D cmp esi,esp
00AE1A6F call 00AE113B
00AE1A74 xor eax,eax ;函数分支功能结束
00AE1A76 pop edi
00AE1A77 pop esi
00AE1A78 pop ebx
00AE1A79 add esp,0D8h
00AE1A7F cmp ebp,esp
00AE1A81 call 00AE113B
00AE1A86 mov esp,ebp
00AE1A88 pop ebp
00AE1A89 ret
经过上述代码可知,C语言与汇编语言的分支区别:
C语言:满足某些条件时,进行执行大括号内的程序代码
汇编语言:翻译成与C语言相反的逻辑,在达到某些条件下,跳过这些程序代码
下述两种if格式对应汇编,原理与上述原理相同
if(表达式){
...
}
if(表达式){
...
}else if(表达式){
...
}else{
...
}