movsx
Move with sign extension
Copies a source into a register, sign-extending when the destination is wider.
386 and later
Intel's reference: MOVSX/MOVSXD
Used in
- Bytes, words, dwords and qwordsx86-64 assembly
CFPFAFZFSFDFOF –––––––
- Changes no flag
Forms
Operands
reg- The destination register. Its width is selected independently of the source width; widening uses a larger destination.
r/m- The source register or memory value, whose width may differ from the destination’s. For example, a byte source can be extended to a 64-bit register. High-byte registers such as
ahcannot be used with a REX prefix. acc- The accumulator matching the operation’s size:
al,ax,eaxorrax. A shorter encoding may be available.
For example, movsx rax, byte [rdi] turns a stored 0xff into rax = -1, and movsx rax, ecx extends a signed 32-bit value to 64 bits.
For widening moves, the source width is independent of the destination width: byte, word and dword memory sources read 8, 16 and 32 bits. NASM also accepts equal-width and narrowing spellings. movsx ax, bx copies 16 bits; movsx ax, ebx retains the low 16 bits. These do not create a wider signed value. A 32-bit destination still clears the upper half of its 64-bit register.
global _start
section .text
_start:
mov rax, 12
mov rbx, 5
mov rcx, 3
mov rsi, 9
movsx rax, bl
mov rax, 60 ; syscall 60: exit
xor rdi, rdi
syscall