LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 13
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

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.

For the same operand (%rdi,%rsi,4), lea stores the computed address itself while mov dereferences it and stores MEM 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:

  1. 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
  1. 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:

or press any other key

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:

or press any other key