0

I was looking at the naming conventions here, and saw that on a 64-bit system the AX register is called RAX. However when looking at my disassembled C code, I find that EAX is used constantly instead of RAX. This is not just the case for EAX, but also for EDI and a few other registers.

I can also find examples of RAX in my dissasembled code, so it does not seem to me that it's merely a naming convention that stuck with the 32-bit names. Will 64-bit x86 use the registers side by side, using 64 bit only when needed, and if so how does that work? If this is not the case and EAX and RAX both refer to the same register, then why the different names?

  • 2
    Does this answer your question? [The advantages of using 32bit registers/instructions in x86-64](https://stackoverflow.com/questions/38303333/the-advantages-of-using-32bit-registers-instructions-in-x86-64) – mediocrevegetable1 May 11 '21 at 19:34
  • And BTW, EAX and RAX are not *exactly* different registers per se. EAX is just the lower 32 bits of RAX. – mediocrevegetable1 May 11 '21 at 19:37
  • Much like how AX is the lower 16 bits of EAX. – Shawn May 11 '21 at 19:40
  • 1
    *int* stays at 32 bits in all main-stream [64-bit data models](https://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models), thus you'll see lots of eax. Accessing memory is the most expensive thing a processor needs to do, you don't get double the cache in a 64-bit processor. – Hans Passant May 11 '21 at 19:48
  • @HansPassant: I find it curious that 64-bit systems don't make more use of a 32-bit equivalent to the 8086 "small" model, where each process would have 4GiB of code address space and 4GiB of data address space, but pointers that aren't qualified `far` would only take four bytes. Many common programs would run more efficiently under such a model than under one where all pointers are 64 bits. – supercat May 11 '21 at 19:51
  • @supercat You mean the [x32 memory model](https://en.wikipedia.org/wiki/X32_ABI)? It never really caught on. – Shawn May 11 '21 at 20:21
  • @Shawn: Does the design of Linux make it necessary that all applications in a system use x64, or all applications use x32? If so, I could see that as being an impediment to x32 catching on, but at the same time I would regard that as a fault with the design of Linux rather than x32. – supercat May 11 '21 at 20:46
  • That does answer my question @mediocrevegetable1 Thank you – user14480191 May 11 '21 at 20:47
  • @supercat I don't think so. Multilib x86 and x86-64 is certainly a thing, at least. – Shawn May 11 '21 at 21:15

1 Answers1

2

%eax refers to the lower 32 bits of the 64-bit %rax register. So, if %rax is initially 0, and you execute the instruction movl $5, %eax, then %rax will also take on the value 5. However, they are not entirely independent, so one should be careful. For example, on my machine, if you set all bits of %rax to 1, and then perform xor %eax, %eax, you will find that ALL of %rax is set to 0, not just the lower 32 bits.

Dharman
  • 30,962
  • 25
  • 85
  • 135
squidwardsface
  • 389
  • 1
  • 7
  • 2
    RAX doesn't even have to be initially 0. Any operation on a general purpose 32-bit register zeroes out the higher 32 bits as well. Which is why `xorl %eax, %eax` zeroes out all of RAX. – mediocrevegetable1 May 11 '21 at 19:43
  • 2
    They're not independent at all: [Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?](https://stackoverflow.com/q/11177137) – Peter Cordes May 11 '21 at 19:45