Index: common/asmdef.h
===================================================================
--- common/asmdef.h	(revision 2534)
+++ common/asmdef.h	(working copy)
@@ -206,6 +206,25 @@ .err	; Invalid src arg in X_movw macro.
 #endif
 .endm
 
+/* Macro 'X_sbiw' extends SBIW instruction for AVR_TINY chips. */
+.macro	X_sbiw	dst,val
+#if !defined (__AVR_TINY__)
+	sbiw	\dst,\val
+#else
+  REGNO	.L__sbiw_dst, \dst
+  .if	.L__sbiw_dst < 0
+    .exitm
+  .endif
+  .if	.L__sbiw_dst % 1
+    .err	; Invalid register arg in X_sbiw macro.
+    .exitm
+  .endif
+
+	subi	.L__sbiw_dst, \val
+	sbic	.L__sbiw_dst + 1, 0
+#endif /* AVR_TINY */
+.endm
+                  
 /* Macro 'X_lpm' extends enhanced lpm instruction for classic chips.
    Usage:
 	X_lpm	reg, dst
Index: common/macros.inc
===================================================================
--- common/macros.inc	(revision 2534)
+++ common/macros.inc	(working copy)
@@ -225,6 +225,45 @@ .macro  X_movw dst src
 	.endif
 .endm
 
+
+.macro  X_sbiw dst val
+	.L_sbiw_dst = -1
+	.L_sbiw_n = 24
+	.irp  reg,	r24,r26,r28,r30
+		.ifc  \reg,\dst
+			.L_sbiw_dst = .L_sbiw_n
+		.endif
+		.L_sbiw_n = .L_sbiw_n + 2
+	.endr
+	.L_sbiw_n = 24
+	.irp  reg,	R24,R26,R28,R30
+		.ifc  \reg,\dst
+			.L_sbiw_dst = .L_sbiw_n
+		.endif
+		.L_sbiw_n = .L_sbiw_n + 2
+	.endr
+	.if   .L_sbiw_dst < 0
+		.L_sbiw_n = 24
+		.rept   4
+			.if \dst == .L_sbiw_n
+				.L_sbiw_dst = .L_sbiw_n
+			.endif
+			.L_sbiw_n = .L_sbiw_n + 2
+		.endr
+	.endif
+	.if   (.L_sbiw_dst < 0)
+		.err    ; Invalid 'X_sbiw' arg.
+	.endif
+                
+#if !defined (__AVR_TINY__)
+	sbiw    \dst, \val
+#else
+	subi    (.L_sbiw_dst), \val
+	sbci    (.L_sbiw_dst)+1, 0
+#endif
+.endm
+
+
 /* Macro 'X_lpm' extends enhanced lpm instruction for classic chips.
    Usage:
 	X_lpm	reg, dst
Index: libc/misc/itoa.S
===================================================================
--- libc/misc/itoa.S	(revision 2534)
+++ libc/misc/itoa.S	(working copy)
@@ -29,7 +29,6 @@
 /* $Id$	*/
 
 #if	!defined (__DOXYGEN__)
-#if	!defined (__AVR_TINY__)
 
 #include "asmdef.h"
 
@@ -60,5 +59,4 @@ ENTRY	__itoa
 
 ENDFUNC
 
-#endif	/* !__AVR_TINY__ */
 #endif	/* !__DOXYGEN__ */
Index: libc/misc/itoa_ncheck.S
===================================================================
--- libc/misc/itoa_ncheck.S	(revision 2534)
+++ libc/misc/itoa_ncheck.S	(working copy)
@@ -29,7 +29,6 @@
 /* $Id$	*/
 
 #if	!defined (__DOXYGEN__)
-#if	!defined (__AVR_TINY__)
 
 #include "asmdef.h"
 
@@ -60,5 +59,4 @@ ENTRY	__itoa_ncheck
 
 ENDFUNC
 
-#endif	/* !__AVR_TINY__ */
 #endif	/* !__DOXYGEN__ */
Index: libc/misc/utoa.S
===================================================================
--- libc/misc/utoa.S	(revision 2534)
+++ libc/misc/utoa.S	(working copy)
@@ -29,7 +29,6 @@
 /* $Id$	*/
 
 #if	!defined (__DOXYGEN__)
-#if	!defined (__AVR_TINY__)
 
 #include "asmdef.h"
 
@@ -60,5 +59,4 @@ ENTRY	__utoa
 
 ENDFUNC
 
-#endif	/* !__AVR_TINY__ */
 #endif	/* !__DOXYGEN__ */
Index: libc/misc/utoa_ncheck.S
===================================================================
--- libc/misc/utoa_ncheck.S	(revision 2534)
+++ libc/misc/utoa_ncheck.S	(working copy)
@@ -29,7 +29,6 @@
 /* $Id$	*/
 
 #if	!defined (__DOXYGEN__)
-#if	!defined (__AVR_TINY__)
 
 #include "asmdef.h"
 
@@ -77,7 +76,7 @@ ENTRY	__utoa_common
 	st	Z+, digit
 
     ; Iterate until all digits are sucked out of VAL.
-	sbiw	val_lo, 0
+	X_sbiw	val_lo, 0
 	brne	1b
 
     ; Store the sign (if any)
@@ -93,5 +92,4 @@ ENTRY	__utoa_common
 
 ENDFUNC
 
-#endif	/* !__AVR_TINY__ */
 #endif	/* !__DOXYGEN__ */
Index: libc/string/memchr.S
===================================================================
--- libc/string/memchr.S	(revision 2534)
+++ libc/string/memchr.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -56,7 +54,12 @@ _U(memchr):
 	ld	__tmp_reg__, Z+
 	cp	__tmp_reg__, val_lo
 	brne	.L_memchr_loop
-	sbiw	ZL, 1		; undo post-increment
+	; undo post-increment
+#if !defined(__AVR_TINY__)
+	sbiw	ZL, 1
+#else
+	ld	__tmp_reg__, -Z
+#endif /*!__AVR_TINY__*/
 	X_movw	ret_lo, ZL
 	ret
 .L_memchr_notfound:
@@ -67,5 +70,3 @@ _U(memchr):
 	.size	_U(memchr), .L_memchr_end - _U(memchr)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/memrchr.S
===================================================================
--- libc/string/memrchr.S	(revision 2534)
+++ libc/string/memrchr.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "asmdef.h"
@@ -44,13 +42,13 @@
 
 ENTRY memrchr
 	X_movw	ZL, len_lo
-	adiw	ZL, 0
+	X_sbiw	ZL, 0
 	breq	2f			; len is 0, return NULL
 	add	ZL, src_lo
 	adc	ZH, src_hi
 
-1:	ld	r0, -Z
-	cp	r0, val
+1:	ld	__tmp_reg__, -Z
+	cp	__tmp_reg__, val
 	breq	2f			; val is found
 	subi	len_lo, lo8(1)
 	sbci	len_hi, hi8(1)
@@ -62,5 +60,3 @@ ENTRY memrchr
 ENDFUNC
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strcat.S
===================================================================
--- libc/string/strcat.S	(revision 2534)
+++ libc/string/strcat.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -49,7 +47,12 @@ _U(strcat):
 	ld	__tmp_reg__, X+
 	tst	__tmp_reg__
 	brne	.L_strcat_skip
-	sbiw	XL, 1		; undo post-increment
+	; undo post-increment
+#if !defined(__AVR_TINY__)
+	sbiw	XL, 1
+#else
+	ld	__tmp_reg__, -X
+#endif /* !__AVR_TINY__ */
 .L_strcat_loop:
 	ld	__tmp_reg__, Z+
 	st	X+, __tmp_reg__
@@ -61,5 +64,3 @@ _U(strcat):
 	.size	_U(strcat), .L_strcat_end - _U(strcat)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strchr.S
===================================================================
--- libc/string/strchr.S	(revision 2534)
+++ libc/string/strchr.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -57,12 +55,15 @@ _U(strchr):
 	clr	ret_hi
 	ret
 .L_strchr_found:
-	sbiw	ZL, 1		; undo post-increment
+	; undo post-increment
+#if !defined(__AVR_TINY__)
+	sbiw	ZL, 1
+#else
+	ld	__tmp_reg__, -Z
+#endif /*!__AVR_TINY__*/
 	X_movw	ret_lo, ZL
 	ret
 .L_strchr_end:
 	.size	_U(strchr), .L_strchr_end - _U(strchr)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strchrnul.S
===================================================================
--- libc/string/strchrnul.S	(revision 2534)
+++ libc/string/strchrnul.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "asmdef.h"
@@ -42,16 +40,18 @@
 ENTRY strchrnul
 	X_movw	ZL, src_lo
 
-1:	ld	r0, Z+
-	cp	r0, val
-	cpse	r0, __zero_reg__
+1:	ld	__tmp_reg__, Z+
+	cp	__tmp_reg__, val
+	cpse	__tmp_reg__, __zero_reg__
 	brne	1b
-
-	sbiw	ZL, 1		; undo post-increment
+	; undo post-increment
+#if !defined(__AVR_TINY__)
+	sbiw	ZL, 1
+#else
+	ld	__tmp_reg__, -Z
+#endif /* !__AVR_TINY__ */
 	X_movw	ret_lo, ZL
 	ret
 ENDFUNC
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strlcat.S
===================================================================
--- libc/string/strlcat.S	(revision 2534)
+++ libc/string/strlcat.S	(working copy)
@@ -35,8 +35,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "asmdef.h"
@@ -84,7 +82,7 @@ ENTRY strlcat
 	ld	__tmp_reg__, X+
 	tst	__tmp_reg__
 	brne	1b
-	sbiw	XL, 1
+	X_sbiw	XL, 1
 	rjmp	3f
 
   ; copy loop
@@ -114,5 +112,3 @@ ENTRY strlcat
 
 ENDFUNC
 #endif /* not __DOXYGEN__ */
-
-#endif /* not __AVR_TINY__ */
Index: libc/string/strncat.S
===================================================================
--- libc/string/strncat.S	(revision 2534)
+++ libc/string/strncat.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -51,7 +49,7 @@ _U(strncat):
 	ld	__tmp_reg__, X+
 	tst	__tmp_reg__
 	brne	.L_strncat_skip
-	sbiw	XL, 1		; undo post-increment (point the the NUL)
+	X_sbiw	XL, 1		; undo post-increment (point to the NUL)
 .L_strncat_loop:
 	subi	len_lo, lo8(1)
 	sbci	len_hi, hi8(1)
@@ -70,5 +68,3 @@ _U(strncat):
 	.size	_U(strncat), .L_strncat_end - _U(strncat)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strpbrk.S
===================================================================
--- libc/string/strpbrk.S	(revision 2534)
+++ libc/string/strpbrk.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #ifndef	__DOXYGEN__
 
 #include "macros.inc"
@@ -56,18 +54,20 @@ _U(strpbrk):
 	breq	3f
   ; check an occurance
 	X_movw	ZL, acc_lo
-2:	ld	r0, Z+
-	cp	r0, ret_lo
-	cpse	r0, __zero_reg__
+2:	ld	__tmp_reg__, Z+
+	cp	__tmp_reg__, ret_lo
+	cpse	__tmp_reg__, __zero_reg__
 	brne	2b
 	brne	1b		; branch if end of accept[] is reached
   ; OK, is found
+#if !defined(__AVR_TINY__)
 	sbiw	XL, 1
+#else
+	ld	__tmp_reg__, -X
+#endif /* !__AVR_TINY__ */
 	X_movw	ret_lo, XL
 3:	ret
 
 	.size _U(strpbrk), . - _U(strpbrk)
 
 #endif	/* !__DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strrchr.S
===================================================================
--- libc/string/strrchr.S	(revision 2534)
+++ libc/string/strrchr.S	(working copy)
@@ -28,8 +28,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -57,11 +55,9 @@ _U(strrchr):
 .L_strrchr_noteq:
 	tst	__tmp_reg__
 	brne	.L_strrchr_loop
-	sbiw	ret_lo, 1	; undo post-increment
+	X_sbiw	ret_lo, 1	; undo post-increment
 	ret
 .L_strrchr_end:
 	.size	_U(strrchr), .L_strrchr_end - _U(strrchr)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strrev.S
===================================================================
--- libc/string/strrev.S	(revision 2534)
+++ libc/string/strrev.S	(working copy)
@@ -37,8 +37,6 @@
      Created by Reiner Patommel
 */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -60,7 +58,7 @@ _U(strrev):
 	ld	ltemp, Z+
 	tst	ltemp
 	brne	1b
-	sbiw	ZL, 2		; to last nonzero byte
+	X_sbiw	ZL, 2		; to last nonzero byte
 	rjmp	3f
   ; swap bytes
 2:	ld	ltemp, X
@@ -76,5 +74,3 @@ _U(strrev):
     .size _U(strrev), . - _U(strrev)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strstr.S
===================================================================
--- libc/string/strstr.S	(revision 2534)
+++ libc/string/strstr.S	(working copy)
@@ -37,8 +37,6 @@
      Philip Soeberg <philip@soeberg.net>
 */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "macros.inc"
@@ -50,7 +48,7 @@ #include "macros.inc"
 
 #define chr1   r20
 #define	beg2   r21	/* begin of s2: s2[0]	*/
-#define chr2   r0
+#define chr2   __tmp_reg__
 
 ASSEMBLY_CLIB_SECTION
 .global _U(strstr)
@@ -89,12 +87,10 @@ _U(strstr):
 	ldi	s1_lo, lo8(1)
 	ldi	s1_hi, hi8(1)
 .L_match:
-	sbiw	s1_lo, 1	; restore after post-increment
+	X_sbiw	s1_lo, 1	; restore after post-increment
 .L_ret:
 	ret
 
 .size _U(strstr), . - _U(strstr)
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
Index: libc/string/strtok_r.S
===================================================================
--- libc/string/strtok_r.S	(revision 2534)
+++ libc/string/strtok_r.S	(working copy)
@@ -30,8 +30,6 @@
 
 /* $Id$ */
 
-#if !defined(__AVR_TINY__)
-
 #if !defined(__DOXYGEN__)
 
 #include "asmdef.h"
@@ -87,13 +85,16 @@
 #define dch	r18
 
 ENTRY strtok_r
+#if defined(__AVR_TINY__)
+	push	dch
+#endif /* __AVR_TINY__ */
 	X_movw	ZL, lst_lo
 	ld	XL, Z+			; X = *last
 	ld	XH, Z
   ; check str
-	sbiw	str_lo, 0
+	X_sbiw	str_lo, 0
 	brne	1f
-	sbiw	XL, 0
+	X_sbiw	XL, 0
 	breq	.Lret			; end of string
 	X_movw	str_lo, XL		; continue parsing
 
@@ -119,7 +120,11 @@ ENTRY strtok_r
 	cp	dch, __tmp_reg__	; __tmp_reg__ != 0
 	brne	7f
 	st	-X, __zero_reg__
+#if !defined(__AVR_TINY__)
 	adiw	XL, 1
+#else
+	ld	__tmp_reg__, X+
+#endif /* __AVR_TINY__ */
 	rjmp	.Lret
 7:	tst	dch
 	brne	6b
@@ -134,10 +139,11 @@ ENTRY strtok_r
 .Lret:	X_movw	ZL, lst_lo		; *last = X
 	st	Z+, XL
 	st	Z, XH
+#if defined(__AVR_TINY__)
+	pop	dch
+#endif /* __AVR_TINY__ */
 	ret
 
 ENDFUNC
 
 #endif /* not __DOXYGEN__ */
-
-#endif /* !defined(__AVR_TINY__) */
