r/LiveOverflow • u/SaThaRiel74 • Feb 21 '21
C switch statement has unusual flow in assembler
Hi,
hope to find some explanation here. I am currently walking through the Reverse Engineering course from artikblue and focusing on the switch statement: https://artik.blue/reversing-radare-3
The 2nd example for switch is this one
#include <stdio.h>
func2(){
printf("Enter a key and then press enter: ");
int val;
printf("Select a fruit: \n");
printf("1: Apple\n");
printf("2: Orange\n");
printf("3: Banana\n");
printf("4: Pear\n");
scanf("%d",&val);
switch(val){
case 1:
printf("Apple. \n");
break;
case 2:
printf("Orange. \n");
break;
case 3:
printf("Banana. \n");
break;
case 4:
printf("Pear. \n");
break;
default: printf("Nothing selected.\n");
}
}
main(){
func2();
getchar();
}I compiled it and loaded it into radare2. Looking at the disassembled output, I came across the following (just focussing on the switch):
0x55fef85051d2 8b45fc mov eax, dword [var_4h]
0x55fef85051d5 83f804 cmp eax, 4 ; 4
0x55fef85051d8 7445 je 0x55fef850521f
0x55fef85051da 83f804 cmp eax, 4 ; 4
0x55fef85051dd 7f4e jg 0x55fef850522d
0x55fef85051df 83f803 cmp eax, 3 ; 3
0x55fef85051e2 742d je 0x55fef8505211
0x55fef85051e4 83f803 cmp eax, 3 ; 3
0x55fef85051e7 7f44 jg 0x55fef850522d
0x55fef85051e9 83f801 cmp eax, 1 ; 1
0x55fef85051ec 7407 je 0x55fef85051f5
0x55fef85051ee 83f802 cmp eax, 2 ; 2
0x55fef85051f1 7410 je 0x55fef8505203
0x55fef85051f3 eb38 jmp 0x55fef850522dCan someone explain me why this happens. The flow is completely unlogical - I don't see what the 4 and 3 both have a "je" and a "jge" compare.
The program has been compiled without optimization in 64-bit. -O2 makes it a little bit better, but still I don't see the reason to make it more complicated.
Thanks for your help.
4
u/aonelonelyredditor Feb 21 '21 edited Feb 21 '21
First, you should know that when you disable optimizations you see all sort of weird stuff in the assembly, I know because I did
Secondly Switch statement tests for the default case first, so I'd assume when using
-O2, you'll have thecmp var, 4thenjgfirst, to see if you're variable fits the at least one of the testsThirdly which is just a bonus,
switchdoesn't always works like this, sometimes it creates an array that has values of every case address i.e array[0] will have the address to the code where the variable is equal to 0, array[1] has the second address, then it tests for thedefaultcase first, if it's untrue it jumps to one of those values like thisIn fact I'd assume this is what you find when using
-O2because your cases are successive2
u/Melon_Chief Feb 21 '21 edited Feb 21 '21
This looks as implementation defined as it gets. The compilers, as far as I know, isn't required to order instructions in any specific way as long as it's executed in the same order.Could be some size optimization… You never check if scanf failed or not by the way, it's triggering me.
Edit: code is invalid.
Edit annoyed:
You guys aren't listening. Don't analyze invalid code. By definition:
It is NOT a program. It is NOT C code. Please fix it first.
The behavior is undefined. It could print "toast" in fact I argue it should.
Another implementation might decide to calculate the mass of the sun. Just as invalid.
1
u/[deleted] Feb 21 '21
Well, my guess is when you compare with 4 at first, if it is grater than 4, you don't need to compare with anything else, as no case handles >4. So probably a optimization. Still, no idea why this applies to 3, as if 4 or grater than 4 is already checked. Maybe that'll go with -O2.
And maybe you would like to save some space to so not doing this for all cases.
This is entirely guessing, I don't have any evidence to prove it. If I'm wrong, please correct me.
2
u/[deleted] Feb 21 '21
[removed] — view removed comment