The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: n00b! on December 13, 2008, 06:23:02 PM

Title: Preservation of ebx, edi, esi
Post by: n00b! on December 13, 2008, 06:23:02 PM
QuoteThe three registers that must remain the same for each interaction with the operating system are,

EBX ESI EDI

Does this mean, that you have to preserve these if you're in a callback like a Window-Proc?
Otherways you can modify it freely like

.code
main:
  mov edi, 2
  xor ebx, ebx
  mov esi, eax
  invoke ExitProcess, 0
end main


If so, is there any other situation where you have to preserve them?
Title: Re: Preservation of ebx, edi, esi
Post by: hutch-- on December 14, 2008, 03:24:37 AM
noob,

many have advised this mistake, ANY INTERACTION with the operating system requires these registers to be preserved and this means not only a callback but ANY API CALL and the vast majority of library and external function calls written correctly.

If you protect these registers first you can create a subsystem that does not have to conform which is the case with some specialised code but when it is finished you must restore the system registers again.

This technique is used by the operating system to distribute the register preservation load between the OS and the user code so that both do the minimum register preservations for stable execution for each thread. Doing more than is needed slows that app down and increases the processor load, doing less than is needed delivers unreliable operation which varies from one OS version to the next and crashes that are very hard to debug.

Do it the right way the first time and you NEVER HAVE A PROBLEM with registers.
Title: Re: Preservation of ebx, edi, esi
Post by: GM on December 15, 2008, 10:09:26 AM
Hutch, StrToFloat doesn't preserve ebx edi esi.
Title: Re: Preservation of ebx, edi, esi
Post by: hutch-- on December 15, 2008, 10:31:58 AM
GM,

I didn't write it.