Being a beginner i am interested in knowing whether the ecx register is preserved if i jump out of a routine and call a win32 function.

Maybe someone can give me the ins and outs of this.

Also if i push a value on the stack before jumpinging or calling another procedure will my push be the next item to pop when i return?

I wish ther were some documentaion for this as far as masm is concerned. If there all ready is just point me in the direction.

thanx :confused:
Posted on 2001-08-21 23:32:28 by titan
You should always save ecx in a loop:

mov ecx,count
looper:
push ecx

invoke something,parm1,parm2

pop ecx
loop looper


I'm not sure about the requirements of register saving by win32 functions, but it is usually a good idea to preserve registers in a public function. You would hope that most of the win32 functions do this. However, in the interest of safety, I always push/pop ecx in a loop. Its one of my standard rules. Also, the way I code, and the way most people code, I dont have any loops using ecx where a function is invoked anyway. However there are many many cases where I call my own procedures in my loops. Not all of my procedures preserve ecx. But its my habbit to push/pop ecx in all loops anyway.

The stack is always preserved by functions. So yes, when you push a value then call a function it will absolutely be the next value to pop. You should also make sure you preserve the stack always.
Posted on 2001-08-22 00:12:34 by mcarp
I don't remember where I originally found this,
but it has been verified in a number of
situations.

The program calling a Win32 fuction should save:

EAX
ECX
EDX

as they may be modified in the called function.

The function which is being called should save:

EBX
ESI
EDI
EBP

and restore before returning control to caller.

Farrier
Posted on 2001-08-22 04:53:50 by farrier
In fact, you don't really need to preserve ecx in a loop, you have to preserve
esi/edi/ebx in you WndProc
The easier way is :

WndProc PROC hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
pushad

; process msg here

popad

popad
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
WndProc ENDP

(s)
Posted on 2001-08-22 04:58:05 by (scalp)
farrier is correct, the windows convention is to preserve EBX, ESI & EDI, EBP and ESP are normally only used for stack manipulation in procedures.

You can expect any properly written function/procedure to preserve these but they can freely modify EAX, ECX & EDX so if you code uses any of these before a procedure call, you may have to preserve then otherwise the procedure that has been called may overwrite them and you own code will not work properly.

Regards,

hutch@pbq.com.au
Posted on 2001-08-22 09:26:10 by hutch--
Here's an extension of titan's question:

Are the registers shared by all programs, or does each module loaded into windows contain it's own "registers" in which it can access and modify?
Posted on 2001-08-22 12:08:05 by vcv
Each thread contains its own register set. Otherwise, it would not be possible to preempt a thread and later resume it.
Posted on 2001-08-22 13:44:04 by tank
ah, thanks :)
Posted on 2001-08-22 14:52:01 by vcv