x86汇编:INC和DEC指令和溢出标志 [英] x86 Assembly: INC and DEC instruction and overflow flag

查看:1888
本文介绍了x86汇编:INC和DEC指令和溢出标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在x86汇编,溢出标志设置当添加一个符号整数溢出运行,并且当一个无符号整数的操作溢出的进位标志置位。

In x86 assembly, the overflow flag is set when an add or sub operation on a signed integer overflows, and the carry flag is set when an operation on an unsigned integer overflows.

然而,当涉及到 INC 的说明,情况似乎有所不同。根据这一网站,在 INC 指令不影响进位标志都没有。

However, when it comes to the inc and dec instructions, the situation seems to be somewhat different. According to this website, the inc instruction does not affect the carry flag at all.

但我不能找到有关如何 INC 影响溢出标志的任何信息,如果在所有

But I can't find any information about how inc and dec affect the overflow flag, if at all.

不要 INC 设置溢出标志当一个整数溢出发生?而这种行为是相同的两个符号和无符号整数?

Do inc or dec set the overflow flag when an integer overflow occurs? And is this behavior the same for both signed and unsigned integers?

=============================的修改 ========== ===================

============================= EDIT =============================

好了,所以基本上这里的共识是,INC和DEC应该表现一样ADD和SUB,在设置标志方面,进位标志的除外。这也是它在Intel手册上说。

Okay, so essentially the consensus here is that INC and DEC should behave the same as ADD and SUB, in terms of setting flags, with the exception of the carry flag. This is also what it says in the Intel manual.

问题是在实践中我不能真正重现此问题,当它涉及到无符号整数。

The problem is I can't actually reproduce this behavior in practice, when it comes to unsigned integers.

考虑以下组装code(使用GCC内联汇编,以方便打印出结果。)

Consider the following assembly code (using GCC inline assembly to make it easier to print out results.)

int8_t ovf = 0;

__asm__
(
    "movb $-128, %%bh;"
    "decb %%bh;"
    "seto %b0;"
    : "=g"(ovf)
    :
    : "%bh"
);

printf("Overflow flag: %d\n", ovf);

下面我们递减-128一个符号的8位值。自-128是最小的可能值,溢出是不可避免的。正如预期的那样,这种打印出:溢出标志:1

Here we decrement a signed 8-bit value of -128. Since -128 is the smallest possible value, an overflow is inevitable. As expected, this prints out: Overflow flag: 1

但是,当我们做同样的用的符号的价值,行为是不是如我所料:

But when we do the same with an unsigned value, the behavior isn't as I expect:

int8_t ovf = 0;

__asm__
(
    "movb $255, %%bh;"
    "incb %%bh;"
    "seto %b0;"
    : "=g"(ovf)
    :
    : "%bh"
);

printf("Overflow flag: %d\n", ovf);

下面我增加255由于255的无符号的8位值是最大的可能值,溢出是不可避免的。然而,这种打印出:溢出标志:0

Here I increment an unsigned 8-bit value of 255. Since 255 is the largest possible value, an overflow is inevitable. However, this prints out: Overflow flag: 0.

咦?为什么不把它设置溢出标志在这种情况下?

Huh? Why didn't it set the overflow flag in this case?

推荐答案

溢出标志被设置时的操作会导致符号改变。您code是非常接近的。我能够设置标志与以下(VC ++)code:

The overflow flag is set when an operation would cause a sign change. Your code is very close. I was able to set the OF flag with the following (VC++) code:

char ovf = 0;

_asm {
    mov bh, 127
    inc bh
    seto ovf
}
cout << "ovf: " << int(ovf) << endl;

当BH递增的MSB从0变为1,从而引起的进行设置。

When BH is incremented the MSB changes from a 0 to a 1, causing the OF to be set.

这也设置的:

char ovf = 0;

_asm {
    mov bh, 128
    dec bh
    seto ovf
}
cout << "ovf: " << int(ovf) << endl;

请记住,处理器不会有符号和无符号数区分。当您使用2的补运算,可以有一组同时处理的指令。如果你想测试无符号溢出,则需要使用进位标志。由于INC / DEC不影响进位标志,你需要使用ADD / SUB该案例。

Keep in mind that the processor does not distinguish between signed and unsigned numbers. When you use 2's complement arithmetic, you can have one set of instructions that handle both. If you want to test for unsigned overflow, you need to use the carry flag. Since INC/DEC don't affect the carry flag, you need to use ADD/SUB for that case.

这篇关于x86汇编:INC和DEC指令和溢出标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆