summaryrefslogtreecommitdiff
path: root/device/lib/pic16/libc/utils/cvtdec.S
blob: 0e6fa420451f3f7b10cbed6187601691ed6ae1fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
;--------------------------------------------------------------------------
;  cvtdec.S - convert a 16-bit binary word to 5 BCD bytes
;
;  Copyright (C) 2004, George Gallant <ggallant571 AT verizon.net>
;
;  This library is free software; you can redistribute it and/or modify it
;  under the terms of the GNU General Public License as published by the
;  Free Software Foundation; either version 2, or (at your option) any
;  later version.
;
;  This library is distributed in the hope that it will be useful,
;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;  GNU General Public License for more details.
;
;  You should have received a copy of the GNU General Public License 
;  along with this library; see the file COPYING. If not, write to the
;  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
;   MA 02110-1301, USA.
;
;  As a special exception, if you link this library with other files,
;  some of which are compiled with SDCC, to produce an executable,
;  this library does not by itself cause the resulting executable to
;  be covered by the GNU General Public License. This exception does
;  not however invalidate any other reasons why the executable file
;  might be covered by the GNU General Public License.
;--------------------------------------------------------------------------

;--
;
;	File:	cvtdec.asm
;	Author:	George Gallant
;	Date:	19OCT04
;	
;	This routine is based on the code and algorithm by
;	Nikolai Golovchenko and Scott Dattalo presented in
;	the piclist webb site on radix conversion methods.
;	Modified to support integers from 0 to 65535 and coded
;	for the pic18 chip set.
;
;			   Digit
;	BIT  Weight  4   3   2   1   0
;	---  ------ --- --- --- --- ---
;	 0      1    0   0   0   0   1
;	 1      2    0   0   0   0   2
;	 2      4    0   0   0   0   4
;	 3      8    0   0   0   0   8
;	
;	 4     16    0   0   0   1   6
;	 5     32    0   0   0   3   2
;	 6     64    0   0   0   6   4
;	 7    128    0   0   1   2   8
;
;	 8    256    0   0   2   5   6
;	 9    512    0   0   5   1   2
;	10   1024    0   1   0   2   4
;	11   2048    0   2   0   4   8
;
;	12   4096    0   4   0   9   6
;	13   8192    0   8   1   9   2
;	14  16384    1   6   3   8   4
;	15  32768    3   2   7   6   8
;
;--
		nolist
		include	<p18fxxx.inc>
		list

		udata

		global	digits

digits:		res	6

		code

		global	cvt_dec_word

;--
;
;	Convert a 16-bit binary word to 5 BCD bytes
;
;	On Call
;		PROD	16 bit word
;		FSR0	pointer to ascii buffer
;
;	On Return
;		digits[4:0] contain the BCD integers
;
;	Notes:	1. strip leading zeros
;		2. only positive integers on input
;		3. FSR0, PROD & W are modified and not preserved
;
;--
cvt_dec_word:	clrw				;calculate digit 0 - lsd

		btfsc	PRODL,0			;bit0
		addlw	1

		btfsc	PRODL,1			;bit1
		addlw	2

		btfsc	PRODL,2			;bit2
		addlw	4

		btfsc	PRODL,3			;bit3
		addlw	8

		btfsc	PRODL,4			;bit4
		addlw	6

		btfsc	PRODL,5			;bit5
		addlw	2

		btfsc	PRODL,6			;bit6
		addlw	4

		btfsc	PRODL,7			;bit7
		addlw	8

		btfsc	PRODH,0			;bit8
		addlw	6

		btfsc	PRODH,1			;bit9
		addlw	2

		btfsc	PRODH,2			;bit10
		addlw	4

		btfsc	PRODH,3			;bit11
		addlw	8

		btfsc	PRODH,4			;bit12
		addlw	6

		btfsc	PRODH,5			;bit13
		addlw	2

		btfsc	PRODH,6			;bit14
		addlw	4

		btfsc	PRODH,7			;bit15
		addlw	8

		clrf	digits+1		;setup for carry in 10's
@1:		addlw	-10
		bnc	@2
		incf	digits+1,f
		bra	@1

@2:		addlw	10
		movwf	digits+0

;	calculate digit 1

		movf	digits+1,w

		btfsc	PRODL,4			;bit4
		addlw	1

		btfsc	PRODL,5			;bit5
		addlw	3

		btfsc	PRODL,6			;bit6
		addlw	6

		btfsc	PRODL,7			;bit7
		addlw	2

		btfsc	PRODH,0			;bit8
		addlw	5

		btfsc	PRODH,1			;bit9
		addlw	1

		btfsc	PRODH,2			;bit10
		addlw	2

		btfsc	PRODH,3			;bit11
		addlw	4

		btfsc	PRODH,4			;bit12
		addlw	9

		btfsc	PRODH,5			;bit13
		addlw	9

		btfsc	PRODH,6			;bit14
		addlw	8

		btfsc	PRODH,7			;bit15
		addlw	6

		clrf	digits+2		;setup for carry into 100's
@3:		addlw	-10
		bnc	@4
		incf	digits+2,f
		bra	@3

@4:		addlw	10
		movwf	digits+1

;	calculate digit 2

		movf	digits+2,w

		btfsc	PRODL,7			;bit7
		addlw	1

		btfsc	PRODH,0			;bit8
		addlw	2

		btfsc	PRODH,1			;bit9
		addlw	5

		btfsc	PRODH,5			;bit13
		addlw	1

		btfsc	PRODH,6			;bit14
		addlw	3

		btfsc	PRODH,7			;bit15
		addlw	7

		clrf	digits+3		;setup for carry into 1000's
@5:		addlw	-10
		bnc	@6
		incf	digits+3,f
		bra	@5
@6:		addlw	10

		movwf	digits+2

;	calculate digit 3

		movf	digits+3,w

		btfsc	PRODH,2			;bit10
		addlw	1

		btfsc	PRODH,3			;bit11
		addlw	2

		btfsc	PRODH,4			;bit12
		addlw	4

		btfsc	PRODH,5			;bit13
		addlw	8

		btfsc	PRODH,6			;bit14
		addlw	6

		btfsc	PRODH,7			;bit15
		addlw	2

		clrf	digits+4		;setup for carry into 10000's
@7:		addlw	-10
		bnc	@8
		incf	digits+4,f
		bra	@7

@8:		addlw	10
		movwf	digits+3

;	calculate digit 4

		movf	digits+4,w

		btfsc	PRODH,6			;bit14
		addlw	1

		btfsc	PRODH,7			;bit15
		addlw	3

@9:		addlw	-10
		bc	@9

		addlw	10
;		movwf	digits+4

;		movf	digits+4,w
		bnz	@11
		movf	digits+3,w
		bnz	@12
		movf	digits+2,w
		bnz	@13
		movf	digits+1,w
		bnz	@14
		bra	@15

@11:		addlw	0x30
		movwf	POSTINC0

		movf	digits+3,w
@12:		addlw	0x30
		movwf	POSTINC0

		movf	digits+2,w
@13:		addlw	0x30
		movwf	POSTINC0

		movf	digits+1,w
@14:		addlw	0x30
		movwf	POSTINC0

@15:		movf	digits+0,w
		addlw	0x30
		movwf	POSTINC0

		return

		end