String copy idiom.

Chris Torek chris at umcp-cs.UUCP
Sat Mar 16 15:21:04 AEST 1985


> Note the following VMS Macro code from a routine in the Decus C library.

>	.entry	strcpy,^M<r2,r3,r4,r5>
>	movl	8(ap),r2	; source string
>	locc	#0,#-1,(r2)	; find null at end of source
>	subl2	r2,r1		; length of source
>	incl	r1		; plus 1 for the null byte
>	movc3	r1, at 8(ap), at 4(ap); copy the string
>	movl	4(ap),r0	; r0 -> destination
>	.end

Unfortunately, this fails for strings longer than 65535 characters.
This can be corrected by looping over the locc and movc3 instructions
until the final 64K segment is reached (I believe locc sets the
condition codes depending on whether it found the character or not).

Here's a stab at it (without referring to the "black book" so I don't
know if I've got things right):

_strcpy:.globl	_strcpy
	.word	0x0
	movl	8(ap),r3	# source => r3
	movl	4(ap),r5	# dest => r5 (movc3 requires this arrangement)
	brb	1f		# go count up length
0:	movc3	$65534,(r3),(r5)# copy current 64K segment
1:	locc	$0,$65534,(r3)	# find null
	bne	0b		# if not found, copy next 64K seg
	subl2	r3,r1		# r1 = len
	incl	r1		# +1 for null, without overflow (65534+1 max)
	movc3	r1,(r3),(r5)	# move final chunk
	movl	4(ap),r0	# dest => r0 as return val
	ret

(I'm not sure if movc3'ing 65532 bytes would be better, so as to stay
longword aligned when the arguments were originally....)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.lang.c mailing list