62

In Intel x86 64 bit architecture there is the rax...rdx registers which are simply A...D general purpose registers.

But there are also registers called rsi and rdi which are the "source index" and "destination index" registers respectively. Why do these registers have actual names (compared to just A, etc)?
What does "source index" and "destination index" actually mean? And is there some convention that says these registers should be used in specific circumstances?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • 39
    Also note that A..D have names as well (Accumulator, Base, Counter, Data) which reflect their typical use. – Jester Apr 29 '14 at 14:47
  • @Jester, ah thank, we've never been told that so I just assumed it was A,B,C, and D – Jonathan. Apr 29 '14 at 14:54
  • Doesn't D stand for "divisor"? – Seva Alekseyev Apr 29 '14 at 15:51
  • 15
    It is not a productive way to think about it, these names are just an historic accident that goes back 38 years. Imposed by having to design a processor with only 29,000 transistors. You'll overlook that a 64-bit processor has 24 extra registers with boring names. – Hans Passant Apr 29 '14 at 16:22
  • 4
    Related: [Why are x86 registers named the way they are?](https://stackoverflow.com/questions/892928/why-are-x86-registers-named-the-way-they-are). – Peter Cordes Mar 07 '18 at 08:24
  • @SevaAlekseyev: No, and that wouldn't make any sense. `dx` is the upper half of the `dx:ax` *dividend* for 16-bit [`div`](https://github.com/HJLebbink/asm-dude/wiki/DIV) or `idiv`. Using `div dx` almost never makes sense. I haven't done much with 16-bit code, but I don't think there's any convention of using `dl` or `dh` for divisors either. – Peter Cordes Mar 07 '18 at 08:26

1 Answers1

42

These registers were originally implicitly used in repetitive instructions, for instance MOVSB, which copies a byte from DS:SI (DataSegment:SourceIndex) to ES:DI (ExtraSegment:DestinationIndex), at the time of the 16-bits computers with segmented memory in real mode. And also as index registers in 16-bit addressing modes like [bx + si].

Right now, these registers are for example used to transmit the first two (integer) function parameters in UNIX's x86_64 ABI, far from their original purpose. (See also What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64)

The names of the new rXX 64-bit registers clearly show that old register names are only here for familiarity and retro-compatibility.

But note that some instructions do still only work with some registers, for example rep movsb only works as a memcpy(rdi, rsi, rcx), and is in fact why RDI and RSI were chosen as the first 2 arg-passing registers in the x86-64 System V ABI: Some functions call memset or memcpy with their first 1 or 2 args, so inlining rep movsb/d is cheaper in that case.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
delehef
  • 681
  • 4
  • 7
  • The calling convention we've been given, says that parameters are pushed onto the stack? – Jonathan. Apr 29 '14 at 14:38
  • It depends on the OS and the processor. The calling convention I gave you is valable at least for GNU/Linux x64 and OS X x64. But as you said, Linux used stack-passing on x86-32. – delehef Apr 29 '14 at 16:23
  • `rdi` and `rsi` are still used implicitly by [`rep movsb` in x86-64](https://github.com/HJLebbink/asm-dude/wiki/REP_REPE_REPZ_REPNE_REPNZ). The instruction still exists, and it's actually useful for implementing `memcpy` in some cases. https://stackoverflow.com/questions/43343231/enhanced-rep-movsb-for-memcpy – Peter Cordes Mar 07 '18 at 08:11
  • 1
    The calling convention is different even for x86-64 where Microsoft uses different calling convention from everybody else: https://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_calling_conventions – it seems to me that Microsoft "x64" calling convention has slightly worse performance and it was invented later so I really cannot understand why it was created in the first place. – Mikko Rantalainen Dec 19 '21 at 12:54