1/* Copyright (c) 2002, 2007 Reiner Patommel
2   Copyright (c) 2007  Dmitry Xmelkov
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7
8   * Redistributions of source code must retain the above copyright
9     notice, this list of conditions and the following disclaimer.
10   * Redistributions in binary form must reproduce the above copyright
11     notice, this list of conditions and the following disclaimer in
12     the documentation and/or other materials provided with the
13     distribution.
14   * Neither the name of the copyright holders nor the names of
15     contributors may be used to endorse or promote products derived
16     from this software without specific prior written permission.
17
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  POSSIBILITY OF SUCH DAMAGE. */
29
30/* $Id: strrev.S 2191 2010-11-05 13:45:57Z arcanum $ */
31
32/*
33   strrev.S
34   Reverse a string
35
36   Contributors:
37     Created by Reiner Patommel
38*/
39/** \file */
40
41/** \ingroup avr_string
42    \fn char *strrev(char *s)
43    \brief Reverse a string.
44
45    The strrev() function reverses the order of the string.
46
47    \returns The strrev() function returns a pointer to the beginning of the
48    reversed string.  */
49
50#if !defined(__AVR_TINY__)
51
52#if !defined(__DOXYGEN__)
53
54#include "macros.inc"
55
56#define str_hi r25
57#define str_lo r24
58#define ltemp  r23
59#define rtemp  r22
60
61    ASSEMBLY_CLIB_SECTION
62    .global _U(strrev)
63    .type   _U(strrev), @function
64
65_U(strrev):
66	X_movw	XL, str_lo	; X is start of string
67	X_movw	ZL, str_lo	; Z becomes end of string
68  ; find end of string
691:	mov	rtemp, ltemp	; to obtain right nonzero character
70	ld	ltemp, Z+
71	tst	ltemp
72	brne	1b
73	sbiw	ZL, 2		; to last nonzero byte
74	rjmp	3f
75  ; swap bytes
762:	ld	ltemp, X
77	st	X+, rtemp
78	st	Z, ltemp
79	ld	rtemp, -Z
803:	cp	XL, ZL
81	cpc	XH, ZH
82	brlo	2b
83
84	ret
85
86    .size _U(strrev), . - _U(strrev)
87
88#endif /* not __DOXYGEN__ */
89
90#endif /* !defined(__AVR_TINY__) */
91