#include <iostream> void main() { std::cout<<"hello world"<<std::endl; int a=8; int b; b=a>7?1:4; if (b==1) { std::cout<<"b equal 1"<<std::endl; } else { std::cout<<"b equal 2"<<std::endl; } system("pause"); }先贴一段代码,对于if语句的分析,首先理清想做什么。
判断语句有几种
1.条件表达式:表达式1?表达式2:表达式3
2.if语句
3.switch语句
首先来分析if语句吧:
DEBUG版:
0040117D . 68 3C404300 PUSH OFFSET test3.??_C@_0M@BNI@hello?5wo>; ASCII "hello world" 00401182 . 68 50B54300 PUSH OFFSET test3.std::cout 00401187 . E8 14FFFFFF CALL test3.004010A0 0040118C . 83C4 08 ADD ESP,8 ; a赋值为8 0040118F . 8BC8 MOV ECX,EAX ; 申明b 00401191 . E8 DDFEFFFF CALL test3.00401073 00401196 . C745 FC 08000>MOV DWORD PTR SS:[EBP-4],8 ; 传值8到 ebp-4 中 0040119D . 33C0 XOR EAX,EAX ; 清空EAX 0040119F . 837D FC 07 CMP DWORD PTR SS:[EBP-4],7 ; 8与7比较 004011A3 . 0F9EC0 SETLE AL ; 是否大于7 大于则AL为1 004011A6 . 48 DEC EAX ; 操作数自减1 EAX为0 004011A7 . 24 FD AND AL,0FD ; AL与 11111101作与运算 004011A9 . 83C0 04 ADD EAX,4 ; EAX加4 004011AC . 8945 F8 MOV DWORD PTR SS:[EBP-8],EAX ; 4 MOV 到 b中 004011AF . 837D F8 01 CMP DWORD PTR SS:[EBP-8],1 ; 比较指令 b值与1比较 004011B3 . 75 20 JNZ SHORT test3.004011D5 ; 与1不相等则跳转到 地址004011EE 输出b equal 2语句 004011B5 . 68 28104000 PUSH test3.00401028 004011BA . 68 30404300 PUSH OFFSET test3.??_C@_09MFLH@b?5equal?>; 相等 则输出b equal 1 004011BF . 68 50B54300 PUSH OFFSET test3.std::cout 004011C4 . E8 D7FEFFFF CALL test3.004010A0 004011C9 . 83C4 08 ADD ESP,8 004011CC . 8BC8 MOV ECX,EAX 004011CE . E8 A0FEFFFF CALL test3.00401073 004011D3 . EB 1E JMP SHORT test3.004011F3 004011D5 > 68 28104000 PUSH test3.00401028 004011DA . 68 24404300 PUSH OFFSET test3.??_C@_09DKAO@b?5equal?>; ASCII "b equal 2" 004011DF . 68 50B54300 PUSH OFFSET test3.std::cout 004011E4 . E8 B7FEFFFF CALL test3.004010A0 004011E9 . 83C4 08 ADD ESP,8 004011EC . 8BC8 MOV ECX,EAX 004011EE . E8 80FEFFFF CALL test3.00401073 004011F3 > 68 1C404300 PUSH OFFSET test3.??_C@_05PBCN@pause?$AA>; /command = "pause" 004011F8 . E8 737E0000 CALL test3.system ; \systemif语句主要调用了 CMP指令 和 JNZ指令。