Solved

thiscall

Posted on 2004-04-26
4
353 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IRQL_NOT_LESS_OR_EQUAL for Kernel Timer Thread 7 578
Show semaphore name in Pascal 12 642
Intel DX58SO board 7 875
Convert dialog units to pixels 5 107
Binary Differential Replication, What it is, how it works and how it differs from standard delta file replication
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

11 Experts available now in Live!

Get 1:1 Help Now