Solved

thiscall

Posted on 2004-04-26
4
352 Views
Last Modified: 2012-06-21
Just a quick question, if you code a member function in assembler, the this pointer is passed as ecx.

Can you use ecx then without the need to save it to the stack and restore it when your finished?

Or will the compiler assume that since it already put the this pointer into ecx, it can skip that on the next member function call.

Is there any way to tell? I looked at the disassembly but didn't see anything conclusive one way or the other.

thanks,
-Sandra
0
Comment
Question by:Sandra-24
4 Comments
 
LVL 11

Assisted Solution

by:dimitry
dimitry earned 25 total points
ID: 10922812
Note: I am talking about VC++ 6.0. Another compiler may behave different.
ECX has a 'this' pointer. And compuler push into the stack but only to use later as 'this'.
It is not restored before return.
So Compiler doesn't suppose that ECX will contain 'this' value after return from your member function, but
it is good "style" to push and pop all registers you are going to change temporarely in your function.
0402794     lea         ecx,[ebp-24h]                                      ; << ecx has 'this' pointer
00402797   call        @ILT+25(Move::PrintData) (0040101e)

0
 
LVL 22

Accepted Solution

by:
grg99 earned 50 total points
ID: 10927509
It depends on the compiler, and the optimization level.   Usually at a low optimization level, the compiler doesnt keep much info in registers, so it *may* be possible to mangle ecx.

At higher optimization levels, the compiler may switch to keeping frequently used values in registers, so ecx may not be mangleable.

The only way to make sure is to read the compiler's "calling conventions" chapter and see what it expects.

0
 
LVL 3

Author Comment

by:Sandra-24
ID: 10932333
Well I read through the VC docs again on calling conventions and didn't find any help one way or another. I also followed a few threads on thiscall and assembly but didn't learn anything of value. I checked google again but also found nothing. I suppose the best is just be safe and save the value of ecx on the stack and restore it after.

thanks,
-Sandra
0
 
LVL 3

Assisted Solution

by:Dancie
Dancie earned 50 total points
ID: 10937744
The only registers that are saved across functions with VC are:
ebp,esi,edi,and ebx. I have never seen a compiler for windows or
unix that does it differently. This seems to be a global standard for all
compilers. The use of ecx may have been a choise of the compiler to
make an optimization, or just a random choise.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
When rebooting a vCenters 6.0 and try to connect using vSphere Client we get this issue "Invalid URL: The hostname could not parsed." When we get this error we need to do some changes in the vCenter advanced settings to fix the issue.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now