hello all,

A simple question, what regs/flags does windows use, i read sumwhere windows uses and expects EDI,ESI,EBX,EBP not to be changed by your program, so ive been saving and restoring them and everything seems ok, is this right? is there any other regs/flags we shouldnt alter. Do window calls ever change or expect the direction flag not to be changed?
Posted on 2001-10-02 20:08:55 by Lennon
Not at all, you can use any Flag without problems at all. I mean, I didn't "Study" that, but I changed the flags at will, tested in both 98/2000 and everything was fine, I think (didn't check) that windows also saves the flags b4 entering in your process, so you can use them without saving them.
As you already said, that are the registers that should come back properly b4 returning....




-MagicMac :)
Posted on 2001-10-02 20:45:07 by magicmac
Lennon,

What you are doing is basically right, if you use them in a proc, preserve and restore EBX, ESI and EDI. Usually EBP and ESP are left alone unless you are doing manual stack manipulation.

Direction flag should be set to clear (CLD).

Nowe one thing to be careful with is if you call another proc that conforms to this convention, it can modify EAX, ECX and EDX so you may need to preserve these with a procedure call from within another procedure.

Regards,

hutch@pbq.com.au
Posted on 2001-10-02 20:48:53 by hutch--
Thanks for the fast replies, im on the game side of programming so i dont actually call the window functions that often (i prolly will when i get into D3D though) and i use the EBP register all over the place as well as all the other regs. Im converting a game from DOS and appart from a few calls to set up the window/screen/read from a file etc i havent had to change anything at all :alright: a trick im using is before every group of window calls(which isnt very often) i use this macro -:

M_RESTORESYSTEM MACRO
mov esi,Store_esi
mov edi,Store_edi
mov ebp,Store_ebp
mov ebx,Store_ebx
ENDM

having allready stored the regs at the start of me program of course....and then after the obvious -:

M_SAVESYSTEM MACRO
mov Store_esi,esi
mov Store_edi,edi
mov Store_ebp,ebp
mov Store_ebx,ebx
cld
ENDM

this makes the problem totally invisable to my DOS program, i just wanted to know if this was correct, and now i know i dont need that cld instruction because windows doesnt change it.

:grin:
Posted on 2001-10-02 21:03:51 by Lennon
It's better to save the registers by doing PUSH, or you'll run into
trouble should you ever need to go multi-threaded.

Also, saving/restoring ESI/EDI/EBX/EBP should only really be important
in callbacks (ie, code windows calls in your program, like a WNDPROC).
It isn't/shouldn't be necessary to restore them before calling an
API. On the other hand, you might want to preserve EAX/ECX/EDX
when calling APIs, because these registers can be destroyed at will.
Posted on 2001-10-02 21:11:56 by f0dder
thanks fodder, i thought about the muli thread thing, but i wont be using threads, and if i do they wont be calling window functions anyway, using push/pop is less versatile, trust me because when i first tried to get round this problem i used push/pop lol ....as for windows trashing regs ....i knew it prolly would so i just pushad/popad if i want regs preserving.
Posted on 2001-10-02 21:20:43 by Lennon
You might not be using multithreading now, but in case you want to
do it later, better write clean code from the beginning. I learned this
the hard way. Trying to find the points where you access global
variables and adding synchronization when it's too late... :mad: .
At least think about it. Using the stack isn't hard :).

pushad/popad instead of preserving just EAX,ECX,EDX is silly. It
takes more stack space and is slower.
Posted on 2001-10-02 21:30:19 by f0dder
lol fodder ive been coding 100% assembler for years on all kinds of systems, i know how to use the stack, my system works better, and they are macros if i ever use multi-threads (which i wont in this project) they can be changed, as for the pushad/popad ive done this twice in the whole source, and the only reason i did that was i wasnt sure what regs windows would change, thats why i put the post on here, to find out what exactly is going on.....
Posted on 2001-10-02 21:40:17 by Lennon
Hang an a second, ive only just read what fodder actually said "Also, saving/restoring ESI/EDI/EBX/EBP should only really be important
in callbacks" :stupid: does this really mean i can mess about with ESI/EDI/EBX/EBP as much as i want, as long as the code isnt callback code, is this true?
Posted on 2001-10-06 21:14:18 by Lennon
It should be true.... BUT(!) ... most stuff under windows will be a callback.
wndproc is a callback, and thus anything called from wndproc will
be a callback :). Of course you can preserve the registers in wndproc
a *NOT* care about it in any subprocs, and this will be OK performance.
Just remember it also when calling "enum" sort of funcs that take
callback proc pointer.
Posted on 2001-10-06 21:50:43 by f0dder
Thanks again fodder, the main code/process/thread whatever you call it (sorry im full of alcohol :grin:) is never called from windows is it?, just things like wndproc, which isnt a problem with this project because its a game, and only sets up one simple window ....with no buttons/menu/ etc infact the wndproc is just
this -:

.IF uMsg == WM_KEYDOWN
.IF wParam == VK_ESCAPE
INVOKE PostQuitMessage,NULL
sub eax,eax
ret
.ENDIF
.ELSEIF uMsg == WM_DESTROY
INVOKE PostQuitMessage,NULL
sub eax,eax
ret
.ENDIF
INVOKE DefWindowProc,hWin,uMsg,wParam,lParam
ret
WndProc ENDP
Posted on 2001-10-06 22:01:52 by Lennon
.IF uMsg == WM_KEYDOWN && wParam == VK_ESCAPE  || uMsg == WM_DESTROY

INVOKE PostQuitMessage,NULL
sub eax,eax
ret
.ENDIF
INVOKE DefWindowProc,hWin,uMsg,wParam,lParam
ret
WndProc ENDP
Posted on 2001-10-07 16:10:22 by buliaNaza
erm, thanks for that.
Posted on 2001-10-07 17:15:54 by Lennon
Lennon,

Be careful with some of the advice you have been given, a callback and a proc are no different, they ALL use the same register preservation convention, if you use any of EBX, ESI or EDI preserve them or somewhere through your development it will come crashing down around your ears. You only use ESP/EBP if you are doing manual stack manipulation.

There is nothing stopping you from using non standard preservation conventions internally with you own procedures but anywhere you interact with the OS, you live dangerously if you do not respect this convention.

Normal things apply with EAX, ECX and EDX, the procedure you call can modify them so if you need to call any OS based procedure, save what you use.

Regards,

hutch@pbq.com.au
Posted on 2001-10-07 18:54:33 by hutch--
Ok point taken Hutch, i'll just stick to what i was doing, everything is working fine anyway. But why only use EBP for stack manipulation, the dos code im converting uses it all over the place, it would be allmost imposible to convert it to not use it, and like i said, the code is actually working perfectly at th moment, no crashes anywhere...everything doing what it should...
Posted on 2001-10-07 19:01:05 by Lennon
no problem with using EBP, just preserve it.
Posted on 2001-10-07 19:19:43 by f0dder