8086 family CPU bug

Hugh Redelmeier hugh at hcrvx1.UUCP
Fri Jul 5 22:34:56 AEST 1985


All 8086 manuals I have read say that shift instructions set
the flags to reflect the value of the result.  All the chips
that I have tested (8086, 8088, 80186) do not set the flags
if the shift count is zero.  I discovered this when the following
C code gave the wrong answer (under XENIX 2.1 -- not their
fault though) (this code is from memory, so it may be slightly
inaccurate):

main() {
	register int i, j;
	i = 0;
	j = 1; j += 1;	/* clear Z flag */
	if (i << i) /* shift of 0 by 0 should set Z flag, but doesn't */
		printf("WRONG!\n");
	}

[Even if the predicate is written more cleanly as (i<<i != 0), the
compiler is smart enough to generate the same code.  The use of i
instead of the literal 0 is to prevent compile-time evaluation.]

At the machine language level what is going on?  The key
instruction is a shift with its count in a register.  This
count is 0.  The shift does not change the flags from their
previous value.

Cure(idealistic): INTEL et al should fix the chips (and give users new ones!)
Cure(imaginable): INTEL et al should revise the manuals; every compiler that
	is affected should be updated (and users given new ones); every
	program compiled using the previous compiler should be re-compiled;
	every assembler program should be combed for this problem.
unCure(sad but true): users should be aware of this problem



More information about the Net.bugs mailing list