Solved

thiscall

Posted on 2004-04-26
4
351 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.net Code Gerators 3 478
c code optimization 6 452
Check what programs are automatically updating 3 336
Get register length/size? 5 592
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

10 Experts available now in Live!

Get 1:1 Help Now