The MASM Forum

General => The Laboratory => Topic started by: jj2007 on February 18, 2019, 11:21:36 PM

Title: Passing esi edi ebx to CreateThread
Post by: jj2007 on February 18, 2019, 11:21:36 PM
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?
Title: Re: Passing esi edi ebx to CreateThread
Post by: hutch-- on February 19, 2019, 01:16:56 AM
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.
Title: Re: Passing esi edi ebx to CreateThread
Post by: tenkey on February 20, 2019, 09:51:20 AM
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.
Title: Re: Passing esi edi ebx to CreateThread
Post by: felipe on February 20, 2019, 11:26:28 AM
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:
Title: Re: Passing esi edi ebx to CreateThread
Post by: jj2007 on February 20, 2019, 01:08:48 PM
tenkey, Felipe - interesting. Do you have any link to M$ documenting this, or are you just guessing aloud?
Title: Re: Passing esi edi ebx to CreateThread
Post by: felipe on February 20, 2019, 02:06:13 PM
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 (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 (https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions?view=vs-2017)  :idea:

I hope you like it  :bgrin:
Title: Re: Passing esi edi ebx to CreateThread
Post by: felipe on February 20, 2019, 02:07:32 PM
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:
Title: Re: Passing esi edi ebx to CreateThread
Post by: jj2007 on February 20, 2019, 06:45:46 PM
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.
Title: Re: Passing esi edi ebx to CreateThread
Post by: aw27 on February 20, 2019, 09:00:40 PM
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?