Skip to content
systemcoding.dev
Assembly

Move Data Between Registers and Memory With MOV, MOVZX and MOVSX

Load and store bytes, words, dwords and qwords on x86-64 with MOV, and widen small values correctly with MOVZX, MOVSX and MOVSXD. Worked examples included.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Intel, Arm and AMD logos.

mov is the most common instruction in any x86-64 listing, and it hides a trap: it copies exactly the width you name and does nothing about the bits above it. Load a signed byte into a 32-bit register with the wrong instruction and a value of -10 becomes 246, a bug that compiles cleanly and survives most tests. This post covers the forms of mov, then the two widening instructions movzx and movsx (plus movsxd for 32 to 64 bits) that make small values behave, with the actual register contents shown.

The forms of mov

mov destination, source copies the source into the destination and leaves the source unchanged. It does not change any flag. The legal combinations are:

FormExample
register from registermov rax, rbx
register from memory (load)mov eax, [rdi]
memory from register (store)mov [rdi], eax
register from constantmov eax, 42
memory from constantmov dword [rdi], 42

Memory to memory is not allowed. To copy 4 bytes from one place to another, go through a register:

asm
        mov     eax, [rsi]         ; load
        mov     [rdi], eax         ; store

The width of the access comes from the register, or from a size keyword (byte, word, dword, qword) when only memory and a constant appear. The write rules for sub-registers from the post on general-purpose registers apply: a 32-bit destination zeroes the upper half, while an 8-bit or 16-bit destination leaves it alone.

Immediates have three sizes

A constant can appear in three encodings, and the assembler picks the shortest that works:

asm
        mov     eax, 5                       ; 5-byte instruction, zeroes upper rax
        mov     rax, 5                       ; 7 bytes: imm32 sign-extended to 64 bits
        mov     rax, 0x1122334455667788      ; 10-byte instruction, full 64-bit constant
        mov     qword [rdi], -1              ; imm32 sign-extended to 64 bits

Only the mov to a register has a true 64-bit immediate form (the instruction Intel calls mov r64, imm64). Every other instruction takes at most a 32-bit immediate, sign-extended. So mov qword [rdi], 0x100000000 is an error, because the constant does not fit in a sign-extended 32 bits, and you load it into a register first.

The first two lines load the same value. Writing mov eax, 5 yourself is shorter than mov rax, 5 and clears the upper half anyway, so prefer it for any non-negative constant below 2^32. A negative constant is the exception: mov rax, -1 needs the 64-bit register, because mov eax, -1 would leave rax equal to 0x00000000FFFFFFFF.

The widening problem

A value in memory is only a sequence of bytes. Whether 0xF6 means 246 or -10 depends on the type. C inserts the right conversion when you assign a signed char to an int. In assembly you choose it. Take a byte array and an int-returning function.

Moving a byte into a 32-bit register with mov is not even possible, since the sizes differ. Moving it into al leaves the rest of eax as it was:

asm
        mov     eax, 0x12345678
        mov     al, [rdi]          ; *rdi = 0xF6
Text
eax = 0x123456F6

The upper 24 bits are old garbage. To widen properly, use one of two instructions:

  • movzx (move with zero extension) fills the upper bits with zeros. Use it for unsigned values.

  • movsx (move with sign extension) copies the top bit of the source into every new upper bit. Use it for signed values.

widen.asmasm
; long load_u8(const unsigned char *p)
load_u8:
        movzx   eax, byte [rdi]        ; zero-extend 8 -> 32 (and so rax)
        ret

; long load_s8(const signed char *p)
load_s8:
        movsx   rax, byte [rdi]        ; sign-extend 8 -> 64
        ret

If *p is 0xF6, the results are:

Text
load_u8 -> rax = 0x00000000000000F6   (246)
load_s8 -> rax = 0xFFFFFFFFFFFFFFF6   (-10)

The sign-extended version copies the top bit (1) into bits 8 to 63. The zero-extended version only needs the 32-bit form movzx eax, ... because writing eax already clears the upper half of rax, and the shorter instruction has no REX prefix. The compiler always picks movzx eax, byte [rdi] over movzx rax, byte [rdi] for that reason.

The sources and destinations that exist are:

InstructionSourceDestination
movzx8 or 16 bits16, 32 or 64 bits
movsx8 or 16 bits16, 32 or 64 bits
movsxd32 bits64 bits

There is no movzxd. A 32-bit zero-extension to 64 bits is a plain mov eax, ecx, since the hardware already zero-extends 32-bit writes.

asm
; long sext32(const int *p)
sext32:
        movsxd  rax, dword [rdi]       ; sign-extend 32 -> 64
        ret

With *p equal to -2 (stored as 0xFFFFFFFE), the function returns 0xFFFFFFFFFFFFFFFE, which is -2 as a 64-bit signed number. Had it used mov eax, [rdi], the result would be 0x00000000FFFFFFFE, which is 4,294,967,294 as a long. NASM accepts movsx rax, dword [rdi] as a synonym for movsxd.

Choosing the right extension from the C type

The C type of the value tells you which instruction the compiler emits, and you can make the same decision by hand:

C type of the sourceWidening to a wider integer
unsigned char, uint8_tmovzx
signed char, int8_tmovsx
unsigned short, uint16_tmovzx
short, int16_tmovsx
unsigned int, uint32_tmov to a 32-bit register
int, int32_tmovsxd

Plain char is signed on x86-64 Linux with GCC, so a char read from a string widens with movsx. That matters for text containing bytes above 127: strcmp-style code written with movsx will treat a UTF-8 byte such as 0xC3 as negative, and so compare it as smaller than ASCII.

A complete worked example

Sum the bytes of a buffer as unsigned values, widening each one:

sumbytes.asmasm
; unsigned long sum_bytes(const unsigned char *p, unsigned long n)
global sum_bytes
sum_bytes:
        xor     eax, eax               ; total = 0
        test    rsi, rsi
        jz      .done
.next:
        movzx   edx, byte [rdi]        ; zero-extend one byte
        add     rax, rdx               ; total += byte
        inc     rdi
        dec     rsi
        jnz     .next
.done:
        ret

Call it on the three bytes {0xFF, 0x01, 0x80}. Since the bytes are widened as unsigned values, the sum is 255 plus 1 plus 128:

Text
384

Now swap movzx for movsx edx, byte [rdi] to treat the bytes as signed. The result is wrong in a quiet way. The movsx writes a 32-bit register, so edx holds 0xFFFFFFFF for the byte 0xFF, and that 32-bit write zero-extends into rdx. The 64-bit add then adds 4,294,967,295 instead of -1, and the three bytes total 8,589,934,464 instead of -128. The fix is a 64-bit destination, movsx rdx, byte [rdi], which keeps the sign all the way up.

Stores never extend

The widening instructions are loads. A store writes exactly the width you give it, so storing a 32-bit register into a byte array element needs the 8-bit name of the register: mov [rdi], al writes one byte, and mov [rdi], eax writes four and clobbers the three neighbors. When you store the low byte of esi or edi, the register must be one of the byte registers that exist in 64-bit mode, sil and dil, which a REX prefix makes available. A function that fills a buffer with a byte value therefore reads mov [rdi+rcx], sil with no widening anywhere, and the compiler never emits movzx on the store side.

When partial moves go wrong

A mov al, [rdi] followed by use of eax is called a partial register write, and on some CPUs the merge costs a cycle or causes a dependency on the old value of the register. Prefer movzx eax, byte [rdi] for loading a byte you intend to use as a number. A later post on partial register writes and flag dependencies explains the microarchitecture reasons.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.