2474
|
1 *********************************************
|
|
2 * 16 x 16 bit integer divide
|
|
3
|
|
4 * OTHER MODULES NEEDED: none
|
|
5
|
|
6 * ENTRY: D = divisor
|
|
7 * X = dividend
|
|
8
|
|
9 * EXIT: X = quotient
|
|
10 * D = remainder
|
|
11
|
|
12 nam 16x16 bit Divide
|
|
13 ttl Assembler Library Module
|
|
14
|
|
15
|
|
16 psect DIV16,0,0,0,0,0
|
|
17
|
|
18 vsect
|
|
19 negcount rmb 1
|
|
20 endsect
|
|
21
|
|
22 * Signed Divide
|
|
23 SDIV16:
|
|
24 clr negcount,u
|
|
25 PSHS D,X
|
|
26 tst ,s
|
|
27 bpl testquo
|
|
28 ldd ,s
|
|
29 comb
|
|
30 coma
|
|
31 addd #$0001
|
|
32 std ,s
|
|
33 inc negcount,u
|
|
34 testquo
|
|
35 tst 2,s
|
|
36 bpl ok
|
|
37 ldd 2,s
|
|
38 comb
|
|
39 coma
|
|
40 addd #$0001
|
|
41 std 2,s
|
|
42 inc negcount,u
|
|
43 ok
|
|
44 puls d,x
|
|
45 bsr DIV16
|
|
46 dec negcount,u
|
|
47 bne goforit
|
|
48 pshs d,x
|
|
49 ldd ,s
|
|
50 coma
|
|
51 comb
|
|
52 addd #$0001
|
|
53 std ,s
|
|
54 ldd 2,s
|
|
55 coma
|
|
56 comb
|
|
57 addd #$0001
|
|
58 std 2,s
|
|
59 puls d,x
|
|
60 goforit
|
|
61 rts
|
|
62
|
|
63
|
|
64 * Unsigned Divide
|
|
65 DIV16:
|
|
66 PSHS D,X save divisor & dividend
|
|
67 LDA #16 bit counter
|
|
68 PSHS A
|
|
69 CLRA initialize remainder
|
|
70 CLRB
|
|
71
|
|
72 div1
|
|
73 ASL 4,S shift dividend & quotient
|
|
74 ROL 3,S
|
|
75 ROLB shift dividend into B
|
|
76 ROLA
|
|
77 CMPD 1,S trial subtraction reqd?
|
|
78 BLO div2
|
|
79 SUBD 1,S yes, do subtraction
|
|
80 INC 4,S increment quotient
|
|
81
|
|
82 div2
|
|
83 DEC ,S count down another bit
|
|
84 BNE div1
|
|
85 LDX 3,S get quotient
|
|
86 LEAS 5,S clean stack
|
|
87 RTS
|
|
88
|
|
89 endsect
|
|
90
|