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 三少_爷 阅读(111) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

随笔分类

随笔档案

My Website

搜索

最新评论

阅读排行榜

评论排行榜