I just wanted to ask which registers I am allowed to use in Win32. eax,edx,ecx,ebx? Which registers can I use always, which registers do I have to save and restore and which can I never use? Thank you
Posted on 2001-04-23 14:10:00 by w116
well, you can use every register only that : ebx, esi and edi must be preserved in your window or dialog procedure you better know ebp and esp well to use them
Posted on 2001-04-23 15:05:00 by roy
You can use them all, just don't mess with the segment regs. You do need to save/restore EBX ESI and EDI if you modify them. You can have MASM do this for you by adding the USES parameter to any PROC that alters these regs. For example BLAH PROC ... USES EBX ESI EDI ... And be very very careful if you play with ESP or EBP. :)
Posted on 2001-04-23 15:05:00 by S/390
.....I understand why you shouldn't mess with ESI, EDI, EBP, and ESP.......but why EBX??? Also, I once used the following code:

.code
  ...
  xor ECX, ECX          ;Clear the counting reg
  .REPEAT
    inc ECX             ;Chalk one up every run-through
    (whatever proc)
   .UNTIL ECX==Xt       ;Stop after X number of runs
.......but it didn't stop. For some reason ECX kept lingering between 10 and 11. Was that because of some other process using ECX too? Should I have used the stack just to be safe? Oh well, *unknown* This message was edited by *unknown*, on 4/23/2001 4:33:38 PM
Posted on 2001-04-23 16:30:00 by *unknown*
eax, ecx, edx can be used freely, ebx, esi, edi have to be preserved because windows assumes this. ebp can be used with care if you don't use local variables, but restore ebp at the end of your procedure. I don't know about esp. You can use it as a general register but it will mess up the stack if you don't save it. I think you can use it if you really want to, but it's not recommended. *unknown*: If you understand why you shouldn't mess with esi and edi, you can also understand why the same applies to ebx. Ebx is not different from esi and edi. You can use them freely, it's just that windows assumes these registers are preserved. And about your loop: if the code in your loop contains API functions, ecx will probably messed by them. You'd better use ebx, ebx isn't affected by API calls. Thomas
Posted on 2001-04-23 17:39:00 by Thomas
.......I read an excerpt (is that how you spell "excerpt"???) from Intel Opcodes and Mnemonics and I get it now. :D Thanx, *unknown* This message was edited by *unknown*, on 4/24/2001 12:14:53 AM
Posted on 2001-04-23 23:58:00 by *unknown*
Firstly, if other processes modifyed the general registers while your processes used them windows would crash and burn withen milliseconds of starting. (Actualy they do but all the regs are elaboratly saved and restored) EAX = Use it whenever and wherever, but be careful as Masm can modify it when using invoke EBX = You can use it in you main code in your app, but.. in a callback procedure (such as a WndProc) windows may expect it to be not modifyed. ECX = Can be used freely EDX = Can be used freely ESI = Same as EBX EDI = Same as EBX EBP = Same as EBX with the exception that masm uses as the reference point for LOCAL varibles, so EBP does actualy get modifyed whenever you call a proc with LOCAL data ESP = You can modify, but the wrong use can easily corrupt the stack, ie, ESP is modifyed with every push/pop instruction. Segment registers = You can modify, im not aure if windows uses GS, but definaly FS cannot be modifyed because it is likly to crash windows or cause a major fault, as windows gets the address of exception handlers from this segment. EFLAGS = What you can change, you can change, it has little of no effect. DRx = Can modify (At least under Win9x) except for DR6 & DR7 CRx = If you could modify these, i dont think you can exev read from them, i cant remember, windows would be evern less secure than it already is. I think you can modify the floating point control registers, im not sure This message was edited by George, on 4/24/2001 8:54:02 AM
Posted on 2001-04-24 08:52:00 by George
Can I use EBX (pushing before/poping after) inside a loop that make a lot of API´s call? EBX will be afected for that calls? Example:

       mov ebx,100
       DoApiCall:
         invoke ApiCall,...,...,...,...
            cmp ebx,0
       jne DoApiCall
Can I rely that EBX wont be chanded in that loop? :confused: Thanx.
Posted on 2001-04-25 06:38:00 by wolfao
this SHOULD work, but I would not rely on Microsoft :D the thing with the registers can also be found in some C include files, where the function prototypes are declared: WINAPI ... is the same as __stdcall ... is the same as #pragma aux __stdcall modify which means, that the mentioned registers can be modified. not all windows functions follow the WINAPI declaration! for instance, the MCIWndRegisterClass or WSARecvEx function. This message was edited by beaster, on 4/25/2001 12:27:20 PM
Posted on 2001-04-25 12:26:00 by beaster
Windows follows the same rules that you must follow. Therefor, ebx, edi, esi, ebp, and esp are preserved across API calls. Else windows would come crashing down.
Posted on 2001-04-25 15:48:00 by bitRAKE