Author Topic: Passing esi edi ebx to CreateThread  (Read 1491 times)

jj2007

  • Member
  • *****
  • Posts: 10858
  • Assembler is fun ;-)
    • MasmBasic
Passing esi edi ebx to CreateThread
« 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?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7801
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Passing esi edi ebx to CreateThread
« Reply #1 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

tenkey

  • Regular Member
  • *
  • Posts: 19
Re: Passing esi edi ebx to CreateThread
« Reply #2 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.

felipe

  • Member
  • *****
  • Posts: 1360
  • At least we are not alone...
Re: Passing esi edi ebx to CreateThread
« Reply #3 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:
No remarkable words at all...

jj2007

  • Member
  • *****
  • Posts: 10858
  • Assembler is fun ;-)
    • MasmBasic
Re: Passing esi edi ebx to CreateThread
« Reply #4 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?

felipe

  • Member
  • *****
  • Posts: 1360
  • At least we are not alone...
Re: Passing esi edi ebx to CreateThread
« Reply #5 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  :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:
No remarkable words at all...

felipe

  • Member
  • *****
  • Posts: 1360
  • At least we are not alone...
Re: Passing esi edi ebx to CreateThread
« Reply #6 on: February 20, 2019, 02:07:32 PM »
the third paragraph says:
Quote
The 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:
No remarkable words at all...

jj2007

  • Member
  • *****
  • Posts: 10858
  • Assembler is fun ;-)
    • MasmBasic
Re: Passing esi edi ebx to CreateThread
« Reply #7 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.

AW

  • Member
  • *****
  • Posts: 2583
  • Let's Make ASM Great Again!
Re: Passing esi edi ebx to CreateThread
« Reply #8 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?