I was using a disassembler when I came across MOVZ and was a bit confused, since I had only used MOV before.
The ARMv8 ISA manual explains of course all the details, and that MOV is an alias for the other three depending on the context, but maybe someone can provide some rationale here, and give concrete examples to speedup the learning process.
MOV
This instruction can take many forms, depending on the value needed to be moved. And it changes if the value is a register or if it is an immediate. If it is in a register, then it produces an
ORR
instruction (ORR <Xd>, XZR, <Wm>
)*. If it is using theSP
(Stack Pointer) it produces anADD
instruction (ADD <Xd|XSP>, <Xn|XSP>, #0
)*. If moving an immediate, then it is one of the MOVZ, MOVK or MOVN instructions.MOVZ and MOVK
These two instructions are sometimes used one after the other. And they are used to move immediate values.
MOVZ
moves an immediate value (16-bit value) to a register, and all the other bits outside the immediate value are set to Zero. The immediate can be shifted to the left0
,16
,32
or48
.MOVK
moves an immediate value but leaves the other bits of the register untouched (theK
is for keep). For example, let's say you need to move this value0x7fb7fb1f88
to registerx0
. First, you will move the first 16 bits (bits 0 to 15) with aMOVZ
instruction, so the rest bits of the register are set to zero. And then you will move the second 16 bits (bits 16 to 31) with aMOVK
instruction, so the value moved before (the first 16 bits) remains in the register, and you do the same with the other resting bits.MOVN
MOVN
is usually used for moving bitmasks, here theN
stands for negated. Let's say you want to move the bitmask0xffffffff0000ffff
to x0. Then you will move0xffff
shifted to the left 16, that will make the value0x00000000ffff0000
. Negating this values becomes 0xffffffff0000ffff.Here is an example: