News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

CDQ same size as xor edx,edx

Started by Magnum, September 16, 2010, 10:41:17 PM

Previous topic - Next topic

Magnum

Masm produces same size code for

xor edx,edx

and

cdq

I wonder why?
Have a great day,
                         Andy

clive

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.
It could be a random act of randomness. Those happen a lot as well.

Magnum

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.

Have a great day,
                         Andy

redskull

My version of MASM (9.00.21022.08) doesn't.  XOR assembles to 33D2, and CDQ assembles to 99.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

clive

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.
It could be a random act of randomness. Those happen a lot as well.

Magnum

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.


Have a great day,
                         Andy

clive

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
It could be a random act of randomness. Those happen a lot as well.

FORTRANS

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.