0

Given the code:

uint64_t f() { return 42; }

compilers output the following assembly for x86-64:

movl $42, %eax
ret

I'm raising my eyebrows, because the return value should be 64-bit, but the compilers appear to think it is safe to assume that the return registers %rax and %rdi are zero. I'm seem to be unable to find anything about this in the x86-64 ABI. Can somebody point to the basis for this assumption?

jotik
  • 17,044
  • 13
  • 58
  • 123
  • Hmmm, not sure that discounts it as a duplicate (that the answer must be provided in an answer rather than the question). The answers to the question do talk about the same subject. There are probably other dupe questions out there given the fairly surprising implicit nature of this feature, as you say yourself. – trojanfoe May 26 '16 at 10:56
  • 1
    @trojanfoe I tend to agree that this is a duplicate of the other. It is worded differently but it really comes down to the automatic zeroing of the upper 32-bits of a 64-bit register when writing to a 32-bit register. I'm voting to close as a duplicate. I mean no ill will towards the OP by doing so. – Michael Petch May 26 '16 at 16:48
  • @jotik You provided a link to an authoritative reference in a comment to my answer; it was to an Intel user manual, I believe. However you have since deleted it. Lots of toys are being thrown out of prams since this answer was closed as a dupe. – trojanfoe May 27 '16 at 07:51

1 Answers1

1

Setting EAX, etc., clears the upper 32-bits anyway.

finding reference...

Actually it should be closed as a dupe of this question.

Community
  • 1
  • 1
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 1
    Yeah, noticed that. :) I don't think it's a duplicate, the *question* there contains the answer wanted here. Your answer is pretty much right. –  May 26 '16 at 10:49
  • @hvd: It's appropriate to close-as-duplicate anyway, because the ultimate goal is for people wondering about this to be directed to somewhere that explains things well. That other question does so very well. It's not like it's mentioned in passing in a question about something else. – Peter Cordes May 26 '16 at 23:28