OpenCores
URL https://opencores.org/ocsvn/8051/8051/trunk

Subversion Repositories 8051

[/] [8051/] [trunk/] [asm/] [DIV16U.asm] - Blame information for rev 186

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 simont
        mov r5, #0a1h;
2
        mov r4, #054h;
3
        mov r1, #001h;
4
        mov r0, #070h;
5
        lcall DIV16U;
6
        mov p0, r5;
7
        mov p0, r4;
8 41 simont
        mov p0, r7;
9
        mov p0, r6;
10 2 simont
 
11 41 simont
;
12
;  testing div
13
;
14 2 simont
 
15 41 simont
  mov a, #0e5h;
16
  mov b, #072h;
17
  div ab;
18
  mov p0, a;
19
  mov p0, b;
20
  jnc ok1;
21
  mov p1, #00h;
22
 
23
ok1:
24
  mov c, psw.2;
25
  jnc ok2;
26
  mov p1, #01h;
27 2 simont
 
28 41 simont
ok2:
29
  mov a, #0d3h;
30
  mov b, #00h;
31
  div ab;
32
  jnc ok3;
33
  mov p1, #02h;
34 2 simont
 
35 41 simont
ok3:
36
  mov c, psw.2;
37
  jc ok4;
38
  mov p1, #03h;
39
 
40
 
41
;
42
;testing mul
43
;
44
 
45
ok4:
46
  mov a, #03h
47
  mov b, #04h
48
  mul ab;
49
  mov p0, a;
50
  mov p0, b;
51
  jnc ok5;
52
  mov p1, #04h;
53
 
54
ok5:
55
  mov c, psw.2;
56
  jnc ok6;
57
  mov p1, #05h;
58
 
59
ok6:
60
  mov a, #057h;
61
  mov b, #0eeh;
62
  mul ab;
63
  mov p0, a;
64
  mov p0, b;
65
  jnc ok7;
66
  mov p1, #06h;
67
 
68
ok7:
69
  mov c, psw.2;
70
  jc ok8;
71
  mov p1, #07h;
72
 
73
ok8:
74
  mov p0, #00h;
75
  nop
76
  sjmp ok8;
77
 
78
 
79
 
80
 
81
 
82 2 simont
;26 Oct 00 added code to zero remainder when dividend is zero
83
;26 Oct 00 Change labels from duxxx to duaxxx
84
;19 Dec 99 corrected comments
85
;16 Dec 99 made from DIV32U
86
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
87
;
88
;DIV16U is called to divide (unsigned) a 16-bit dividend using a
89
; 16-bit divisor.
90
;
91
;DIV16U solves for quotient and remainder the equation:
92
;
93
; dividend = divisor*quotient + remainder
94
;
95
;Call:
96
;  r5,r4 = dividend
97
;  r1,r0 = divisor
98
;  lcall DIV16U
99
;  jc   divide_by_zero
100
;
101
;Return:
102
; r5,r4 = quotient
103
; r7,r6 = remainder
104
; c flag set to 1 if divide by zero attempted
105
; All registers, acc, b and have been changed.
106
; Data pointer has not been disturbed
107
;
108
;Note:
109
; (1)Most significant (ms) register always listed first when comma
110
;  separates two in a comment. Example: r5,r4 (r5 contains the ms bits)
111
; (2) The algorithm used in this code borrows heavily from work posted
112
;   by John C. Wren who said he got it from a C complier.
113
;
114
;Original author: John Veazey, Ridgecrest, CA, 16 Dec 99
115
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116
;
117
;cseg
118
DIV16U:
119
;
120
;Clear the working quotient
121
;
122
      clr   a
123
      mov   r2,a
124
      mov   r3,a
125
;
126
;b counts the number of places+1 the divisor was initially
127
; shifted left to align its ms bit set with the ms bit set
128
; in the dividend
129
;
130
      mov   b,#1
131
;
132
;Make an error return if trying to divide by zero
133
;
134
      mov   a,r1
135
      orl   a,r0
136
      jz    dua920
137
;
138
;Just return with quotient and remainder zero if dividend is zero
139
;
140
      mov   a,r5;
141
      orl   a,r4;
142
      jnz   dua200;
143
      mov   r7,a;
144
      mov   r6,a;
145
      ajmp  dua910      ;Make a normal return
146
;
147
;Align the msb set in the demoninator with the msb set in the
148
; numerator. Increment the shift count in b each time a shift left
149
; is performed.
150
;
151
dua200:
152
      mov   a,r1        ;Stop if MSB set
153
      rlc   a
154
      jc    dua600
155
      clr   c
156
      mov   a,r5        ;Compare r1 & r5
157
      subb  a,r1
158
      jc    dua600      ; jump if r1>r5
159
      jnz   dua240      ; jump if r1
160
      mov   a,r4        ;r1=r5, so compare r0 & r4
161
      subb  a,r0
162
      jc    dua600      ; jump if r0>r4
163
dua240:
164
      clr   c           ;Now shift the denominator
165
      mov   a,r0        ; left 1 bit position
166
      rlc   a
167
      mov   r0,a
168
      mov   a,r1
169
      rlc   a
170
      mov   r1,a
171
      inc   b           ;Increment b counter and
172
      sjmp  dua200      ; continue
173
;
174
;Compare the shifted divisor with the remainder (what's
175
; left of the dividend)
176
;
177
dua600:
178
      clr   c
179
      mov   a,r5
180
      subb  a,r1
181
      jc    dua720      ;jump if r1>r5
182
      jnz   dua700      ;jump if r1
183
      mov   a,r4
184
      subb  a,r0
185
      jc    dua720      ;jump if r0>r4
186
;
187
;Divisor is equal or smaller, so subtract it off and
188
; get a 1 for the quotient
189
;
190
dua700:
191
      mov   a,r4
192
      clr   c
193
      subb  a,r0
194
      mov   r4,a
195
      mov   a,r5
196
      subb  a,r1
197
      mov   r5,a
198
      clr   c
199
      cpl   c           ;Get a 1 for the quotient
200
      sjmp  dua730
201
;
202
;Divisor is greater, get a 0 for the quotient
203
;
204
dua720:
205
      clr   c
206
;
207
;Shift 0 or 1 into quotient
208
;
209
dua730:
210
      mov   a,r2
211
      rlc   a
212
      mov   r2,a
213
      mov   a,r3
214
      rlc   a           ;Test for overlow removed here because
215
      mov   r3,a        ; it can't happen when dividing 16 by 16
216
;
217
;Now shift the denominator right 1, decrement the counter
218
; in b until b = 0
219
;
220
dua740:
221
      clr   c
222
      mov   a,r1
223
      rrc   a
224
      mov   r1,a
225
      mov   a,r0
226
      rrc   a
227
      mov   r0,a
228
      djnz  b,dua600
229
;
230
;Move quotient and remainder so that quotient is returned in the same
231
; registers as the dividend. This makes it easier to divide repeatedly
232
; by the same number as you would do when converting to a new radix.
233
;
234
      mov   a,r5
235
      mov   r7,a
236
      mov   a,r4
237
      mov   r6,a
238
      mov   a,r3
239
      mov   r5,a
240
      mov   a,r2
241
      mov   r4,a
242
;
243
;Make the normal return
244
;
245
dua910:
246
      clr   c
247
      ret
248
;
249
;Make the error return
250
;
251
dua920:
252
      clr   c
253
      cpl   c
254
      ret
255
;End of DIV16U

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.