Welcome to mirror list, hosted at ThFree Co, Russian Federation.

strcpy.S « i960 « machine « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed8bb72345f7e9aae980e4d35e115ecf92278ee3 (plain)
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
/*******************************************************************************
 * 
 * Copyright (c) 1993 Intel Corporation
 * 
 * Intel hereby grants you permission to copy, modify, and distribute this
 * software and its documentation.  Intel grants this permission provided
 * that the above copyright notice appears in all copies and that both the
 * copyright notice and this permission notice appear in supporting
 * documentation.  In addition, Intel grants this permission provided that
 * you prominently mark as "not part of the original" any modifications
 * made to this software or documentation, and that the name of Intel
 * Corporation not be used in advertising or publicity pertaining to
 * distribution of the software or the documentation without specific,
 * written prior permission.
 * 
 * Intel Corporation provides this AS IS, WITHOUT ANY WARRANTY, EXPRESS OR
 * IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY
 * OR FITNESS FOR A PARTICULAR PURPOSE.  Intel makes no guarantee or
 * representations regarding the use of, or the results of the use of,
 * the software and documentation in terms of correctness, accuracy,
 * reliability, currentness, or otherwise; and you rely on the software,
 * documentation and results solely at your own risk.
 *
 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS,
 * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES
 * OF ANY KIND.  IN NO EVENT SHALL INTEL'S TOTAL LIABILITY EXCEED THE SUM
 * PAID TO INTEL FOR THE PRODUCT LICENSED HEREUNDER.
 * 
 ******************************************************************************/

	.file "strcpy.s"
#ifdef	__PIC
	.pic
#endif
#ifdef	__PID
	.pid
#endif
/*
 * (c) copyright 1988,1993 Intel Corp., all rights reserved
 */
/*
	procedure strcpy  (optimized assembler version for the 80960K series)
	procedure strcat  (optimized assembler version for the 80960K series)

	dest_addr = strcpy (dest_addr, src_addr)

	copy the null terminated string pointed to by src_addr to 
	the string space pointed to by dest_addr.  Return the original
	dest_addr.

	This routine will fail if the source and destination string
	overlap (in particular, if the end of the source is overlapped
	by the beginning of the destination).  The behavior is undefined.
	This is acceptable according to the draft C standard.

	Undefined behavior will also occur if the end of the source string
	(i.e. the terminating null byte) is in the last two words of the 
	program's allocated memory space.  This is so because strcpy fetches 
	ahead.  Disallowing the fetch ahead would impose a severe performance 
	penalty.

	Strategy:

	Fetch the source string and store the destination string by words
	until the null byte is encountered.  When the word with the null
	byte is reached, store it by bytes up through the null byte only.

	Tactics:

	1) Do NOT try to fetch and store the words in a word aligned manner 
	because, in my judgement, the performance degradation experienced due
 	to non-aligned accesses does NOT outweigh the time and complexity added
	by the preamble and convoluted body that would be necessary to assure 
	alignment.  This is supported by the intuition that most source and
	destination strings will be word aligned to begin with.


	procedure strcat

	dest_addr = strcat (dest_addr, src_addr)

	Appends the string pointed to by src_addr to the string pointed
	to by dest_addr.  The first character of the source string is
	copied to the location initially occupied by the trailing null
	byte of the destination string.  Thereafter, characters are copied
	from the source to the destination up thru the null byte that
	trails the source string.

	See the strcpy routine, above, for its caveats, as they apply here too.

	Strategy:

	Skip to the end (null byte) of the destination string, and then drop
	into the strcpy code.

	Tactics:

	Skipping to the null byte is Ldone by reading the destination string
	in long-words and scanbyte'ing them, then examining the bytes of the 
	word that contains the null byte, until the address of the null byte is
	known.  Then we drop into the strcpy routine.  It is probable (approx.
	three out of four times) that the destination string as strcpy sees
	it will NOT be word aligned (i.e. that the null byte won't be the
	last byte of a word).  But it is not worth the complication to that
	routine to force word aligned memory accesses to be gaurenteed.
*/
	.globl _strcpy, _strcat
	.globl __strcpy, __strcat
	.leafproc _strcpy,__strcpy
	.leafproc _strcat,__strcat
	.align    2
_strcat:
#ifndef __PIC
 	lda	Lrett,g14
#else
 	lda	Lrett-(.+8)(ip),g14
#endif
__strcat:
	mov	g14,g13		# preserve return address
	ldl	(g0),g4		# fetch first two words
	addo	8,g0,g2		# post-increment src word pointer
	lda	0xff,g3		# byte extraction mask

Lsearch_for_word_with_null_byte:
	scanbyte 0,g4		# check for null byte
	mov	g5,g7		# copy second word
	bo.f	Lsearch_for_null	# branch if null found 
	scanbyte 0,g7		# check for null byte
	ldl	(g2),g4		# fetch next pair of word of src
	addo	8,g2,g2		# post-increment src word pointer
	bno	Lsearch_for_word_with_null_byte	# branch if null not found yet

	subo	4,g2,g2		# back up the byte pointer
	mov	g7,g4		# move word with null to search word
Lsearch_for_null:
	subo	9,g2,g5		# back up the byte pointer
Lsearch_for_null.a:
	and	g4,g3,g6	# extract byte
	cmpo	0,g6		# is it null?
	addo	1,g5,g5		# bump src byte ptr
	shro	8,g4,g4		# shift word to position next byte
	bne	Lsearch_for_null.a
	b	Lend_of_dest_found

_strcpy:
#ifndef __PIC
 	lda	Lrett,g14
#else
 	lda	Lrett-(.+8)(ip),g14
#endif
__strcpy:
	mov	g0, g5
Lend_of_dest_found:
	ld	(g1), g2	# fetch first word of source 
	mov	g14,g6		# preserve return address
	lda	0xff, g3	# byte extraction mask = 0xff;
Lwloop:				# word copying loop
	addo	4, g1, g1	# post-increment source ptr
	scanbyte 0, g2		# does source word contain null byte?
	mov	g2, g4		# save a copy of the source word
	be	Lcloop		# branch if null present
	ld	(g1), g2	# pre-fetch next word of source
	st	g4, (g5)	# store current word
	addo	4, g5, g5	# post-increment dest ptr
	b	Lwloop

Lcloop:				# character copying loop
	and	g3, g4, g14	# extract next char
	shro	8, g4, g4	# position word for next byte extraction
	cmpo	0, g14 		# is it null?
	stob	g14, (g5)	# store the byte
	addo	1, g5, g5	# post-increment dest ptr
	bne	Lcloop		# quit if null encountered

	bx	(g6)		# g0 = dest string address; g14 = 0
Lrett:	
	ret