News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Preserving edi

Started by msmith, February 08, 2006, 02:34:34 AM

Previous topic - Next topic

msmith

In searching this forum and others as well as MSDN on the subject, this is the most definative statement I could find :

"you can use ebx esi edi, they are guaranteed not to be changed"

Is this true? If so, where is the definative statement of this fact to be found in the Microsoft docs?

I almost always use edi in my compiler to hold the handle arg of a function. If I call several functions in a row, do I need to reload edi before each call?

zooba

The convention for procedures is that EAX, ECX and EDX are caller-save while EBX, ESI and EDI are callee-save.

Quote from: MSDNIn addition, by using EBX, ESI or EDI in inline assembly code, you force the compiler to save and restore those registers in the function prologue and epilogue.

That's the best I could find in MSDN '03, but considering they generally ignore everything lower-level than C it's not surprising.

Unless it's specified by the function you are calling, EDI is not required to have any particular value in it, and if the function has been properly written (easy to determine using a debugger, just look for push edi near the start, or make sure it doesn't use it) the value won't change.

AFAIK, all Windows API functions do this correctly, as do all the MASM32 library functions.

msmith

Quote
AFAIK, all Windows API functions do this correctly, as do all the MASM32 library functions.

What about msvcrt functions. I use a couple of them but not much.

Ratch

msmith,
     As far as I know, MS does not document which reggies to save and restore.  It leaves it to the compiliers, specifically the C compilier to do the right thing.  Nevertheless, those of us who write Windows programs in assembler know about the "sacred four" registers, to wit: EBX,EBP,ESI,EDI, and also the direction flag (DF).  If you process a message, and don't return to Windows with the same values in those entities they arrived with, you are sooner or later in for a rough tough ride with your program.  Guaranteed.  Ratch

msmith

Quote
If you process a message, and don't return to Windows with the same values in those entities they arrived with, you are sooner or later in for a rough tough ride with your program.  Guaranteed.

I always conform to this. I'm worried about what API functions do to the contents of edi.

hutch--

I wonder why this stuff is in contention so many years down the track. The convention has been around since winNT 3.5

ESP and EBP must be preserved between calls if you use a stack frame or not. If you use EBX ESI or EDI, you must preserve them within a procedure but you can modify EAX ECX and EDX.

The reciprocal is that any function you call can also modify EAX ECX and EDX so if you call another function and you have values in those registers, you may have to preserve them as well.

Write your code any other way and it will not be reliable across different Windows version.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

tenkey

As for documentation, the earlier compilers such as VC 4, documented this under the subject of "calling conventions". I'd have to dig around to see if VC 6 also documented this. I did go to the online version of MSDN and noticed the register information does not appear be present any more.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8