I'm currently learning x86 Assembly language (I'm at the start of the course) and I'm having some problems understanding how the stack works in one particular case.
Let's say I have this code:
double(entier n) { return n + n; }
I've tried to convert it into x86 code and I ended up with this :
push ebp #save old pointer stack
mov ebp, esp #put new pointer stack
mov ebx, dword[ebp + 8] #get argument n and put it in ebx
add ebx, dword[ebp + 8] #add n to ebx
But then I was totally blocked and couldn't find how to return the value of ebx
. I found a solution on internet that was the following:
mov [ebp + 12], ebx
pop ebp
ret
pop ebp
ret
I don't understand how it works. Isn't ebp+12
the value of the 2nd argument? (In my case there's none). The pop is used to move the esp
pointer but why do we need 2 pop and 2 return in that case ? Is it only to remove the value that have been used during the function declaration?