Writing naked functions with custom prolog and epilog code in Visual Studio

只愿长相守 提交于 2019-12-04 10:22:48

Since ret requires a constant argument, you need to arrange for your function to have a constant number of parameters, but that situation is only required at the point you're ready to return from the function. So, just before the end of the function, do this:

  1. Pop the return address off the top of the stack and store it in a temporary; ECX is a good place.
  2. Remove the variable number of arguments from the stack, either by popping each one off individually, or by adjusting ESP directly.
  3. Push the return address back onto the stack.
  4. Use ret with a constant argument.

Incidentally, the issue you refer to as (a) really is a problem, in the general case. You've just been lucky that the caller seems to always refer to its own local variables using a frame pointer instead of the stack pointer. Functions aren't required to do that, though, and there's no guarantee that a future version of the host program will continue to work that way. The compiler is also liable to save some register values on the stack only for the duration of the call, and then expect to be able to pop them off again afterward. Your code would break that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!