unsigned int add and check assembly

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] 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值