In ARM64 assembly code, when does register number 31 mean XZR and when does it mean SP?
Whether an instruction interprets register 31 as the zero register wzr
/xzr
or the stack pointer wsp
/xsp
depends on the instruction type and isn't easily predictable. The same mnemonic may refer to multiple instructions, some of which accept the stack pointer as an operand and others that don't. In general though, all instructions that make sense to use with the stack pointer have a form where they can be used with the stack pointer. The assembler automatically picks an appropriate form for you. It will never silently take sp
to mean xzr
or vice versa.
In the ARM Reference Manual, Xn
or Wn
is used to refer to a register where register 31 refers to the zero register. Xn|SP
or Wn|WSP
is used to refer to a register where register 31 refers to the stack pointer. See the introduction to §5 “A64 INSTRUCTION SET” for details.