News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Passing esi edi ebx to CreateThread

Started by jj2007, February 18, 2019, 11:21:36 PM

Previous topic - Next topic

jj2007

Tests on Win7-64 show that the non-volatile regs esi edi ebx appear unchanged on entry to a threadproc. I googled around a bit but could not find any documentation of this behaviour. One could, of course, interpret the Win32 ABI accordingly, but I find it pretty unclear in this respect. What do you think? Has anybody ever stumbled over documentation of this feature?

hutch--

I guess it depends on what you think can be gained, if its only protecting 3 registers when a new thread is a complex operation by the OS that is far longer, the gain is trivial and probably unmeasureable but where you risk a register based crash if you don't preserve them. Normally with CreateThread() you pass a structure to it so its not like it matters.

tenkey

If CreateThread is optimized to use rep movsd to do something like copy structures before calling threadproc, then the HLL compiler doesn't need to restore edi and esi until the end of CreateThread. So it's not safe to assume ebx, esi, and edi at entry to threadproc is the same as at entry to CreateThread.

felipe

as far as i know this threadproc will be a call back function, so the non volatile registers will be required in their total integrity by windows... :idea:

jj2007

tenkey, Felipe - interesting. Do you have any link to M$ documenting this, or are you just guessing aloud?

felipe

here it is a link to microsoft documentation about the callback function for createthread (threadproc): https://msdn.microsoft.com/en-us/library/windows/desktop/ms686736(v=vs.85).aspx  :idea:

but i think you are asking for the famous 32 bit abi documentation from microsoft...you know there is a good work made by agner, but i found this documentation from microsoft again, about the 32 bit calling convention: https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions?view=vs-2017  :idea:

I hope you like it  :bgrin:

felipe

the third paragraph says:
QuoteThe compiler generates prolog and epilog code to save and restore the ESI, EDI, EBX, and EBP registers, if they are used in the function.
... :idea:

jj2007

Thanks, tenkey and Felipe. Although the two linked sources do not explicitly deal with the non-volatile regs in this specific context (i.e. read-only use), it is definitely not a good idea to rely on them.

aw27

Each thread used to have its own separate and independent set of CPU core registers. Are things different now or am I not catching up what you are talking about?