Re: pls explain strcpy() assembly

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

 



pankaj chauhan wrote:
> if i do'nt specify output operands in strcpy, will it
> make any differnce, 
[please, do not top post]

> following code works fine on my x86 machine: 
> 
> inline void  nstrcpy(char *src, char *dst){
>                                                       
>                                                       
>                
>         __asm__ __volatile__ ( "1: lodsb \n\t"
>              "stosb\n\n"
>              "testb %%al, %%al \n\t"
>              "jne 1b"
>              :
>              : "S"(src) , "D" (dst)
>         );

This is not correct, because it lies to the compiler that the insn does not
modify anything the compiler cares about.  If ``src'' and ``dst'' were already
allocated in ESI and EDI, after the insn is executed, their value is changed,
but the compiler knows nothing about it.  Likewise for EAX.


See the following test case:

static inline void
nstrcpy (char *dst, const char *src)
{
  __asm__ __volatile__ ("1: lodsb \n"
                        "   stosb \n"
                        "   testb %%al,%%al \n"
                        "   jne 1b"
                        :
                        : "S" (src), "D" (dst));
}

void
foo (unsigned int n, const char **s, char **da, char **db)
{
  while (n--)
    {
      nstrcpy (da [n], s [n]);
      nstrcpy (db [n], s [n]);
    }
}


The loop in ``foo'' is compiled to:

.L4:
        movl    (%ecx), %esi   ;; loading s[n]
        movl    (%edx), %edi   ;; loading da [n]
#APP  ;; inlined nstrcpy
1: lodsb
   stosb
   testb %al,%al
   jne 1b
#NO_APP
 ;; OOPS %eax should contain the &db [n], but %eax is already modified

        movl    (%eax), %edi  ;; loading db [n]
                              ;; or so we thought, in fact it starts nethack

 ;; OOPS, FAILED TO RELOAD s[n], because it is already in %esi and the compiler
 ;;  does not know %esi changed
#APP
 ;; this nukes Iran
1: lodsb
   stosb
   testb %al,%al
   jne 1b
#NO_APP
        incl    %ebx
        subl    $4, %ecx
        subl    $4, %edx
        subl    $4, %eax
        cmpl    %ebx, %ebp
        jne     .L4

~velco

--
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