Hi Experts,
What is the key difference between if/else and swtich case statements ?
How the modern ARM GCC compilers handles it ?
It depends. Straightforwardly if's are a test and branch whereas switches use a table for dense ranges - but there are a very large number of variations.
You can see what happens for things you are interested in by using the -S option and looking at the .s assembler file that comes out.
You might also like to look at this free book
http://beginners.re/Reverse_Engineering_for_Beginners-en.pdf
The site "Reverse Engineering for Beginners" free book has been set up by the author about it
In addition to what daith said, switch statements lend themselves to being more easily optimised into jump/branch tables, rather than a series of compares, which can improve performance in some use cases.
Hi Daith,
I am fond of that book.
Thanks for the mention.
Regards,
Tehcguyz
If you really want to know what the compiler does about them you should start looking up things like static single assignment (SSA) and phi functions. Compilers can be a bit tricky in some areas but the source for how things like conditionals are handled and optimized for each particular architecture is really quite readable and almost user friendly nowadays.
Hello,
the most important difference would be the order of which predicates are evaluated.
As for the switch, the evaluations are done at the same, that is, there is no priority.
And the execution time will be the same for any conditions.
As for the if, the evaluations are done in order.
And the execution time will depend on the timing when the condition matches in the if-then-else statement.
If the conditions of if and else are exclusive, the if can be replaced with the switch.
Best regards,
Yasuhiko Koumoto.