Amit Singh Tomar
Amit Singh Tomar

Reputation: 8610

Is it the right code in assembly

I am new to Assembly and reading about calling convention in x86 .

In one of the example below .

cdecl int MyFunction1(int a, int b)
{
 return a + b;
}


 x = MyFunction1(2, 3);


_MyFunction1:
push ebp
mov ebp, esp
mov eax, [ebp + 8]
mov edx, [ebp + 12]
add eax, edx
pop ebp
ret

push 3
push 2
call _MyFunction1
add esp, 8

I am able to understand most part of the given code but have doubt on one line where pop ebp has been done.

I think right call will be "pop [ebp+4]" because after push ebp , mov ebp ,esp is performed which cause ebp pointer to decremented by 4 and hence to reach to original position have to add 4 bytes to ebp.

Upvotes: 0

Views: 117

Answers (2)

xen-0
xen-0

Reputation: 709

The typical epilogue reads:

mov esp, ebp
pop ebp
ret

The old ebp is stored at the address your current ebp is pointing to. Hence mov esp, ebp puts the stack pointer at this address, so pop ebp will restore ebp correctly (and esp).

It so happens that you can forgo the mov esp, ebp instruction in your function, since you never use the stack and ebp and esp already point to the same address.

pop [ebp+4] would be incorrect, since that would put the value at the top of the stack into [ebp+4]

Upvotes: 3

ady
ady

Reputation: 173

push ebp
la la la la la la la la la la 
pop ebp
ret

So it's fine.

One push

One pop

Upvotes: 0

Related Questions