B – 5
B
Division Exceptions
Since many applications do not require complete error checking, the code
has been designed so you can remove tests that are not necessary for your
project. This will decrease memory requirements, as well as increase
execution speed.
The module
signed_div
expects the 32-bit dividend to be stored in
AY1&AY0, and the divisor in AX0. Upon return either the AR register
holds the quotient and MR0 holds the remainder, or the overflow flag is
set. The entire routine takes at most twenty-seven cycles to execute. If an
exception condition exists, it may return sooner. The first two instructions
store the dividend in the MR registers, the absolute value of the
dividend’s MSW in AF, and the divisor’s absolute value in AR.
The code block labeled
test_1
checks for division by 0x8000. Attempting to
take the absolute value of 0x8000 produces an overflow. If the AV flag is
set (from taking the absolute value of the divisor), then the quotient is –
AY1. This can produce an error if AY1 is 0x8000, so after taking the
negative of AY1, the overflow flag is checked again. If it is set control is
returned to the calling routine, otherwise the remainder is computed. If it
is not necessary to check for a divisor of 0x8000, this code block can be
removed.
The code block labeled
test_2
checks for a division overflow condition. The
absolute value of the divisor is subtracted from the absolute value of the
dividend’s MSW. If the divisor is less then the dividend, it is likely an
overflow will occur. If the two are equal in magnitude, but different in
sign, the result will be 0x8000, so this special case is checked. If your
application does not require an overflow check, this code block can be
removed. If you decide to remove
test_2
be sure to change the JUMP
address in
test_1
to
do_divs
, instead of
test_2
.
After error checking, the actual division is performed. Since the absolute
value of the divisor has been stored in AR, this is used as the X-operand
for the DIVS instruction. 15 DIVQ instructions follow, computing the rest
of the quotient. The correct sign for the quotient is determined, based on
the AS flag of the ASTAT register. Since the MR register contains the
original dividend, the remainder can be determine by a multiply subtract
operation. The divisor times the quotient is subtracted from MR to
produce the remainder in MR0.
The last step before returning is to clear the ASTAT register which may
contain an overflow flag produced during the divide.