1: / Long quotient
2:
3: .globl aldiv
4: .globl csv, cret
5: aldiv:
6: jsr r5,csv
7: mov 8.(r5),r3
8: sxt r4
9: bpl 1f
10: neg r3
11: 1:
12: cmp r4,6.(r5)
13: bne hardldiv
14: mov 4(r5),r1
15: mov 2(r1),r2
16: mov (r1),r1
17: bge 1f
18: neg r1
19: neg r2
20: sbc r1
21: com r4
22: 1:
23: mov r4,-(sp)
24: clr r0
25: div r3,r0
26: mov r0,r4 /high quotient
27: mov r1,-(sp) / Stash interim result
28: mov r1,r0
29: mov r2,r1
30: div r3,r0
31: bvc 1f
32: mov (sp),r0 / Recover interim result.
33: mov r2,r1 / (Regs may be clobbered by failed div.)
34: sub r3,r0 / this is the clever part
35: div r3,r0
36: tst r1
37: sxt r1
38: add r1,r0 / cannot overflow!
39: 1:
40: tst (sp)+ / Pop temp off stack
41: mov r0,r1
42: mov r4,r0
43: tst (sp)+
44: bpl 9f
45: neg r0
46: neg r1
47: sbc r0
48: 9:
49: mov 4.(r5),r2
50: mov r0,(r2)+
51: mov r1,(r2)
52: jmp cret
53:
54: / The divisor is known to be >= 2^15 so only 16 cycles are needed.
55: hardldiv:
56: clr -(sp)
57: mov 4.(r5),r0
58: mov 2(r0),r2
59: mov (r0),r1
60: bpl 1f
61: com (sp)
62: neg r1
63: neg r2
64: sbc r1
65: 1:
66: clr r0
67: mov 6.(r5),r3
68: bge 1f
69: neg r3
70: neg 8.(r5)
71: sbc r3
72: com (sp)
73: 1:
74: mov $16.,r4
75: 1:
76: clc
77: rol r2
78: rol r1
79: rol r0
80: cmp r3,r0
81: bgt 3f
82: blt 2f
83: cmp 8.(r5),r1
84: blos 2f
85: 3:
86: sob r4,1b
87: br 1f
88: 2:
89: sub 8.(r5),r1
90: sbc r0
91: sub r3,r0
92: inc r2
93: sob r4,1b
94: 1:
95: mov r2,r1
96: clr r0
97: tst (sp)+
98: beq 1f
99: neg r0
100: neg r1
101: sbc r0
102: 1:
103: mov 4.(r5),r2
104: mov r0,(r2)+
105: mov r1,(r2)
106: jmp cret
Defined functions
aldiv
declared in line
3; defined in line
5; used 1 times