1 ;
--------------------------------------------------------------------------
4 ; Copyright
(C
) 2000-2021, Michael Hope
, Philipp Klaus Krause
6 ; This library is free software; you can redistribute it
and/or modify it
7 ; under the terms of the GNU General Public License as published by the
8 ; Free Software Foundation; either version
2, or (at your option
) any
11 ; This library is distributed in the hope that it will
be useful
,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY
or FITNESS FOR
A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
16 ; You should have received
a copy of the GNU General Public License
17 ; along with this library; see the file COPYING. If
not, write to the
18 ; Free Software Foundation
, 51 Franklin Street
, Fifth Floor
, Boston
,
21 ; As
a special exception
, if you link this library with other files
,
22 ; some of which are compiled with SDCC
, to produce an executable
,
23 ; this library does
not by itself cause the resulting executable to
24 ;
be covered by the GNU General Public License. This exception does
25 ;
not however invalidate any other reasons why the executable file
26 ; might
be covered by the GNU General Public License.
27 ;
--------------------------------------------------------------------------
48 ; Fall through to __div16
50 ;; signed
16-bit division
60 ;; Register used
: AF
,B,DE
,HL
63 ;; Determine sign of quotient by xor-ing high bytes of dividend
64 ;;
and divisor. Quotient is positive if signs are the same
, negative
65 ;; if signs are different
66 ;; Remainder has same sign as dividend
67 ld a, h ; Get high byte of dividend
68 xor a, d ;
Xor with high byte of divisor
69 rla ; Sign of quotient goes into the carry
70 ld a, h ; Get high byte of dividend
71 push af ; Save sign of both quotient
and reminder
73 ; Take absolute value of dividend
75 jr NC
, .chkde ; Jump if dividend is positive
76 sub a, a ; Subtract dividend from
0
79 sbc
a, a ; Propagate borrow
(A=0xFF if borrow
)
83 ; Take absolute value of divisor
86 jr Z
, .dodiv ; Jump if divisor is positive
87 sub a, a ; Subtract divisor from
0
90 sbc
a, a ; Propagate borrow
(A=0xFF if borrow
)
94 ; Divide absolute values
99 ; Negate quotient if it is negative
100 pop af ; recover sign of quotient
101 ret NC ; Jump if quotient is positive
103 sub a, a ; Subtract quotient from
0
106 sbc
a, a ; Propagate borrow
(A=0xFF if borrow
)
113 ; Negate remainder if it is negative.
116 ret NC ; Return if remainder is positive
117 sub a, a ; Subtract quotient from
0
120 sbc
a, a ; Propagate borrow
(A=0xFF if borrow
)