title: How to write a swap function in assembly?
body: <p>I've been trying to figure out how to write a x86 GAS swap function for my program. I know its easier to do xchg or just write it C, but I want to be able to write it out anyways.</p>
  
  <p>On my 1st midterm we were given this as as swap function:</p>
  
  <pre><code>movl 8(%ebp), %edx
  movl 12(%ebp), %ecx
  movl (%edx), %ebx
  movl (%ecx), %eax
  movl %eax, (%edx)
  movl %ebx, (%ecx)
  </code></pre>
  
  <p>but I receive a segmentation fault when running this. Haven't been able to succeed in finding my answer anywhere else on the web, so much help would be appreciated.</p>
  
  <p>EDIT: </p>
  
  <p>C CODE: </p>
  
  <pre><code>void program2()
  {
  int numA[2] = {5,10};
  int *num1 = &amp;numA[0];
  int *num2 = &amp;numA[1];
  int loop=0;
  printf("stop3\n");
  for(loop=0;loop&lt;=10;loop++)
  {
      *num1 *=2;
      *num2 *=3;
      printf("%d\n%d\n",*num1,*num2);
      _asSwap(*num1,*num2);
      printf("stop5\n");
      printf("P2num1= %d\n P2num2= %d\n",*num1,*num2);
  
  }
  </code></pre>
  
  <p>Assembly:</p>
  
  <pre><code>_asSwap:
  push    %ebp
  movl    %esp, %ebp
  
  movl 8(%ebp), %edx
  movl 12(%ebp), %ecx
  movl (%edx), %ebx
  movl (%ecx), %eax
  movl %eax, (%edx)
  movl %ebx, (%ecx)
  
  pop %ebp
  ret
  </code></pre>