Article View: comp.sys.intel
Article #1672Re: strcpy() on a 386
From: Henry Perkins
Date: Wed, 14 Oct 1987 17:38
Date: Wed, 14 Oct 1987 17:38
42 lines
2432 bytes
2432 bytes
In article <2804@bobkat.UUCP>, m5@bobkat.UUCP (Mike McNally ) writes:
> How do clever iAPX 386 users (or iAPX *8[68] users for that matter)
> implement C-style string copies? Seems to me that there are two
> approaches:
>
> loop: lodsb
> stosb
> cmp AL, 0
> loopne loop
>
> The other approach would be to SCASB to the end of the source string,
> then move that many bytes with REP MOVSB. Neither way is particularly
> appealing. So like, how do the pros do it? What's the deal?
Here's a quick comparison of the two approaches above, using
8086 cycle counts. I've left out the overhead of setting up
all the appropriate registers before getting to the loop itself.
Repeat_Here: LODSW ; 12 cycles
OR AL,AL ; 3 cycles
JZ Exit ; 4 cycles to stay in the loop
STOSW ; 11 cycles
OR AH,AH ; 3 cycles
LOOPNZ Repeat_Here ; 19 cycles to stay in the loop
Exit: ; Total: 52 cycles per 2 characters
REP SCASB ; 15 cycles per character
REP MOVSW ; 17 cycles per 2 characters
; Total: 47 cycles per 2 characters
But why try to be so clever? My approach is MUCH simpler: just
move a number of characters equal to the lesser of the declared
lengths of the two strings. Since REP MOVSW is only 17 cycles
per 2 characters, this is more efficient whenever the shorter of
the strings is longer than 1/3 its declared maximum length. On
the average this wins 2 out of 3 times, and the code is both
smaller and easier to read.
--
{hplabs,amdahl,ames}!bnrmtv!perkins --Henry Perkins
It is better never to have been born. But who among us has such luck?
One in a million, perhaps.
Technical Details
Message-ID:
<2811@bnrmtv.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!sri-unix!ctnews!pyramid!hplabs!nsc!amdahl!bnrmtv!perkinsReferences:
<2804@bobkat.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!sri-unix!ctnews!pyramid!hplabs!nsc!amdahl!bnrmtv!perkins From: perkins@bnrmtv.UUCP (Henry Perkins) Newsgroups: comp.sys.intel Subject: Re: strcpy() on a 386 Message-ID: <2811@bnrmtv.UUCP> Date: Wed, 14-Oct-87 13:38:23 EDT Article-I.D.: bnrmtv.2811 Posted: Wed Oct 14 13:38:23 1987 Date-Received: Sat, 17-Oct-87 02:58:32 EDT References: <2804@bobkat.UUCP> Distribution: na Organization: BNR Inc., Mountain View, California Lines: 42 Summary: Don't try to be too clever; just copy the declared length.