The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => The Orphanage => Topic started by: Magnum on September 16, 2010, 10:41:17 PM

Title: CDQ same size as xor edx,edx
Post by: Magnum on September 16, 2010, 10:41:17 PM
Masm produces same size code for

xor edx,edx

and

cdq

I wonder why?
Title: Re: CDQ same size as xor edx,edx
Post by: clive on September 17, 2010, 12:00:45 AM
Quote from: Magnum on September 16, 2010, 10:41:17 PM
Masm produces same size code for

xor edx,edx

and

cdq

I wonder why?

Doesn't CDQ sign extend EAX to EDX:EAX, where EDX the equals 0x00000000 or 0xFFFFFFFF depending on bit 31 of EAX. It's much more likely that it simply loads one of those two constants than do any math. It's also conceivable the it just fans out bit 31 to all bits, or barrel shifts right maintaining the sign.
Title: Re: CDQ same size as xor edx,edx
Post by: Magnum on September 17, 2010, 12:51:15 AM
I am guessing that it's a compiler optimization.

cwd also zeros out edx.

16 bit compilers replace cdq with cwd unless a com file is made.

Title: Re: CDQ same size as xor edx,edx
Post by: redskull on September 17, 2010, 01:08:17 AM
My version of MASM (9.00.21022.08) doesn't.  XOR assembles to 33D2, and CDQ assembles to 99.

-r
Title: Re: CDQ same size as xor edx,edx
Post by: clive on September 17, 2010, 01:28:40 AM
Quote from: Magnum on September 17, 2010, 12:51:15 AM
I am guessing that it's a compiler optimization.

cwd also zeros out edx.

16 bit compilers replace cdq with cwd unless a com file is made.

CWD/CQD are commonly used in and optimization of ABSolute. Neither function always sets EDX/DX to zero, it depends on the sign of EAX/AX, so for half of all values in EAX/AX the content of EDX/DX with be FFFFFFFFh or FFFFh. Now if EAX only holds a 16-bit value in the low order bits (ie high order 16-bits are zero), then yes EDX will always be zero. Is this what you are talking about? I think we need some more info on the context, with some examples.

In 16-bit CDQ is 66,99, in 32-bit it is 99. Are we talking 16-bit or 32-bit?

Which compilers alter the code for COM files, I'm not familiar with a compiler that does that, care to cite something specific and provide an example. I can see this perhaps if the compiler assumes you are using an 8088,8086,80188,80186,80286, but I see no reason why a COM file couldn't contain 80386 instructions if run on a suitable processor.
Title: Re: CDQ same size as xor edx,edx
Post by: Magnum on September 17, 2010, 01:42:09 AM
For 16-bit, I use Tasm.

My testing showed the compiler only changing cdq to cwd for .exes.

But I also wasn't putting any values in edx either.


Title: Re: CDQ same size as xor edx,edx
Post by: clive on September 17, 2010, 01:49:40 AM
Quote from: Magnum
My testing showed the compiler only changing cdq to cwd for .exes.

Borland Pascal, C compiler Vx.xx? TASM is an assembler.

If EAX is 0..65535, then yes EDX will be zero, but CDQ is supposed to operate on full scale 32-bit values

EAX = 7FFF FFFF -> EDX:EAX 0000 0000 7FFF FFFF
EAX = 8000 0000 -> EDX:EAX FFFF FFFF 8000 0000
Title: Re: CDQ same size as xor edx,edx
Post by: FORTRANS on September 17, 2010, 11:43:56 AM
Quote from: clive on September 17, 2010, 01:28:40 AM
but I see no reason why a COM file couldn't contain 80386 instructions if run on a suitable processor.

Hi,

   Absolutely right.  Of course it then requires a 386+ to run.  <g>
I have used *.COM files with 32-bit instructions on my Pentium at
one time.

Cheers,

Steve N.