The meaning of RET 2 in assembly
Yes, but ret 2
also removes 2 bytes of parameters from the stack. Presumably, your function was called like:
push some_parameter
call Function
At this point, a cdecl
function - a "caller cleans up" function (Generally used by C) - would require add sp, 2
to "clean up the stack", removing the parameter. Such a function would end in a plain ret
.
A stdcall
function, which is what you've got, is a "callee cleans up" function (used by Windows APIs, for example) doesn't require the add sp, 2
- it has been done by the ret 2
.
If you're not aware of it, call
puts the return address on the stack (and ret
pops it off), so you can't just pop
to get the parameter inside your function.
Lets say I have a procedure to add two words and leave the sum in EAX
. The words are arguments I want to pass to the procedure on the stack. i.e:
push word1
push word2
call addtwob
The procedure would look something like:
addtwob proc
push ebp
mov ebp,esp
mov eax, [ebp+6]
add eax, [ebp+8]
pop ebp
ret 4
Endp
[ebp+6]
and [ebp+8]
address word2
and word1
on the stack.
ret 4
just returns like usual but then adds 4 to the stack pointer (esp
) so you don't have to pop word2
pop word1
off the stack after returning from the call, therefore it cleans/balances the stack without needing to pop the previous pushes.