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:
| Register | Size | Bits | When to use |
|---|---|---|---|
rax | 8 bytes | 64 bits | Full numbers, addresses, syscall numbers |
eax | 4 bytes | 32 bits | Lower half of rax |
ax | 2 bytes | 16 bits | Lower half of eax |
al | 1 byte | 8 bits | Lower half of ax (bits 0-7) |
ah | 1 byte | 8 bits | Upper half of ax (bits 8-15) |
The same pattern applies to other registers:
| 64-bit | 32-bit | 16-bit | 8-bit (low) |
|---|---|---|---|
rbx | ebx | bx | bl |
rcx | ecx | cx | cl |
rdx | edx | dx | dl |
rsi | esi | si | sil |
rdi | edi | di | dil |
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 unchangedWhen 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
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:
- Load the value from
sourceintoal - Store the value from
alintodestination
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
syscallThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture3First instruction - MOV
What is MOV?MOV ImmediateMOV Register to RegisterMOV Memory to RegisterMOV Register to MemoryLEA - Load AddressMOV with Different SizesPractice: Swap RegistersRecap: MOV Challenge