Re: faster strcpy()

Riley Williams (rhw@bigfoot.com)
Fri, 24 Apr 1998 12:11:54 +0100 (BST)


Hi Chris.

> As to why the above is faster in userland, that seems really odd. I
> would assume the libc string copy to be something like:

> char *strcpy(char *a,char*b)
> {
> char c = b;
> while(*a++ = *b++);
> return c;
> }

That would return the wrong value, but replace the "char c=b" with
"char c=a" and you have the standard implementation.

> Which I can't see how it could be slower than what you are using...

Perhaps GCC isn't optimising it correctly? If my memory of 86
assembler is correct, it should turn into something like this:

Q> ; strcpy: Takes two parameters, being the target and source
Q> ; addresses of the strings concerned, and copies all
Q> ; bytes from the source to the target until a zero
Q> ; byte has been copied. Assumes a 4-byte return address.
Q> __strcpy:
Q> push bp
Q> push cx
Q> mov bp,sp
Q> mov di,[bp+6]
Q> mov si,[bp+8]
Q> mov cx,-1
Q> xor ax,ax
Q> repne movsb
Q> mov ax,[bp+6]
Q> pop cx
Q> pop bp
Q> retf

Obviously, I've used 16-bit registers in the above, and I'm also
writing from memory, not from any reference books, but it shouldn't be
far out...

Best wishes from Riley.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu