www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Register Preservation in Inline ASM Blocks

reply dsimcha <dsimcha yahoo.com> writes:
What is the rule on who is responsible for saving registers when inserting an
inline ASM block into the middle of a function?  For example:

void fun() {
    auto ptr = somePointer();
    asm {
        mov EAX, ptr;
        lock;
        inc [EAX];
    }
}

Is this acceptable (will the compiler do what it needs to do to deal with my
usage of EAX), or do I need to do something more like:

asm {
    push EAX;
    lock;
    inc [EAX];
    pop EAX;
}

, leaving EAX exactly as I found it?
Nov 19 2010
parent Walter Bright <newshound2 digitalmars.com> writes:
dsimcha wrote:
 What is the rule on who is responsible for saving registers when inserting an
 inline ASM block into the middle of a function?  For example:
 
 void fun() {
     auto ptr = somePointer();
     asm {
         mov EAX, ptr;
         lock;
         inc [EAX];
     }
 }
 
 Is this acceptable (will the compiler do what it needs to do to deal with my
 usage of EAX), or do I need to do something more like:
 
 asm {
     push EAX;
     lock;
     inc [EAX];
     pop EAX;
 }
 
 , leaving EAX exactly as I found it?

Unless you use 'naked' asm, the compiler will figure it out for you.
Nov 19 2010