This question, which has not been thought of before, in C and pointers to this book, when it comes to switch is to give such a passage:
That is, if the condition of switch is an integer value? Seems to have been so used, but has not studied why. Ashamed...
The best way to study C is to see its assembly implementation, which is tested on Linux 64-bit.
Compile and run, output 10;
Then dump the target file.
intMain () {40052D: - Push %RBP 40052E: - theE5 mov%RSP,%RBP 400531: - theEcTen Sub $0x10,%rspint a=10; 400535: C7 $FC 0axx xx xxMovl $xa,-0x4(%RBP) switch (a)40053C:8B $FC MOV-0x4(%RBP),%eax 40053F: theF8 onCmp $X1,%eax 400542: About -Je40054b <main+0x1e>400544: theF8 0a CMP $Xa%eax 400547: About0e JE400557<main+0x2a>400549: EB -jmp400563<main+0x36>{case1:printf("1\n");40054B:bfGeneva . + xxmov $x400604,%edi 400550: E8 BB fe FF FF CALLQ400410<puts@plt> Break;400555: EB -jmp40056E <main+0x41>caseTen:printf("10\n");400557: BF . . + xxmov $x400606,%edi 40055C:e8 af FE FF FF CALLQ400410<puts@plt> Break;400561: EB 0b JMP40056E <main+0x41>default:printf("default\n");400563: BF 09 . + xxmov $x400609,%edi 400568: E8 a3 FE FF FF CALLQ400410<puts@plt> Break;40056D: -Nopreturn 0;40056E:b8xx xx xx xxmov $x0,%eax}
Mainly look at this code, is a switch code to expand a lot of assembly code
Switch(a)40053C:8B $FC MOV-0x4(%RBP),%eax 40053F: theF8 onCmp $X1,%eax 400542: About -Je40054b <main+0x1e>400544: theF80A CMP $Xa%eax 400547: About 0E JE400557<main+0x2a>400549: EB -jmp400563<main+0x36>
The value of a is stored in the register eax, then there are two comparison three challenges, which is the following two case conditions,
Is it possible to conclude that switch's implementation, as mentioned above, opens a deep understanding of the computer system, there is a section of the switch, which says that switch is implemented using a jump table, and my test code is not.
Aside from this question, the use of a comparison jump is not a little less efficient way to achieve. If there are more than n case comparisons, it's not dead. And the in-depth computer book is about a look-up method (well, it reminds me of the way FPGA chips are implemented).
Does GCC optimize itself based on case?
Next, add more case, write an experiment, and remove the print statement.
Then dump out the target file, as follows
intMain () {4004Ed -Push%RBP 4004Ee: - theE5 mov%RSP,%RBP intA=Ten;4004F1:c7 $Fc0Axx xx xxMovl $xa,-0x4(%RBP)Switch(a)4004F8:8B $FC MOV-0x4(%RBP),%eax 4004Fb: theE80A Sub $Xa%eax 4004Fe theF8 -Cmp $X7,%eax 400501: the 0C ja40050F <main+0x22>400503: theC0 mov%eax,%eax 400505: - 8BGenevaC5 A8 to +mov0x4005a8(,%rax,8),%rax 40050C:xx 40050D:FF E0 JMPQ*%Rax Case -: Break; Case -: Break;default: Break;40050F: -Nop400510: EB onjmp400513<main+0x26> Case the: Break; Case -: Break; Case -: Break;400512: -Nopdefault: Break; }return 0;400513: B8xx xx xx xxmov $X0,%eax}
The heinous thing came up. Look at this piece of code.
Switch(a)4004F8:8B $FC MOV-0x4(%RBP),%eax 4004Fb: theE80A Sub $Xa%eax 4004Fe theF8 -Cmp $X7,%eax 400501: the 0C ja40050F <main+0x22>400503: theC0 mov%eax,%eax 400505: - 8BGenevaC5 A8 to +mov0x4005a8(,%rax,8),%rax 40050C:xx 40050D:FF E0 JMPQ*%Rax
There is no way to compare jumps in turn.
F8 code, storing the value of a in the register EAX
FB code, subtract the value of EAX by 10; it's actually a mapping idea,
The following comparison and jump is to determine the value of eax more than 7, corresponding to the default situation.
The following MOV and JMPQ are important issues.
Look at this assembler code.
mov 0x4005a8 (,%rax,8),%rax
Sleepy Tomorrow again analysis.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Research on the seemingly simple switch problem