title: Malloc and strdup(self implementation) in assembly
body: <p><strong>Update: Fixed my C code example. I'm copying from a char* to another char* in assembly. I need to copy by value and then return the address of the new char* to the caller.</strong></p>
  
  <p>I'm trying to create an equivalent of the following C code in assembly but gdb/valgrind is reporting that I'm attempting to free something that is already "freed".</p>
  
  <p>Code in C:</p>
  
  <pre><code>char* func( int x, char* name){
  
      namestr = (char *) malloc( strlen(name) + 1 );
      nameestr = strdup( namestr, strlen(name) +1 );
      free( name ); //Just showing what I plan to do later.
  
      return namestr;    
  }
  
  int main( ){
      char* name = (char *) malloc( 10 );
      *name = "0123456789"
  
      char* some_string = func( 10, name );
      free( some_string );
  }
  </code></pre>
  
  <p>Code in Assembly:</p>
  
  <pre><code>new_student:
      pushl   %ebp
      movl    %esp, %ebp
      pushl   %edi
      pushl   %esi
      subl    $20, %esp
      movl    12(%ebp), %ecx
      movl    %ecx, %edi
      movl    %ecx, -12(%ebp)
  
  ;get length and allocate the appropriate space
  .STR_ALLOCATE:
      movl    $0, %eax
      movl    $-1, %ecx
      repnz   scasb
      notl    %ecx
      subl    $1, %eax
      addl    $1, %eax
      movl    %ecx, -8(%ebp)
      pushl   %ecx
      call    allocate
      add     $4, %esp
      movl    %eax, -16(%ebp)
  
  ;copy the string byte by byte
  .STR_CPY:
      movl    -8(%ebp), %ecx
      movl    %eax, %edi
      movl    12(%ebp), %esi
      rep     movsb
      movl    -12(%ebp), %eax
  
  .END:
      popl    %esi
      popl    %edi
      leave
      ret
  </code></pre>
  
  <p>Can anyone give me some hints on what I'm doing wrong?</p>