TExcelTAD
Home › x86-64 Assembly › lea: Address Arithmetic

lea: Address Arithmetic

lea (load effective address) computes the address described by a memory operand and puts the address itself into a register. It never touches memory. That makes it useful for two things: getting pointers, and doing fast arithmetic.

nasm
mov rax, [rbx + 8]      ; rax = the 8 bytes stored at address rbx + 8
lea rax, [rbx + 8]      ; rax = rbx + 8   (no memory access)

Getting the address of something

The most common use is to compute a pointer, for example to the third element of an array or to a string for write:

nasm · try it yourself
section .data
names:  db "Ada  Alan Grace"   ; three 5-byte slots

section .text
global _start
_start:
    mov ecx, 2                  ; we want slot number 2
    lea rsi, [names + rcx*4]    ; names + 2*4 ...
    add rsi, rcx                ; ... + 2 = names + 10 (slots are 5 bytes)
    mov eax, 1
    mov edi, 1
    mov edx, 5
    syscall

    mov eax, 60
    xor edi, edi
    syscall
Grace

Note: Since slots are 5 bytes wide and the scale can only be 1, 2, 4 or 8, the example computes names + rcx*4 + rcx in two steps. The next section shows a neater way.

lea as a calculator

Because the CPU's address unit can compute base + index*scale + constant in a single instruction, compilers use lea for plain arithmetic all the time. It has two extra advantages over add/imul: the destination can differ from both sources, and flags are not changed.

lea form Computes
lea rax, [rdi + rsi] rax = rdi + rsi
lea rax, [rdi + 10] rax = rdi + 10
lea rax, [rdi + rdi*2] rax = rdi * 3
lea rax, [rdi*4 + 7] rax = rdi * 4 + 7
lea rax, [rdi + rdi*4] rax = rdi * 5
lea rax, [rdi + rdi*8] rax = rdi * 9
nasm · try it yourself
section .text
global _start
_start:
    mov r12, 7
    mov r13, 100

    lea rdi, [r12 + r13]        ; 7 + 100
    call print_uint
    lea rdi, [r12 + r12*2]      ; 7 * 3
    call print_uint
    lea rdi, [r12 + r12*4]      ; 7 * 5
    call print_uint
    lea rdi, [r13 + r12*8 - 6]  ; 100 + 56 - 6
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
107
21
35
150

Combining two lea instructions multiplies by more constants: x * 10 is lea rax, [rdi + rdi*4] (×5) followed by add rax, rax (×2). With that, the 5-byte slot example becomes clean:

nasm · try it yourself
section .data
names:  db "Ada  Alan Grace"

section .text
global _start
_start:
    mov ecx, 1                  ; slot 1 this time
    lea rax, [rcx + rcx*4]      ; rax = rcx * 5 (the byte offset)
    lea rsi, [names + rax]      ; rsi = address of the slot
    mov eax, 1
    mov edi, 1
    mov edx, 5
    syscall

    mov eax, 60
    xor edi, edi
    syscall
Alan

A Celsius to Fahrenheit converter

F = C × 9 / 5 + 32. The multiplication by 9 is a single lea; the division uses div, which you will meet properly in Multiplication & Division:

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 100                ; degrees Celsius
    lea rax, [rdi + rdi*8]      ; rax = C * 9
    xor edx, edx
    mov ecx, 5
    div rcx                     ; rax = C * 9 / 5
    lea rdi, [rax + 32]         ; + 32
    call print_uint             ; 212

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
212

lea with RIP-relative addresses

In position-independent code (default rel or [rel label]), lea is the way to get the address of a global: lea rsi, [rel message]. The mov rsi, message form used in this course needs an absolute address, which only works in non-PIE executables.

Common mistakes

  • lea rax, [rbx] does not load from memory; it is just mov rax, rbx.
  • The source of lea must be a memory operand: lea rax, rbx is an error.
  • The scale is limited to 1, 2, 4 or 8, and only one register may be scaled.

Test yourself

If rdi = 6, what does lea rax, [rdi + rdi*4 + 2] put in rax?

  1. 32
  2. 26
  3. 30
  4. Whatever is stored at address 32

Which is an advantage of lea rax, [rbx + rcx] over mov rax, rbx + add rax, rcx?

  1. It reads memory faster
  2. One instruction, and it does not modify the flags
  3. It works with any scale
  4. It sets the carry flag on overflow

How can you compute rdi * 9 with one instruction?

  1. lea rax, [rdi*9]
  2. lea rax, [rdi + rdi*8]
  3. lea rax, [rdi*8 + 9]
  4. mov rax, rdi*9