title: Segmentation Fault when getting the max of three numbers in Assembly x86
body: <p>I am trying to get the max of three numbers using C to call a method in Assembly 32 bit AT &amp; T. When the program runs, I get a segmentation fault(core dumped) error and cannot figure out why. My input has been a mix of positive/negative numbers and 1,2,3, both with the same error as a result.</p>
  
  <p>Assembly</p>
  
  <pre><code># %eax - first parameter
  # %ecx - second parameter
  # %edx - third parameter
  
  .code32
  .file "maxofthree.S"
  .text 
  .global maxofthree
  .type maxofthree @function
  
  maxofthree:
      pushl %ebp # old ebp
      movl %esp, %ebp # skip over
      movl 8(%ebp), %eax # grab first value
      movl 12(%ebp), %ecx # grab second value
      movl 16(%ebp), %edx # grab third value
      #test for first
      cmpl %ecx, %eax # compare first and second
      jl firstsmaller # first smaller than second, exit if
      cmpl %edx, %eax # compare first and third
      jl firstsmaller # first smaller than third, exit if
      leave # reset the stack pointer and pop the old base pointer
      ret # return since first &gt; second and first &gt; third
      firstsmaller:  # first smaller than second or third, resume comparisons
      #test for second and third against each other
      cmpl %edx, %ecx # compare second and third
      jg secondgreatest # second is greatest, so jump to end
      movl %eax, %edx # third is greatest, move third to eax
      leave # reset the stack pointer and pop the old base pointer
      ret # return third
      secondgreatest: # second &gt; third
      movl %ecx, %eax #move second to eax
      leave # reset the stack pointer and pop the old base pointer
      ret # return second
  </code></pre>
  
  <p>C code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;inttypes.h&gt;
  long int maxofthree(long int, long int, long int);
  
  int main(int argc, char *argv[]) {
  if (argc != 4) {
      printf("Missing command line arguments. Instructions to"
              " execute this  program:- .\a.out &lt;num1&gt; &lt;num2&gt; &lt;num3&gt;");
      return 0;
  }
  
  long int x = atoi(argv[1]);
  long int y = atoi(argv[2]);
  long int z = atoi(argv[3]);
  printf("%ld\n", maxofthree(x, y, z)); // TODO change back to (x, y, z)
  }
  </code></pre>