The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jj2007 on May 28, 2008, 11:17:42 AM

Title: trash ecx or edx?
Post by: jj2007 on May 28, 2008, 11:17:42 AM
Quick poll: If you write a macro that uses two registers, then one is EAX, sure, but which one would you choose as No. 2 if you cannot afford a push/pop? ECX or EDX?
Title: Re: trash ecx or edx?
Post by: hutch-- on May 28, 2008, 12:06:41 PM
jj,

by spec, you can trash any of eax ecx edx are not required to be preserved. If it in the middle of a critical algo, I would see if you have another register which may be either of these or any of the others that is repeatedly overwritten with new data AFTER you have run the macro you may be able to use that instead. If you lay out the algo carefully you can often get a couple of extra registers this way.
Title: Re: trash ecx or edx?
Post by: jj2007 on May 28, 2008, 01:34:08 PM
Hutch, what you say is perfectly correct but that was not my question. Imagine you have a big fat project, and you need that macro pretty often. And you don't want to insert all the time a push/pop. In my fattest source, I use 142 times edx, and 444 times ecx, so edx would be a better choice because it's more rarely used.
Remember the abs macro thread (http://www.masm32.com/board/index.php?topic=9241.msg67112#msg67112). Surely there were macros that were just a little bit faster, but at the expense of having to sacrifice an extra register. No good for a general purpose macro, especially since many people have so much trust in Sir Hutch's macros that they would blindly write mov MyVar, abs(edx)...
Title: Re: trash ecx or edx?
Post by: Tedd on May 28, 2008, 01:59:34 PM
I'd consider edx as a secondary data register -- in the same way that mul and div do
Title: Re: trash ecx or edx?
Post by: hutch-- on May 28, 2008, 02:24:40 PM
jj,

You could try a local or even a data? section variable so you can use MOV rather than PUSH/POP as it is almost exclusively faster.


.data?
  _edx dd ?

.code
  ......
  mov _edx, edx
    ; use EDX for something
  mov edx, _edx