RE: faster strcpy()

Mikael Klasson (mikael.klasson@sciron.se)
Fri, 24 Apr 1998 19:33:33 +0200


> > > while(*a++=*b++); perhaps?
> >
> > No, that's how it was before. This copies byte at time and is slow.
> > memcpy is fast. If we could use the same technique as memcpy uses
> > to copy strings and at the same time check the terminating 0...
> > I don't see the answer myself, does anybody see?
>
> I don't know c or i386 assembler, but there are string commands. Would
one of
> them do it? I don't have my assembler book handy. Is there a store and
> continue while zero?

No. There's "rep movsX" (where X is b/w/d) for moving data but that one
requires ecx to be initialized to the number of items to copy and
doesn't care much about what it's copying. A "lodsd" and "stosd"
followed by the test routine someone posted here earlier and a
conditional jump seems like the best solution to me (provided that the
gain of copying dwords make up for the time the test routine takes to
execute compared to a "lodsb","stosb","or al,al", "jnz lop" version).

[some time later]
Copying dwords certainly seem to make up for the overhead time of the
test routine. Even when implementing it with a .com file under dos (16
bit seg and _lots_ of penalties when operating on 32 bit data) it's a
few cycles faster per byte copied than the latter version (copying byte
by byte). In a proper implementation, with 32 bit segment, the
difference should be quite a bit bigger.

I don't guarantee that my implementation is bug-free (nothing is). Feel
free to check it out if you like, I've attached it to this mail. No,
it's not optimized.

--
Mikael

;------------- start of test1.asm .model tiny .386 .code org 100h _main:

db 0fh,31h mov time1,eax

mov cx,10000 mainlop: ;----------------------------------------------------------- ;----------------------------------------------------------- ;-----------------------------------------------------------

lea si,s1 lea di,s2

lop: lodsb stosb or al,al jnz lop

;----------------------------------------------------------- ;----------------------------------------------------------- ;-----------------------------------------------------------

loop mainlop

db 0fh,31h

sub eax,time1 call _putd

ret

_putd: push eax shr eax,16 call _putw pop eax _putw: push eax mov al,ah call _putb pop eax _putb: push eax shr al,4 call _putn pop eax _putn: push eax and al,0fh add al,90h daa adc al,40h daa int 29h pop eax ret

time1 dd ?

s1 db 'Testing mem copy routines...',0 ; 28 chars s2 db 256 dup(?)

end _main

;------------- end of test1.asm

in the second version I replaced the inner loop with

;----------------------------------------------------------- ;----------------------------------------------------------- ;----------------------------------------------------------- lea si,s1 lea di,s2

lop: lodsd stosd

mov ebx,eax sub eax,01010101h not ebx and eax,ebx and eax,80808080h

jz lop ;----------------------------------------------------------- ;----------------------------------------------------------- ;-----------------------------------------------------------

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