Recently started messing with AArch64 assembly and I noticed that it has an assigned register strictly for zero, whereas (most) other architectures you would just xor var, var
.
The site I was reading about zr
explained it as a reference point for zero which sounds a lot like how I define ground in DC electronics. And because ARM is used by hobbyists, tying ground in the circuit to zero in the code kinda makes sense to me.
I'm sure it's much more complex than this, but is this a safe analogy to make? And would using this register compared to other ways of getting '0' result in different outcomes?
The zero register xzr
resp. wzr
is a cute design trick in the Aarch64 ISA. It's register number is 31, just as the stack pointer sp
resp. wsp
. Depending on the context, register number 31 refers to one of them.
This cute trick allows the Aarch64 ISA to simplify its instruction set. For example, the cmp xn, xm
instruction is actually subs xzr, xn, xm
, i.e. it's a subtraction with the result being discarded. A mov xn, xm
is simply an orr xn, xzr, xm
, i.e. a bitwise-or of the source with zero. Register 31 is only recognised as the stack pointer where it makes sense and the instruction set has been cleverly chosen so you almost never hit on this limitation.