add two int numbers, check whether overflowed

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
using namespace std;

int ifo_add(int a,int b)
{
    __asm{
        mov eax,a
        add eax,b
        jo overflow
        xor eax,eax
        jmp no_overflow
overflow:
        mov eax,1
no_overflow:
    }
}
int main() {
    int a,b;

    a=          1;b= 2;printf("%11d+(%2d)  %d\n",a,b,ifo_add(a,b));
    a=         -1;b=-2;printf("%11d+(%2d)  %d\n",a,b,ifo_add(a,b));
    a= 2147483647;b= 1;printf("%11d+(%2d)  %d\n",a,b,ifo_add(a,b));
    a=-2147483647;b=-1;printf("%11d+(%2d)  %d\n",a,b,ifo_add(a,b));
    a=-2147483647;b=-2;printf("%11d+(%2d)  %d\n",a,b,ifo_add(a,b));

    system("pause");
    return 0;
}

posted on 2012-08-20 13:50 三少_爷 阅读(138) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


<2026年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

导航

统计

常用链接

留言簿

随笔分类

随笔档案

My Website

搜索

最新评论

阅读排行榜

评论排行榜