title: How to link a C object file with a Assembly Language object file?
body: <p>I am having trouble linking 2 object files one of which was generated from an Assembly Language Source File and another that was generated from a C Source file.</p>
  
  <p>C source code:</p>
  
  <pre><code>//main2.c
  extern int strlength(char *);
  int main(){
      char * test = "hello";
      int num = strlength(test);
      return num;
  }
  </code></pre>
  
  <p>Assembly source code:</p>
  
  <pre><code>#strlength.s
  .include "Linux32.s"
  
  .section .text
  .globl strlength
  .type strlength, @function
  strlength:
   pushl %ebp
   movl %esp, %ebp
   movl $0, %ecx
   movl 8(%ebp), %edx
  read_next_byte:
   movb (%edx), %al
   cmpb $END_OF_FILE, %al
   jle end
   incl %edx
   incl %ecx
   jmp read_next_byte
  end:
   movl %ecx, %eax
   popl %ebp
   ret
  </code></pre>
  
  <p>When I compile and run using 'gcc' like this:</p>
  
  <pre><code>gcc main2.c strlength.s -m32 -o test
  ./test
  echo $?
  </code></pre>
  
  <p>I get 5 which is correct. However when I compile/assemble separately and then link with 'ld'
  like this:</p>
  
  <pre><code>as strlength.s --32 -o strlength.o
  cc main2.c -m32 -o main2.o
  ld -melf_i386 -e main main2.o strlength.o -o test
  ./test
  </code></pre>
  
  <p>I get a segmentation fault. What is causing this? Am I not following the C calling convention 100% correctly?</p>