Menu
Coddy logo textTech

MOV with Different Sizes

Part of the Fundamentals section of Coddy's Assembly journey. Lesson 20 of 45.

You have already used al (8 bits) and rax (64 bits). But there are other sizes too. Different registers hold different amounts of data.

The x86-64 register families:

RegisterSizeBitsWhen to use
rax8 bytes64 bitsFull numbers, addresses, syscall numbers
eax4 bytes32 bitsLower half of rax
ax2 bytes16 bitsLower half of eax
al1 byte8 bitsLower half of ax (bits 0-7)
ah1 byte8 bitsUpper half of ax (bits 8-15)

The same pattern applies to other registers:

64-bit32-bit16-bit8-bit (low)
rbxebxbxbl
rcxecxcxcl
rdxedxdxdl
rsiesisisil
rdiedididil

Why size matters:

When you move data, the source and destination sizes should match. If they do not match, the CPU may:

  • Only copy part of the data
  • Copy extra bytes from unknown memory
  • Cause an error

Examples of matching sizes:

mov rax, 1000        ; 64-bit to 64-bit (fine)
mov eax, 1000        ; 32-bit to 32-bit (fine)
mov ax, 1000         ; 16-bit to 16-bit (fine)
mov al, 5            ; 8-bit to 8-bit (fine)

What happens with mismatched sizes:

mov rax, 0x123456789ABC   ; 64-bit value
mov al, 0xFF              ; al gets 0xFF, rax's other bits remain unchanged

When you move a smaller value into a larger register, the upper bits stay the same. This can cause unexpected results.

The safe rule:

In this course so far, all data in memory is 1 byte. That is why you use al (not rax).

When you move data, the register size must match the memory size:

  • 1 byte of memory → use al, bl, cl, etc.
  • 8 bytes of memory → use rax, rbx, rcx, etc.

You will learn about different memory sizes in Chapter 3.

challenge icon

Challenge

You have these data addresses already created:

  • source: contains a 1-byte value (5)
  • destination: empty space (1 byte)

Write two lines of assembly to:

  1. Load the value from source into al
  2. Store the value from al into destination

Try it yourself

section .data
    msg db 'destination = '
    msg_len equ $ - msg

section .bss
    digit resb 1

section .text
    global _start

_start:
    ; The student's code runs first (loads from source, stores to destination)
    
    %include "solution.asm"
    
    ; Load the stored value from destination
    mov al, [destination]
    
    ; Save it
    mov r12, rax
    
    ; Print "destination = "
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, msg_len
    syscall
    
    ; Print the value
    mov rax, r12
    add rax, 48
    mov [digit], al
    mov rax, 1
    mov rdi, 1
    mov rsi, digit
    mov rdx, 1
    syscall
    
    ; Exit
    mov rax, 60
    mov rdi, 0
    syscall
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Assembly playground