Check the code below:
#include <stdio.h>
using namespace std;
char getChar(int x, int y)
{
char c;
unsigned int a = x;
(a + y > 10) ? (c = 1) : (c = 2);
// once x=7, a is uint and then transfer it into 111h
// y=-8
// when adding uint and int, both of variables would be transferred into uint
// so when adding 111h and fffffff8h, the result is 4294967295h
// 4294967295h>10. so c = 1
std::cout << "a+y=" << (a + y) << endl;
std::cout << "a= \n" << a << "x=\n" << x << "y=\n" << y << endl;
printf("c1 = %d\n", c);
return c;
}
int main(void)
{
char c3 = getChar(7,-7); // c3 would be 2
char c4 = getChar(7, -8);// c4 would be 1
printf("c3 = %d\n", c3);
printf("c1 = %d\n", c4);
}
// once x=7, a is uint and then transfer it into 111h
// y=-8
// when adding uint and int, both of variables would be transferred into uint
// so when adding 111h and fffffff8h, the result is 4294967295h
// 4294967295h>10. so c = 1
// once x=7, a is uint and then transfer it into 111h
// y=-7
// when adding uint and int, both of variables would be transferred into uint
// so when adding 111h and fffffff9h, the result is 4294967296h, overflow
// the result is 0 so c =0
Let's then check in assembly:
(a + y > 10) ? (c = 1) : (c = 2);
00007FF7247525AB mov eax,dword ptr [y]
00007FF7247525B1 mov ecx,dword ptr [a]
00007FF7247525B4 add ecx,eax
00007FF7247525B6 mov eax,ecx
00007FF7247525B8 cmp eax,0Ah
00007FF7247525BB jbe getChar+4Dh (07FF7247525CDh)
00007FF7247525BD mov byte ptr [c],1
00007FF7247525C1 movzx eax,byte ptr [c]
00007FF7247525C5 mov byte ptr [rbp+0F4h],al
00007FF7247525CB jmp getChar+5Bh (07FF7247525DBh)
00007FF7247525CD mov byte ptr [c],2
00007FF7247525D1 movzx eax,byte ptr [c]
00007FF7247525D5 mov byte ptr [rbp+0F4h],al
00007FF7247525CD mov byte ptr [c],2
00007FF7247525D1 movzx eax,byte ptr [c]
00007FF7247525D5 mov byte ptr [rbp+0F4h],al
// once x=7, a is uint and then transfer it into 111h
// y=-8
// when adding unint and int, both of variables would be transferred into unint
//
std::cout << "a+y=" << (a + y) << endl;
00007FF7247525DB mov eax,dword ptr [y]
It won't caculate the number
but jbe indicates that it jumps based on the equation
cmp eax,0Ah
in the eax, it shows the result ffffffff which is larger than 0Ah
so it will jump to the line
00007FF7247525CD mov byte ptr [c],2
if the euqation is not fullfiled, it will skip and not jump, when it arrives at one line before 00007FF7247525CD , the code will jump to
00007FF7247525DB mov eax,dword ptr [y]