2474
|
1 ****************************************
|
|
2 * 16 x 8 bit integer divide
|
|
3
|
|
4 * Result must be an 8 bit value
|
|
5
|
|
6 * ENTRY: A = divisor
|
|
7 * X = dividend
|
|
8
|
|
9 * EXIT: A = remainder
|
|
10 * B = quotient
|
|
11 * all other registers (except CC) preserved
|
|
12
|
|
13 nam 16x8 bit Divide
|
|
14 ttl Assembler Library Module
|
|
15
|
|
16
|
|
17 psect DIV168,0,0,0,0,0
|
|
18
|
|
19 vsect
|
|
20 negcount rmb 1
|
|
21 endsect
|
|
22
|
|
23 * Signed Divide
|
|
24 SDIV168:
|
|
25 clr negcount,u
|
|
26 PSHS A,X
|
|
27 tst ,s
|
|
28 bpl testquo
|
|
29 lda ,s
|
|
30 coma
|
|
31 inca
|
|
32 sta ,s
|
|
33 inc negcount,u
|
|
34 testquo
|
|
35 tst 1,s
|
|
36 bpl ok
|
|
37 ldd 1,s
|
|
38 comb
|
|
39 coma
|
|
40 addd #$0001
|
|
41 std 1,s
|
|
42 inc negcount,u
|
|
43 ok
|
|
44 puls a,x
|
|
45 bsr DIV168
|
|
46 dec negcount,u
|
|
47 bne goforit
|
|
48 pshs a,x
|
|
49 lda ,s
|
|
50 coma
|
|
51 inca
|
|
52 sta ,s
|
|
53 ldd 1,s
|
|
54 coma
|
|
55 comb
|
|
56 addd #$0001
|
|
57 std 1,s
|
|
58 puls a,x
|
|
59 goforit
|
|
60 rts
|
|
61
|
|
62
|
|
63 DIV168:
|
|
64 LDB #8 bit counter
|
|
65 PSHS A,B,X save count and divisor and value
|
|
66 TFR X,D put dividend in D
|
|
67
|
|
68 div1
|
|
69 ASLB shift dividend and quotient
|
|
70 ROLA
|
|
71 CMPA 0,S is trial subtraction successful?
|
|
72 BCS div2
|
|
73 SUBA 0,S yes, subtract and set bit
|
|
74 INCB in quotient
|
|
75
|
|
76 div2
|
|
77 DEC 1,S count down bits
|
|
78 BNE div1 loop till done
|
|
79 LEAS 2,S clean stack
|
|
80 PULS X,PC return
|
|
81
|
|
82 endsect
|
|
83
|