我只是写了一个简单的程序来学习c编程
1 #include<stdio.h>
2
3 int main()
4 {
5 int a = 5;
6 switch(a)
7 {
8 case 0:
9 {
10 ;
11 int a = 10;
12 printf("%d\n",a);
13 break;
14 }
15 default :
16 printf("%d",a);
17
18 }
19 return 0;
20 }
输出: 5 当我忘记大括号时,它变成:
8 case 0:
9
10 ;
11 int a = 10;
12 printf("%d\n",a);
13 break;
14
输出: 0
我对此有点困惑,并尝试编译和调试:
- 1 0x000000000040051c <+0>: push %rbp |- 1 0x000000000040051c <+0>: push %rbp
| 2 0x000000000040051d <+1>: mov %rsp,%rbp || 2 0x000000000040051d <+1>: mov %rsp,%rbp
| 3 0x0000000000400520 <+4>: sub $0x10,%rsp || 3 0x0000000000400520 <+4>: sub $0x10,%rsp
| 4 => 0x0000000000400524 <+8>: movl $0x5,-0x8(%rbp) || 4 => 0x0000000000400524 <+8>: movl $0x5,-0x8(%rbp)
| 5 0x000000000040052b <+15>: mov -0x8(%rbp),%eax || 5 0x000000000040052b <+15>: mov -0x8(%rbp),%eax
| 6 0x000000000040052e <+18>: test %eax,%eax || 6 0x000000000040052e <+18>: test %eax,%eax
| 7 0x0000000000400530 <+20>: jne 0x40054f <main+51> || 7 0x0000000000400530 <+20>: jne 0x40054f <main+51>
| 8 0x0000000000400532 <+22>: movl $0xa,-0x4(%rbp) || 8 0x0000000000400532 <+22>: movl $0xa,-0x4(%rbp)
9 0x0000000000400539 <+29>: mov -0x4(%rbp),%eax | 9 0x0000000000400539 <+29>: mov -0x4(%rbp),%eax
10 0x000000000040053c <+32>: mov %eax,%esi | 10 0x000000000040053c <+32>: mov %eax,%esi
11 0x000000000040053e <+34>: mov $0x400614,%edi | 11 0x000000000040053e <+34>: mov $0x400614,%edi
12 0x0000000000400543 <+39>: mov $0x0,%eax | 12 0x0000000000400543 <+39>: mov $0x0,%eax
13 0x0000000000400548 <+44>: callq 0x4003f0 <printf@plt> | 13 0x0000000000400548 <+44>: callq 0x4003f0 <printf@plt>
14 0x000000000040054d <+49>: jmp 0x400563 <main+71> | 14 0x000000000040054d <+49>: jmp 0x400563 <main+71>
15 0x000000000040054f <+51>: mov -0x8(%rbp),%eax | 15 0x000000000040054f <+51>: mov -0x4(%rbp),%eax
16 0x0000000000400552 <+54>: mov %eax,%esi | 16 0x0000000000400552 <+54>: mov %eax,%esi
17 0x0000000000400554 <+56>: mov $0x400618,%edi | 17 0x0000000000400554 <+56>: mov $0x400618,%edi
18 0x0000000000400559 <+61>: mov $0x0,%eax | 18 0x0000000000400559 <+61>: mov $0x0,%eax
19 0x000000000040055e <+66>: callq 0x4003f0 <printf@plt> | 19 0x000000000040055e <+66>: callq 0x4003f0 <printf@plt>
20 0x0000000000400563 <+71>: mov $0x0,%eax | 20 0x0000000000400563 <+71>: mov $0x0,%eax
21 0x0000000000400568 <+76>: leaveq | 21 0x0000000000400568 <+76>: leaveq
+ 22 +-- 2 lines: 0x0000000000400569 <+77>: retq ----------------------|+ 22 +-- 2 lines: 0x0000000000400569 <+77>: retq ---------------------
有点不同但很重要:
$ diff with.txt without.txt
15c15
< 0x000000000040054f <+51>: mov -0x8(%rbp),%eax
---
> 0x000000000040054f <+51>: mov -0x4(%rbp),%eax
提前致谢。
更新:
我学到的一课gcc -Wall
总是一个很好的做法