1

I noticed a strange usage of mov instruction by gcc in which the source and destination operand were the same.

mov     edi, edi

Code: https://godbolt.org/z/kANkNL

Note that clang also generates a mov instruction, but moves data from edi to eax. (which is also not obvious to me. Why use eax register at all? It is a void function).

What's the purpose of this mov instruction?

Jester
  • 56,577
  • 4
  • 81
  • 125
skgbanga
  • 2,477
  • 2
  • 20
  • 29
  • 2
    That's zero extending edi into rdi. Looking for the duplicate... – Jester Jan 13 '20 at 15:57
  • @phuclv Yes, second answer by Jeach explains what Jester pointed in their comment. Thanks. – skgbanga Jan 13 '20 at 16:03
  • 2
    See also [Peter's comment on the second duplicate](https://stackoverflow.com/questions/21439289/zero-out-top-32-bits-of-64-bit-register#comment99719418_21439432) which applies to why clang used `eax`. – Jester Jan 13 '20 at 16:03
  • 2
    on x86 it's a NOP: [Windows32 API: “mov edi,edi” on function entry?](https://stackoverflow.com/q/11337433/995714) but on x86-64 it's a zero extension [Why did GCC generate mov %eax,%eax and what does it mean?](https://stackoverflow.com/q/11910501/995714). Please tag the correct 32/64-bit architecture – phuclv Jan 13 '20 at 16:05

0 Answers0