Does the Win32 entry point have to preserve any re

2020-03-24 08:48发布

问题:

I am writing a program in NASM, and I do not want to link it with the CRT, and so I will specify the entry point (which will be the Win32 entry point). This is the program source code:

global _myEntryPoint

section .text
_myEntryPoint:
    mov eax, 12345

Now this is what I know about the Win32 entry point (please correct me if I am wrong):

  • The Win32 entry point does not return a value like a normal function does (to exit the Win32 entry point I have to call ExitProcess()).
  • The Win32 entry point does not take any arguments.

Now what I don't know is the following:

  • Does the Win32 entry point have to preserve any registers values (callee-saved registers)? I think the answer is No, since when the Win32 entry point exits, it terminates the process and not return execution to a function that expects some registers values to be preserved.

回答1:

As described in my answer to the proposed duplicate, you shouldn't return from the Win32 entry point at all, in which case there is obviously no need for you to preserve any registers. The way your question is phrased vaguely suggests that you were worried that you night need to restore registers before calling ExitProcess but this is definitely not the case; calling ExitProcess does not cause you to return from the entry point, it just stops running your code. (See also here for an update, and this may also be of interest.)

Should you ignore that advice and return from the entry point anyway, well, in practice the answer is the same: you don't actually need to have preserved any registers. To the best of my knowledge this behaviour is not documented, however, so if you wanted to be cautious you might choose to strictly follow the stdcall convention.