?
Solved

thiscall

Posted on 2004-04-26
4
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Assisted Solution

by:dimitry
dimitry earned 100 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 200 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 200 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

New style of hardware planning for Microsoft Exchange server.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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