flat assembler
Message board for the users of flat assembler.

Index > Windows > WindowProc content

Author
Thread Post new topic Reply to topic
CodeX



Joined: 08 Feb 2006
Posts: 20
Location: Estonia
CodeX
Hello!

Can somebody explain why Fasm and Fresh examples save ebx, esi, and edi registers on stack at the start of WindowProc and restore them at the end?
Is it some kind of Windows API requirement?
Post 21 Mar 2006, 10:36
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1607
Location: Ukraine
shoorick
yes, these registers are used internally by system, destroing their values may cause process fault.
Post 21 Mar 2006, 11:13
View user's profile Send private message Visit poster's website Reply with quote
CodeX



Joined: 08 Feb 2006
Posts: 20
Location: Estonia
CodeX
I've tried to compile and run a few programs without these pushes and pops and everything seems to be Ok.
Is there any official document concerning this issue?
Post 21 Mar 2006, 11:24
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1607
Location: Ukraine
shoorick
if you do not change these registers that saving has no sense, except for the reason "to not forget".
ebx is not used in w9x and wxp, but used in w2k (wnt - just do not know) it all depends on current usage of these registers by system: there could be a problem, but you may not see it if it is not fatal, but it may cause fault later. i heard about calling conventions (but did not see them) - if you will call win api function it will (may) destroy ecx, eax and edx (eax and edx/eax are used to return result) and will preserve ebx, edi and esi, so, do it same.
personally, i do not preserve these registers when i do not use them inside winproc, and if i do - i preserve exactly in place where i'm using them.
Post 21 Mar 2006, 12:27
View user's profile Send private message Visit poster's website Reply with quote
CodeX



Joined: 08 Feb 2006
Posts: 20
Location: Estonia
CodeX
I've googled a lot of documents about the subject but none from MS. All I've found in MSDN is that "if you use asm-inlines in your C-program then you should preserve these registers because C-compilers use this convention to keep some internal data in these registers between function calls" - I'm not using any C-compiler, just FAsm Smile . Also have found in MSDN a preliminary specification for developing drivers for 64-bit Windows which says that RBX, RSI, and RDI (as well as a few additional Rn registers) must be preserved. But there's nothing about 32-bit Windows register preserving requirements. Maybe there is, but I couldn't find it.
In my programs I have changed ebx register without preserving its' content and it worked fine, but that's probably because I have XP on my machine.
Anyway, thanks for your answer. Smile
Post 21 Mar 2006, 12:55
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7750
Location: Kraków, Poland
Tomasz Grysztar
As for your own functions which only you call and use, you're free to use registers in any way you want. When you call any official API, you can be sure that it preserves EBX, ESI and EDI - what makes things easier sometimes when you have to do many API calls in a row. And when you create any function that may get called by Windows or some other application (like the WindowProc, DialogProc, any hooks etc.), you also need to preserve those registers.
Post 21 Mar 2006, 13:25
View user's profile Send private message Visit poster's website Reply with quote
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris
This is part of the so-called standard calling convention specification.
Quoted from WinDbg documentation:

Calling Conventions
The x86 architecture has several different calling conventions. Fortunately, they all follow the same register preservation and function return rules:

Functions must preserve all registers, except for eax, ecx, and edx, which can be changed across a function call, and esp, which must be updated according to the calling convention.
The eax register receives function return values if the result is 32 bits or smaller. If the result is 64 bits, then the result is stored in the edx:eax pair.

The following is a list of calling conventions used on the x86 architecture:

Win32 (__stdcall)
Function parameters are passed on the stack, pushed right to left, and the callee cleans the stack.

Native C++ method call (also known as thiscall)
Function parameters are passed on the stack, pushed right to left, the "this" pointer is passed in the ecx register, and the callee cleans the stack.

COM (__stdcall for C++ method calls)
Function parameters are passed on the stack, pushed right to left, then the "this" pointer is pushed on the stack, and then the function is called. The callee cleans the stack.

__fastcall
The first two DWORD-or-smaller arguments are passed in the ecx and edx registers. The remaining parameters are passed on the stack, pushed right to left. The callee cleans the stack.

__cdecl
Function parameters are passed on the stack, pushed right to left, and the caller cleans the stack. The __cdecl calling convention is used for all functions with variable-length parameters.
Post 21 Mar 2006, 13:30
View user's profile Send private message Reply with quote
CodeX



Joined: 08 Feb 2006
Posts: 20
Location: Estonia
CodeX
Thanks for your comments.
I already got the point. I only wonder, why it is so hard (if possible at all) to get this information from MS. Smile
Post 21 Mar 2006, 14:21
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.