Intel 64, rsi and rdi registers

Jonathan. picture Jonathan. · Apr 29, 2014 · Viewed 53.4k times · Source

in Intel 64 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. 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?

Answer

delehef picture delehef · Apr 29, 2014

These registers were originally implicitly used in repetitive instructions, for instance MOVSB, which copy 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 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.)