为什么INC和DEC指令不影响进位标志

为什么INC和DEC指令不影响进位标志

本文介绍了为什么INC和DEC指令不影响进位标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么x86指令 INC (增量)和 DEC (递减)不会影响到 CF (进位标志)的FLAGSREGISTER?

Why do the x86 instruction INC (increment) and DEC (decrement) not affect the CF (carry flag) in FLAGSREGISTER?

推荐答案

要了解为什么你可能需要记住的有32位和64位值,当前的x86的CPU开始作为非常有限的8位机,回去英特尔8008(I $ C $在这个世界早在1973年的CD,我还记得(啊)吧!)。

To understand why you probably need to remember the current "x86" CPUs with 32 and 64 bit values started life as much more limited 8-bit machines, going back to the Intel 8008. (I coded in this world back in 1973, I still remember (ugh) it!).

在那个世界里,登记为precious小。您需要INC / DEC出于各种目的,最常见的是闭环控制。许多回路参与做多precision算术(例如,16位或更多!)通过让INC / DEC将Z位置,你可以用它们很好地控制回路pretty;通过坚持循环控制指令不影响进位,进位是整个循环迭代preserved并且可以实现多precision操作,而无需编写吨code要记住随身携带状态。

In that world, registers were precious and small. You need inc/dec for various purposes, the most common being loop control. Many loops involved doing "multi-precision arithmetic" (e.g, 16 bits or more!) By having inc/dec set the Z bit, you could use them to control loops pretty nicely; by insisting the loop control instructions not change the carry bit, the carry is preserved across loop iterations and you can implement multiprecision operations without writing tons of code to remember the carry state.

这工作pretty好,一旦你习惯了丑陋的指令集。

This worked pretty well, once you got used to the ugly instruction set.

在更多的现代机器具有较大的字长,你不需要这个了,所以INC和DEC可能是语义上等同于添加...,1等等。这其实是我用,当我需要进设置: - }

On more modern machines with larger word sizes, you don't need this is much, so INC and DEC could be semantically equivalent to ADD ...,1 etc. That in fact is what I use when I need the carry set :-}

大多数情况下,我远离INC和DEC走了,因为他们做的部分条件code更新,这样就可以在管道中引起滑稽摊位,和ADD / SUB不会。那么,它并不重要(大部分地方),我用ADD / SUB避免摊位。我用INC / DEC保持仅当code小事务,例如,在高速缓存行,其中的一个或两个指令的大小使足够差异无关紧要拟合。这可能是毫无意义的纳米[字面!] - 优化,但我pretty老派在我的编码习惯

Mostly, I stay away from INC and DEC now, because they do partial condition code updates, and this can cause funny stalls in the pipeline, and ADD/SUB don't. So where it doesn't matter (most places), I use ADD/SUB to avoid the stalls. I use INC/DEC only when keeping the code small matters, e.g., fitting in a cache line where the size of one or two instructions makes enough difference to matter. This is probably pointless nano[literally!]-optimization, but I'm pretty old-school in my coding habits.

我的解释告诉我们,为什么INC / DEC设置零位。我没有特别
为什么INC / DEC设置标志(和奇偶?位)说服力的解释。

My explanation tells us why INC/DEC set the zero bit. I don't have a particularlycompelling explanation for why INC/DEC set the sign (and the parity? bits).

编辑2016年4月:看来失速问题处理现代x86s更好。
请参见

EDIT April 2016: It seems that the stall problem is handled better on modern x86s.See INC instruction vs ADD 1: Does it matter?

这篇关于为什么INC和DEC指令不影响进位标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:22