Re: pls explain strcpy() assembly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 18/01/06, Rajaram Suryanarayanan <rajaram_linux@xxxxxxxxx> wrote:
> Hi,
>
>  Can anybody pls explain me step by step what is done by the assembly code
> below ? Particularly, I want to know how "src" and "dest" get substituted in
> the assembly code.
>
>   static inline char * strcpy(char * dest,const char *src)
>  {
>  int d0, d1, d2;
>  __asm__ __volatile__(
>          "1:\tlodsb\n\t"
>          "stosb\n\t"
>          "testb %%al,%%al\n\t"
>          "jne 1b"
>          : "=&S" (d0), "=&D" (d1), "=&a" (d2)
>          :"0" (src),"1" (dest) : "memory");
>  return dest;
>  }

The esi (source index register) is referenced as S. It is set to point
to what src is pointing to. The "0" in front of src means look at the
previous list (set of output operands) and use the 0th element.
Similarly, dest references D which is the
(destination index register).

"1:\tlodsb\n\t"
 1 is a label. lodsb loads a byte from what esi points to and
increments esi. The byte should be in al register.

stosb\n\t
stosb moves the byte in al into the location pointed to by edi (D).
edi will also be incremented .

testb %%al,%%al\n\t
To test for end of string test the byte in al register. If it is not
null jump back to the
top of the loop and repeat.
This will continue till you hit the end of the string (NUL) in which
case the jump will not take place. At this point the string has been
copied.

I am not sure why we need d0, d1 and d2. Can anyone give a hint? Thanks.

The syntax will make sense if you read this
http://www-128.ibm.com/developerworks/linux/library/l-ia.html

Hope this helps.
vijay

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux