1 #include
2
3 using std::cout;
4 using std::endl;
5
6 // 交换x和y的值并返回两者中较大的值
7 int swap(int &x, int &y);
8
9 int main()
10 {
11 int x = 1, y = 2;
12 cout <" " <" " < endl;
13
14 return 0;
15 }
16
17 int swap(int &x, int &y)
18 {
19 int temp = x;
20 x = y;
21 y = temp;
22
23 return (x > y ? x : y);
24 }
swap()函数的功能是: 交换x和y的值并返回两者中较大的值 . 按照我的思路, 程序应该输出 2 2 1 但是最后的结果竟然是: 2 1 2
1 0x004016cd <+29>: call 0x409810 <__main>
2 0x004016d2 <+34>: movl $0x1,-0x1c(%ebp)
3 0x004016d9 <+41>: movl $0x2,-0x20(%ebp)
4 0x004016e0 <+48>: mov -0x20(%ebp),%ebx
5 0x004016e3 <+51>: mov -0x1c(%ebp),%esi
6 0x004016e6 <+54>: lea -0x20(%ebp),%eax
7 0x004016e9 <+57>: mov %eax,0x4(%esp)
8 0x004016ed <+61>: lea -0x1c(%ebp),%eax
9 0x004016f0 <+64>: mov %eax,(%esp)
10 0x004016f3 <+67>: call 0x40175c int&, int&)>
11 0x004016f8 <+72>: mov %eax,(%esp)
12 0x004016fb <+75>: mov $0x464fa0,%ecx
13 0x00401700 <+80>: call 0x438df0 <_ZNSolsEi>
14 0x00401705 <+85>: sub $0x4,%esp
15 0x00401708 <+88>: movl $0x466064,0x4(%esp)
16 0x00401710 <+96>: mov %eax,(%esp)
17 0x00401713 <+99>: call 0x458000 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc>
18 0x00401718 <+104>: mov %esi,(%esp)
19 0x0040171b <+107>: mov %eax,%ecx
20 0x0040171d <+109>: call 0x438df0 <_ZNSolsEi>
21 0x00401722 <+114>: sub $0x4,%esp
22 0x00401725 <+117>: movl $0x466064,0x4(%esp)
23 0x0040172d <+125>: mov %eax,(%esp)
24 0x00401730 <+128>: call 0x458000 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc>
25 0x00401735 <+133>: mov %ebx,(%esp)
26 0x00401738 <+136>: mov %eax,%ecx
27 0x0040173a <+138>: call 0x438df0 <_ZNSolsEi>
28 0x0040173f <+143>: sub $0x4,%esp
29 0x00401742 <+146>: mov $0x0,%eax
30 0x00401747 <+151>: jmp 0x401751 161>
31 0x00401749 <+153>: mov %eax,(%esp)
32 0x0040174c <+156>: call 0x40c570 <_Unwind_Resume>
33 0x00401751 <+161>: lea -0xc(%ebp),%esp
34 0x00401754 <+164>: pop %ecx
35 0x00401755 <+165>: pop %ebx
36 0x00401756 <+166>: pop %esi
37 0x00401757 <+167>: pop %ebp
38 0x00401758 <+168>: lea -0x4(%ecx),%esp
39 0x0040175b <+171>: ret
把y的值放到距离栈底1c字节的位置.
把x的值放到距离栈底20字节的位置.
把x的值放到距离栈底4字节的位置.