I'm new to AT&T assembly language. I read this from linux source code(kernel 2.4.0 include/asm_i386/string).

static inline void * __memcpy(void * to, const void * from, size_t n)
{
int d0, d1, d2;
__asm__ __volatile__(
	"rep ; movsl\n\t"
	"testb $2,%b4\n\t"
	"je 1f\n\t"
	"movsw\n"
	"1:\ttestb $1,%b4\n\t"
	"je 2f\n\t"
	"movsb\n"
	"2:"
	: "=&c" (d0), "=&D" (d1), "=&S" (d2)
	:"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
	: "memory");
return (to);
}

I wonder whether the temporary variables d0,d1,d2 are necessary here.
Can it be this way?

static inline void * __memcpy(void * to, const void * from, size_t n)
{

__asm__ __volatile__(
	"rep ; movsl\n\t"
	"testb $2,%b4\n\t"
	"je 1f\n\t"
	"movsw\n"
	"1:\ttestb $1,%b4\n\t"
	"je 2f\n\t"
	"movsb\n"
	"2:"
	: 
	:"c" (n/4), "&q" (n),"D" ((long) to),"S" ((long) from)
	: "memory");
return (to);

If can, what the differences are?
Please help...thanks

I usually find that there is a good reason, but in this case I can't see any reason to have those temporary variables, as they are unused in any other way. Perhaps they are just a relic when the original author was debugging the routine...

Also, this routine is amazingly cavalier about the state of the Direction Flag. There should be a "cld\n\t" at the front. (Check to see how it is used, first, before changing it though... I think the standard C library expects this to work in the forward direction though.)

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.