Is there a way that we can rewrite the below assembly operation using only mov instruction
movzx eax, WORD [short_temp]
Is there a way that we can rewrite the below assembly operation using only mov instruction
movzx eax, WORD [short_temp]
You never need to avoid movzx
. If 32-bit registers like EAX are available, then your code is running on a 386 or later so movzx
is supported, too.
It's by far the most efficient way to do this on an Intel CPU. movzx r32, [memory]
decodes to just a load uop, with no ALU operation. The latency is the same as normal load (IIRC), so the zero extension is done for free by the load unit.
On an AMD Bulldozer-family CPU, movzx
always uses an ALU port (as well as a load port when it's a memory operand), so it could in theory be faster to xor-zero EAX before using a word-load (@Downvoter's answer). This might give you one cycle lower load-use latency at the cost of larger code-size and more m-ops for the frontend to decode.
Other suggestions like doing a word-load into AX and then and eax, 0xFFFF
are obviously worse, because of reading EAX after a partial-register write (of AX), and because it's a separate ALU operation that adds another cycle of latency. (xor-zeroing avoids the partial-register penalty, even on Intel P6-family CPUs.)
Of course, easily. Just zero out eax
and set ax
afterwards:
XOR EAX, EAX
MOV AX, [short_temp]
If you want to obtain a word from [EAX]
as you said here, just store the address to some other register beforehand:
MOV EBX, EAX
XOR EAX, EAX
MOV AX, [short_temp]
or, if that's not possible, do as @fuz proposed and clear the upper 16 bits of EAX
using AND
:
MOV AX, [EAX]
AND EAX, FFFFh