B – 7
B
Division Exceptions
Return Values
AR = 16-bit quotient
MR0 = 16-bit remainder
AV flag set if divide would overflow
Altered Registers
AX0, AX1, AR, AF, AY0, AY1, MR, MY0
Computation Time: 30 cycles
}
.ENTRY
signed_div, unsigned_div;
signed_div:
MR0=AY0,AF=AX0+AY1;
{Take divisor’s absolute value}
MR1=AY1, AR=ABS AX0;
{See if divisor, dividend have
same magnitude}
test_1:
IF NE JUMP test_2;
{If divisor non-zero, do test 2}
ASTAT=0x4;
{Divide by zero, so overflow}
RTS;
{Return to calling program}
test_2:
IF NOT AV JUMP test_3;
{If divisor 0x8000, then the}
AY0=AY1, AF=ABS AY1;
{quotient is simply -AY1}
IF NOT AV JUMP recover_sign;
ASTAT=0x4;
{0x8000 divided by 0x8000,}
RTS;
{so overflow}
test_3:
AF=PASS AF;
{Check for division overflow}
IF NE JUMP test_4;
{Not equal, jump test 4}
AY0=0x8000;
{Quotient equals -1}
ASTAT=0x0;
{Clear AS bit of ASTAT}
JUMP recover_sign;
{Compute remainder}
test_4:
AF=ABS MR1;
{Get absolute of dividend}
AR=ABS AX0;
{Restore AS bit of ASTAT}
AF=AF-AR;
{Check for division overflow}
IF LT JUMP do_divs;
{If Divisor>Dividend do divide}
ASTAT=0x4;
{Division overflow}
RTS;
Listing B.1 Division Error Routine
(continues on next page)