LEA - Load Address
Part of the Fundamentals section of Coddy's Assembly journey — lesson 19 of 45.
So far, you have used mov to load values from memory into registers. But sometimes you do not want the value — you want the address itself.
LEA stands for Load Effective Address. It calculates a memory address and puts that address into a register, without going to memory.
Syntax:
lea register, [address]MOV vs LEA:
| Instruction | What it does |
|---|---|
mov al, [number] | Goes to memory, gets the value inside, puts it into al |
lea rsi, [number] | Does NOT go to memory. Calculates the address of number and puts it into rsi |
Think of it like this:
mov= open the box and take what is insidelea= write down the box number (address) on a piece of paper
Example:
number db 42 ; The value 42 is stored at some address
mov al, [number] ; al = 42 (the value)
lea rsi, [number] ; rsi = address of number (e.g., 0x402000)Why do you need LEA?
In assembly, when you want to print a string or pass a memory location to a function, you need the address, not the value. The write syscall (which you will learn in Chapter 5) expects an address in rsi.
What LEA can do (beyond simple addresses):
LEA can also do basic math with addresses. For example:
lea rsi, [number + 5] ; rsi = address of number + 5 bytesThis is useful for working with arrays and data structures.
Challenge
Write one line of assembly to load the address of message into the rsi register.
Try it yourself
section .data
msg_prefix db 'message = '
prefix_len equ $ - msg_prefix
section .text
global _start
_start:
; The student's lea instruction runs first
; rsi should now contain the address of message
; Print prefix
mov rax, 1
mov rdi, 1
mov rsi, msg_prefix
mov rdx, prefix_len
syscall
%include "solution.asm"
; Print the string using the address the student loaded
mov rax, 1
mov rdi, 1
; rsi already has the address (from student's code)
mov rdx, 6 ; "Hello" + newline = 6 bytes
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