Skip to content
Registersx86-64

RSI Register

RSI is the x86-64 source index: second argument on Linux and macOS, callee-saved on Windows, and the read pointer for movs, lods and cmps string instructions.

RSI has two jobs that have nothing to do with each other. To the calling convention it is the second argument on System V and a preserved register on Windows. To the CPU it is the source pointer for the string instructions. Which one you are looking at is usually obvious from the next instruction.

As an argument

ABIRole of RSI
System V AMD64 (Linux, macOS, BSD)second integer or pointer argument, caller-saved
Microsoft x64 (Windows)callee-saved; arguments use rcx, rdx, r8, r9
Linux syscallsecond argument

On Linux this makes RSI the buffer in read(fd, buf, n) and write(fd, buf, n), the flags in open(path, flags), and argv in both main(argc, argv) and execve(path, argv, envp). The standard 64-bit execve("/bin/sh") shellcode clears it for that reason:

asm
xor  esi, esi          ; argv = NULL
push rsi               ; string terminator
mov  rdi, 0x68732f2f6e69622f   ; "/bin//sh"
push rdi
mov  rdi, rsp          ; path
xor  edx, edx          ; envp = NULL
push 0x3b
pop  rax               ; execve
syscall

On Windows, RSI appearing in a prologue (push rsi, or mov [rsp+0x10], rsi) means the function wants a register that survives calls, exactly like RBX. That difference is a quick way to tell which ABI a blob of unlabelled code was compiled for: code that loads rsi and rdi right before a call is System V.

As the source index

The string instructions read from [rsi] and advance it by the operand size, forwards if the direction flag is clear and backwards if it is set:

  • movs copies from [rsi] to [rdi]. With a rep prefix and a count in rcx it is an inlined memcpy.
  • lods loads [rsi] into al, ax, eax or rax.
  • cmps compares [rsi] with [rdi]; repe cmpsb is an inlined memcmp.
  • outs writes [rsi] to an I/O port, which you will only meet in drivers and firmware.
asm
mov  rsi, rdx          ; source
mov  rdi, rax          ; destination
mov  ecx, 0x40         ; count
rep  movsb

lodsb in a loop is the signature of a hand-written decoder. It reads one byte and steps the pointer in a single-byte instruction (ac), so shellcode authors use it wherever a compiler would emit a movzx and an inc. The API-hashing loop in Metasploit and Cobalt Strike payloads is the best-known case: lodsb, a ror by 13 of the running hash, then an add. Find that triple and you have found the import resolver.

Both ABIs require the direction flag to be clear at function entry and exit. Code that runs std and does not follow it with cld is either copying an overlapping buffer backwards or trying to break tools that assume forward copies.

Sub-registers

ESI is the low 32 bits, SI the low 16 and SIL the low 8. There is no high-byte register. SIL needs a REX prefix: the same ModRM encoding without one means DH, so a disassembler that loses track of a prefix byte will show dh where sil was meant. Writing ESI zeroes the upper half of RSI.

Reverse-engineering notes

In a Linux binary, read RSI at a call site as "the second thing passed", then check the callee's prototype. For memcpy-family calls it is the source, so the data you want to see is at the address in rsi before the call, not after.

In a rep movs or lods loop, set a breakpoint on the instruction and dump [rsi]. For a decryption stub that is the ciphertext; the plaintext shows up at [rdi] when the loop finishes.

Related: RDI, its destination counterpart, string operations, calling conventions, and the register map.