MOV Register to Memory
Part of the Fundamentals section of Coddy's Assembly journey — lesson 18 of 45.
Now that you know how to load data from memory into a register, you will learn the reverse: storing data from a register into memory.
Syntax:
mov [address], registerThe brackets [ ] mean: "store the value from the register into this memory address."
Think of it like this:
Imagine at memory address destination there is an empty box. You have a value sitting in a register (for example, al contains the number 7).
When you write:
mov [destination], alYou are telling the CPU: "Take the value from the al register (7) and put it into the box called destination."
After this instruction is executed, the memory address destination now contains the value 7. The value in al stays there — it is not removed.
Example:
mov al, 7 ; Put 7 into al register
mov [destination], al ; Store the value from al into memory address "destination"Note: In this exercise, the data in memory is 1 byte (8 bits). That is why we use al — it is the 8-bit register that matches the size. You will learn more about register sizes later.
Important rules:
| Rule | Explanation |
|---|---|
| The brackets are required | mov destination, al would be wrong |
| The destination must be a memory address | Use a label like [destination] |
| Size must match | For this exercise, use al (8 bits) |
Common mistake:
; Wrong: missing brackets
mov destination, al
; Correct: stores the value into memory
mov [destination], alIn the challenge below, the memory address destination is already created for you. You will learn how to create your own data in Chapter 3. For now, just focus on the MOV instruction.
Challenge
Write two lines of assembly to:
- Put the number 9 into the
alregister - Store the value from
alinto the memory addressdestination
(The memory address destination is already created for you.)
Try it yourself
section .data
msg db 'destination = '
msg_len equ $ - msg
section .bss
digit resb 1
section .text
global _start
_start:
; solution.asm is included here
; It defines destination AND the student's MOV instructions
%include "solution.asm"
; Load the stored value from memory into al for printing
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