[net.lang] Integer division on the Intel iAPX86,8

sambo@ukma.UUCP (Inventor of micro-S) (08/05/85)

     I am currently working on a compiler for a language I have designed, and
whose target machine is the IBM PC.  I happened to look at the description of
the IDIV instruction for the Intel 8086 and 8088.  It says that the minimum
negative quotient for a byte division is -127, and the minimum negative quo-
tient for a word division is -32767.  I tested this, and lo and behold, both
-128 / 1 and -32768 / 1 do indeed generate a division-by-zero interrupt.  I
am wondering three things:
     1) Why does this happen - i.e. what hardware algorithm does this proces-
sor use?
     2) Are there any compilers for a machine using the 8086 or 8088 that can
handle both -128 / 1 and -32768 / 1 correctly?  (IBM Pascal Version 1.00 does
not.)
     3) Are there any other processors that behave the way the 8086 and 8088
do in this respect?  (I tested this on both our VAX 11/750 and our 3B2/300 by
writing a C program and modifying the assembly language output, and both of
them work correctly.)
     By the way, the algorithm on page 2-73 of the iAPX88 Book dated July 1981
is wrong - it implies that the maximum negative quotient is -128 and -32768
for byte and word division respectively, which is clearly contradicted in the
prose description for the instruction.
     P.S.: I know some of you may be tempted to express your great admiration
for the totality of Intel's product line, but this is not what I am looking for.
-----------------------------------------
Samuel A. Figueroa, Dept. of CS, Univ. of KY, Lexington, KY  40506-0027
ARPA: ukma!sambo<@ANL-MCS>, or sambo%ukma.uucp@anl-mcs.arpa,
      or even anlams!ukma!sambo@ucbvax.arpa
UUCP: {ucbvax,unmvax,boulder,oddjob}!anlams!ukma!sambo,
      or cbosgd!ukma!sambo

	"Micro-S is great, if only people would start using it."

markb@sdcrdcf.UUCP (Mark Biggar) (08/07/85)

In article <2018@ukma.UUCP> sambo@ukma.UUCP (Inventor of micro-S) writes:
>the IDIV instruction for the Intel 8086 and 8088.  It says that the minimum
>negative quotient for a byte division is -127, and the minimum negative quo-
>tient for a word division is -32767.  I tested this, and lo and behold, both
>-128 / 1 and -32768 / 1 do indeed generate a division-by-zero interrupt.  I
>am wondering three things:
>     1) Why does this happen - i.e. what hardware algorithm does this proces-
>sor use?

The actual divide algorithm only works on positive numbers, so the input
values have their signs remembered and the twos-complement is taken for
negatives.  But, the twos-complement of 0x8000 or 0x80 (done by complementing
and adding 1) is still 0x8000 or 0x80; so the algorithm now ignores the sign
bits (thinking the numbers are now positive) and divides by zero.

Mark Biggar
{allegra,burdvax,cbosgd,hplabs,ihnp4,akgua,sdcsvax}!sdcrdcf!markb

gnu@sun.uucp (John Gilmore) (08/09/85)

Dividing the largest negative number by 1 works fine on the 68010 (16
bit signed divide) and 68020 (32 bit signed divide); I tested it.  Has
anybody tried an 80186 or 80286 yet?  They may have "fixed it in the
next release".

PS:  Followups should go to net.arch only.

kds@intelca.UUCP (Ken Shoemaker) (08/13/85)

> Dividing the largest negative number by 1 works fine on the 68010 (16
> bit signed divide) and 68020 (32 bit signed divide); I tested it.  Has
> anybody tried an 80186 or 80286 yet?  They may have "fixed it in the
> next release".
> 
> PS:  Followups should go to net.arch only.

**sigh** I replied directly to the poster.  Yes, the 186 and the 286
add the most negative/signed two's complement number to the
acceptable results for division w/o a divide-by-zero trap.  It's
been fixed on every release (i.e., it was designed that way from
the start).  But not only that, extra hardware was added such that
multiples and divides take 1/4 the number of clocks.  So there...
-- 
...and I'm sure it wouldn't interest anybody outside of a small circle
of friends...

Ken Shoemaker, Microprocessor Design for a large, Silicon Valley firm

{pur-ee,hplabs,amd,scgvaxd,dual,qantel}!intelca!kds
	
---the above views are personal.  They may not represent those of the
	employer of its submitter.

jchapman@watcgl.UUCP (john chapman) (08/14/85)

 Is there some reason why integer division articles should be
 sent to net.lang? Please guys, edit the newsgroup line.

-- 

	John Chapman
	...!watmath!watcgl!jchapman

	Disclaimer : These are not the opinions of anyone but me
		     and they may not even be mine.