Click to See Complete Forum and Search --> : Visual C++ and asm


etollerson
November 26th, 2004, 12:30 PM
I've written a couple of procedures in assembly and assembled them outside of VC6. I've linked them in with a simple console project. In the console app, I call the procedure in __asm block. Everything is OK. Works like a charm. But what I'm worried about is if I just stick the block anywhere will it affect registers that VC compiler is assuming unchanged. MSDN just has information on writing functions and prolog and epilog code. It claims that you can use EAX,EBX,ECX,EDX,EDI,and ESI, But then it says if you use EBX,ESI,or EDI then you force the compiler to prodece prolog/epilog code to preserve them. So if I just stick an __asm block anywhere can I assume I can use EAX,ECX,EDX at will and only preserve the others that I use.

__uint64 board = 0x10;
int firstBitSet;

__asm{

lea edx,board
call find_bit
mov firstBitSet,EAX

}

The above block along with find_bit modifies EAX,ECX,EDX without preserving them.Do I need to preserve them? Is there any documentation on this type of stuff? I preserve and use other registers and then restore them.
I'm writing asm procs to speed up getting the answer and I don't want to preserve something that VC doesn't mind me changing.
Also will board always be in data segment or stack segment in VC.
I think the DS and SS is the same.But I don't know if it is always true.
I used flat memory model in assembling

.MODEL C,FLAT

rxbagain
November 28th, 2004, 06:20 AM
VC compiler, as I observed, usually reinitialize EAX,EBX,ECX,EDX,EDI,and ESI for each operation and it DOES NOT rely on the present value of the registers. Everytime it performs operation/call another function, it prepares those registers without relying on their previous values.

The only concern with the ECX is, if it is used in class function with the thiscall convention w/c uses ECX as a pointer to this. YOU SHOULD PRESERVE ECX in this case. It is a good practice then to preserve ECX in your asm code since functions with thiscall might use it.

As with the EDI and ESI, they are preserved in the prolog/epilog of the function (this is true with even in functions that does nothing).

DS and SS registers generally the same with win32 EXEs, but it is not always in that case, they may be different in some cases.

About you variable board, it depends on where you declared it. If it is global, it is generally in DS segment. If it is used as local (variables declared inside functions), it is in SS segment. Just an input, with the global variables, u can use mov edx, offset board to reference it in contrast to a local variable that can only use LEA since the pointer may varry from each call.

Hope it would help you.