Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The meaning of RET 2 in assembly

Tags:

x86

assembly

I am very new to assembly, and I don't understand what it exactly means when, at the end of a proc, you write a number with the ret statement.

Like this:

Function Proc push ax cx . ...body... . pop cx ax ret 2  Function endp 

I understand it has something to do with where the stack pointer should return to at the end of the function?

What does it do?

like image 285
Adam Avatar asked Jul 13 '13 09:07

Adam


1 Answers

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.

like image 139
Frank Kotler Avatar answered Oct 10 '22 01:10

Frank Kotler