File: itoa.S

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (104 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2000, 2001, 2002, Michael Rickmann
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

/* $Id: itoa.S,v 1.7 2005/10/19 20:12:45 aesok Exp $ */

/*
   itoa.S

   Copyright (C) 2000 Michael Rickmann <mrickma@gwdg.de>
   Changes: Reiner Patommel <rpato@post4.tele.dk>
	(no stack space used anymore)
   Changes: Reiner Patommel <rpato@post4.tele.dk> bug# 4010 		  		

   #include <stdlib.h>
   char *itoa(int value, char *string, int radix);
  ----------------------------------------------------------------------
*/

#if !defined(__DOXYGEN__)

#include "macros.inc"
#include "ctoasm.inc"

	.global _U(itoa)
	.func _U(itoa)

/* arguments */
#define r_val_hi r25
#define r_val_lo r24
#define r_str_hi r23
#define r_str_lo r22
#define r_radix  r20

/* local variables */
#define r_lstr_hi r19
#define r_lstr_lo r18


_U(itoa):
	X_movw	ZL, r_str_lo	; &string
	X_movw	r_lstr_lo, ZL	; save &string
	clt			; make sure T flag is clear
	cpi	r_radix, 2	; no radix < 2
	brlt	99f		; return with \0
	cpi	r_radix, 37	; no radix > 36
	brge	99f		; return with \0
	cpi	r_radix, 10	; decimal number ?
	brne	1f		; no -> convert
	bst	r_val_hi, 7	; for decimal numbers remember sign of value
	brtc	1f		; positive number -> convert
	com	r_val_hi	; negative number -> make positive
	neg	r_val_lo
	sbci	r_val_hi, 0xff
1:
	mov	r22, r_radix	; radix
	clr	r23		;  dividend in r25:r24, divisor in r23:r22
	XCALL	_U(__udivmodhi4);  quotient in r23:r22, remainder in r25:r24
				;  clobbered: r0, r21, r26, r27
				;  call-used but preserved: r18, r19, r20, r30, r31
	subi	r24, 0xd0	; + '0'
	cpi	r24, 0x3a	; > '9' ?
	brlt	10f
	subi	r24, 0xd9	; + 'a' - 10 - '0'
10:
	st	Z+, r24		; write char to string in reverse order
	X_movw	r_val_lo, r22	; quotient -> dividend
	sbiw	r_val_lo, 0	; value == 0 ?
	brne	1b
	brtc	99f		; T flag clear? -> positive number		
	ldi	r21, '-'
	st	Z+, r21		; write minus sign to string
99:	
	st	Z, __zero_reg__	; terminate string
	X_movw	r_val_lo, r_lstr_lo	; restore &string as return value
	XJMP	_U(strrev)	; reverse string

	.endfunc

#endif /* not __DOXYGEN__ */