1、编写两个简单的条件语句来反汇编一下看最终机器是如何工作的,以下示例是在XCode中反汇编的,即AT&T汇编,8086或win32也是相同的原理
1.1、if…else…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| #include <iostream>
#include <cmath>
using namespace std;
int main(){
int a = 0;
int b = 0;
if (a > 0){
b = 1;
}else{
b = 2;
}
return 0;
} |
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int a = 0;
int b = 0;
if (a > 0){
b = 1;
}else{
b = 2;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| cplus`main:
0x100000f70 <+0>: pushq %rbp
0x100000f71 <+1>: movq %rsp, %rbp
0x100000f74 <+4>: movl $0x0, -0x4(%rbp)
0x100000f7b <+11>: movl $0x0, -0x8(%rbp);int a
0x100000f82 <+18>: movl $0x0, -0xc(%rbp);int b
;if...else...开始
;1、小于或等于则跳转到3【0x100000f9f】
0x100000f89 <+25>: cmpl $0x0, -0x8(%rbp)
0x100000f8d <+29>: jle 0x100000f9f
;2.1、符合条件则执行if条件下的代码
0x100000f93 <+35>: movl $0x1, -0xc(%rbp);b=1
;2.2、执行结束后直接跳转到结尾
0x100000f9a <+42>: jmp 0x100000fa6
;3、else条件下的代码
0x100000f9f <+47>: movl $0x2, -0xc(%rbp);b=2
;if...else...结束
0x100000fa6 <+54>: xorl %eax, %eax
0x100000fa8 <+56>: popq %rbp
0x100000fa9 <+57>: retq |
cplus`main:
0x100000f70 <+0>: pushq %rbp
0x100000f71 <+1>: movq %rsp, %rbp
0x100000f74 <+4>: movl $0x0, -0x4(%rbp)
0x100000f7b <+11>: movl $0x0, -0x8(%rbp);int a
0x100000f82 <+18>: movl $0x0, -0xc(%rbp);int b
;if...else...开始
;1、小于或等于则跳转到3【0x100000f9f】
0x100000f89 <+25>: cmpl $0x0, -0x8(%rbp)
0x100000f8d <+29>: jle 0x100000f9f
;2.1、符合条件则执行if条件下的代码
0x100000f93 <+35>: movl $0x1, -0xc(%rbp);b=1
;2.2、执行结束后直接跳转到结尾
0x100000f9a <+42>: jmp 0x100000fa6
;3、else条件下的代码
0x100000f9f <+47>: movl $0x2, -0xc(%rbp);b=2
;if...else...结束
0x100000fa6 <+54>: xorl %eax, %eax
0x100000fa8 <+56>: popq %rbp
0x100000fa9 <+57>: retq
1.2、for(…;…;…)
1
2
3
4
5
6
7
8
9
10
11
12
| #include <iostream>
#include <cmath>
using namespace std;
int main(){
int a = 0;
for (int i = 0; i < 5; i++){
a = 1;
}
return 0;
} |
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int a = 0;
for (int i = 0; i < 5; i++){
a = 1;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| cplus`main:
0x100000f70 <+0>: pushq %rbp
0x100000f71 <+1>: movq %rsp, %rbp
0x100000f74 <+4>: movl $0x0, -0x4(%rbp)
0x100000f7b <+11>: movl $0x0, -0x8(%rbp);int a = 0
0x100000f82 <+18>: movl $0x0, -0xc(%rbp);int i = 0
;----for----start
;1、for循环开始,首先进行条件判断【不符合则跳转到结尾地址0x100000fa8】
0x100000f89 <+25>: cmpl $0x5, -0xc(%rbp);if(i < 5)
0x100000f8d <+29>: jge 0x100000fa8;
;2、for循环中的业务代码
0x100000f93 <+35>: movl $0x1, -0x8(%rbp);(a=1)
;3、for循环中修改条件值
0x100000f9a <+42>: movl -0xc(%rbp), %eax;取出i的值
0x100000f9d <+45>: addl $0x1, %eax;i++
0x100000fa0 <+48>: movl %eax, -0xc(%rbp);回写到变量
;4、for循环再次判断
0x100000fa3 <+51>: jmp 0x100000f89【转到循环开始位置】
;----for----end
0x100000fa8 <+56>: xorl %eax, %eax
0x100000faa <+58>: popq %rbp
0x100000fab <+59>: retq |
cplus`main:
0x100000f70 <+0>: pushq %rbp
0x100000f71 <+1>: movq %rsp, %rbp
0x100000f74 <+4>: movl $0x0, -0x4(%rbp)
0x100000f7b <+11>: movl $0x0, -0x8(%rbp);int a = 0
0x100000f82 <+18>: movl $0x0, -0xc(%rbp);int i = 0
;----for----start
;1、for循环开始,首先进行条件判断【不符合则跳转到结尾地址0x100000fa8】
0x100000f89 <+25>: cmpl $0x5, -0xc(%rbp);if(i < 5)
0x100000f8d <+29>: jge 0x100000fa8;
;2、for循环中的业务代码
0x100000f93 <+35>: movl $0x1, -0x8(%rbp);(a=1)
;3、for循环中修改条件值
0x100000f9a <+42>: movl -0xc(%rbp), %eax;取出i的值
0x100000f9d <+45>: addl $0x1, %eax;i++
0x100000fa0 <+48>: movl %eax, -0xc(%rbp);回写到变量
;4、for循环再次判断
0x100000fa3 <+51>: jmp 0x100000f89【转到循环开始位置】
;----for----end
0x100000fa8 <+56>: xorl %eax, %eax
0x100000faa <+58>: popq %rbp
0x100000fab <+59>: retq