← again tap: answer good → ↑ swipe up at the end: next
Question
What does the lea instruction do and how is it different from mov?
Answer
lea computes the address and stores the address itself; mov with the same operand would instead read the memory at that address.
* lea keeps the computed address; mov with the same operand instead loads the value stored there. *
# rax = rdi + rsi*4 (just math, no memory access)
lea (%rdi,%rsi,4), %rax
# rax = MEM[rdi + rsi*4] (reads from memory)
mov (%rdi,%rsi,4), %rax
Common uses:
- Fast arithmetic — multiply and add in one instruction:
# rax = rdi * 3
lea (%rdi,%rdi,2), %rax
# rax = rdi * 5 + 7
lea 7(%rdi,%rdi,4), %rax
- Address computation — get pointer to array element:
# rax = &A[i]
lea (%rdi,%rsi,4), %rax
Key property: lea never accesses memory and never modifies flags.
Go deeper:
LEA instruction reference — confirms LEA computes the effective address, touches no memory, and affects no flags.
x86 instruction listings (Wikipedia) — places LEA among mov and the addressing-mode instructions for comparison.
Question
What is the movzbl instruction and when does the compiler use it?
Answer
"Move zero-extend byte to long": it reads one byte and fills the rest of the 32-bit destination with zeros.
# Read 1 byte from memory, fill upper bits with zeros
movzbl (%rax), %edx
If memory at (%rax) contains 0x41 ('A'), then %edx becomes 0x00000041.
The naming pattern: mov + z(zero) or s(sign) + source size + dest size
| Instruction | Meaning |
|---|---|
movzbl |
Zero-extend byte → long (32-bit) |
movzbq |
Zero-extend byte → quad (64-bit) |
movzwl |
Zero-extend word → long |
movsbl |
Sign-extend byte → long |
movslq |
Sign-extend long → quad |
Common context: Reading a char from a string, reading a byte from a lookup table, or widening a small value before arithmetic.
Go deeper:
MOVZX (zero-extend) reference — movzbl is MOVZX byte-to-long; the page shows DEST := ZeroExtend(SRC).
x86 instruction listings (Wikipedia) — situates MOVZX/MOVSX among the data-movement instructions.