Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

assembly - Is Zero Register 'zr' in aarch64 essentially ground?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 subtract with the result being discarded. A mov xn, xm is simply an orr xn, xzr, xm. 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 this detail.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...