MOV Memory to Register
Part of the Fundamentals section of Coddy's Assembly journey. Lesson 17 of 45.
So far, you have moved numbers directly into registers (immediate) and copied values between registers. Now you will learn how to load data from memory into a register.
Why this matters:
Remember from Chapter 0: the CPU cannot work directly with memory. Data must be loaded into registers first. This is how you do it.
Syntax:
mov register, [address]The brackets [ ] mean: "go to this memory address and get the value stored there."
Think of it like this:
Imagine at memory address number there is a box. Inside that box is a value, for example the number 5. When you write:
mov al, [number] ; Load the value from memory address "number" into alYou are telling the CPU: "Go to the box called number, take whatever value is inside (in this case 5), and copy it into the al register."
After this instruction is executed, the value 5 is now stored in al. The original value inside the box at address number stays there. It is not removed.
Important rules:
| Rule | Explanation |
|---|---|
| The source must be a memory address | [label] or [address] |
| The brackets are required | mov al, myNumber loads the address, not the value |
| Size must match | If memory stores a byte, use al (not rax) |
Common mistake:
; Wrong: loads the ADDRESS of myNumber, not the value
mov al, myNumber
; Correct: loads the VALUE stored at myNumber
mov al, [myNumber]Challenge
Write one line of assembly to load the value from memory address value into the al register.
Try it yourself
section .data
msg db 'al = '
msg_len equ $ - msg
section .bss
digit resb 1
section .text
global _start
_start:
; The student's code runs first (loads value into al)
%include "solution.asm"
; Save the value from al
mov r12, rax
; Print "al = "
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, msg_len
syscall
; Print the value in al
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 ChallengePractice on your own: Assembly playground