title: Accessing function args from the stack relative to EBP while pushing/popping other registers?
body: <p>I am writing an assembly program and a C program; the C program will call a function written in assembly. The environment is Ubuntu 18.04LTS x64.</p>
  <p>It is designed for 32 bits x86 and will be compiled by NASM, but it can't pass correct parameters.</p>
  <p>To simplify the problem, I just changed my function to get the sum of <em>a</em> and <em>b</em>.</p>
  <pre><code>extern int FindPattern(int a,int b);
  
  int result;
  result=FindPattern(1,1);
  printf(&quot;%d\n&quot;,result);
  </code></pre>
  <pre><code>global FindPattern
   section .text
  FindPattern:
      push    ebp
      push    esi
      push    edi
      push    ebx
      push    edx
      push    ecx
      
      mov     ebp,esp
      mov     esi,[ebp+8]     ; a
      mov     edi,[ebp+12]    ; b
      mov     eax,0
      add     eax,esi
      add     eax,edi         ; return a+b
      
      pop     ecx
      pop     edx
      pop     ebx
      pop     edi               
      pop     esi
      pop     ebp
      ret          
  </code></pre>
  <p>The function just adds <em>a</em> and <em>b</em>, and returns the sum. The sum should be 2, however I got a random number like 1449041840. It seems the assembly didn't get correct parameters.</p>
  <p>What's wrong with the code and how can I fix it?</p>
  <pre><code># Makefile
  cc=gcc
  ASMBIN=nasm
  
  all : asm cc link
  asm:
      $(ASMBIN) -o findpattern.o -f elf32 -l findpattern.lst findpattern.asm
  cc :
      $(cc) -m32 -c -g -O0 -fpack-struct graph_io.c
  link :
      $(cc) -m32 -o graph_io findpattern.o graph_io.o 
  clean:
      rm *.o
      rm graph_io
      rm findpattern.lst
  </code></pre>