2

My code is as follows : (In Windows 64 bit environment, debugging in Visual Express 2012 on Windows 7, Core i5 520M CPU)

mov edx, a_number_which_is_less than_16_bits
shl rdx,32       ; moves that number to upper 32 bits of RDX
<few lines that leave an useless number in EDX, but does not touch upper 32 bits of RDX>
xor edx,edx      ; should clear the lower 32 bits of RDX keeping upper 32 bits intact

But it is clearing the upper 32 bits of RDX as well...leaving entire RDX as zero The Intel Manual for 64 bit processors does not specify that XOR instruction clears upper 32 bit as well (Vol 2 B, Page 4-531).

Has this been seen by other programmers as well ?

Magoo
  • 77,302
  • 8
  • 62
  • 84
quasar66
  • 555
  • 4
  • 14
  • Other answers have explained that the behavior is intentional. As for *why* you'll see instructions use `xor` on `esi`, `edi`, `esx`, `edx`, ... instead of `rsi`, `rdi`, `rsx`, `edx`, ..., I believe it's because 64-bit variants require more space. So when compilers want to clear the whole register, they will piggy-back on zero-extended results. So `xor edx,edx` is `0x31, 0xd2`; whereas `xor rdx, rdx` is `0x48, 0x31, 0xd2`. See https://stackoverflow.com/a/33668295/4386952 for more detail. – Daniel Rosenwasser Jun 28 '22 at 11:17

2 Answers2

3

Zero-extended and sign-extended results were design decisions by AMD, and Intel followed. So this is expected behaviour. The technical merits are discussed elsewhere.

Community
  • 1
  • 1
Brett Hale
  • 21,653
  • 2
  • 61
  • 90
2

The fact that the upper 32-bits of a 64-bit destination register are cleared when dealing with a 32-bit operand is described in Intel's "Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 1: Basic Architecture" - 3.4.1.1 General-Purpose Registers in 64-Bit Mode:

  • 32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register.
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Thanks ... I knew it does this to mov instructions, but to do this to or/xor was perhaps a little too much ... – quasar66 May 11 '13 at 13:38