1

Now I am pretty sure this is a basic question but I couldn't find the answer. How many bits at most can offset be ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Aaa Bbb
  • 627
  • 4
  • 12
  • 2
    A disp32 is 32 bits at most. [Referencing the contents of a memory location. (x86 addressing modes)](https://stackoverflow.com/q/34058101) – Peter Cordes Dec 16 '21 at 09:02

1 Answers1

4

There are six addressing modes supporting this format. The assembler will automatically pick the shortest addressing mode into which the displacement fits. The three relevant of these addressing modes are:

  • register indirect, no displacement (displacement must be zero)
  • register indirect, 8 bit displacement (displacement must be between −128 and 127)
  • register indirect, 32 bit displacement (displacement must be between −2147483648 and 2147483647)

It is not possible to encode a greater displacement than what these addressing modes support. If you need a greater displacement, perform arithmetic to load it. For example, do

mov rax, 123456789abcdef0h
lea rax, [rbx+rax]
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    Or better, `add rax, rbx` avoids a SIB byte so it's shorter than LEA, if you don't need to avoid writing FLAGS and were just hoping to use LEA as a non-destructive copy-and-ADD. – Peter Cordes Dec 16 '21 at 12:48
  • 1
    @PeterCordes I had chosen `lea` specifically to not write the flags. – fuz Dec 16 '21 at 13:04
  • 2
    That's one minor reason for using LEA this way. The usual one is to save a MOV, so IMO it makes sense to at least mention the fact that LEA is not the most efficient choice anymore in that case, except when leaving FLAGS unmodified is actually useful to avoid another instruction or something. Worse code size, and can't run on as many ports on some CPUs. – Peter Cordes Dec 16 '21 at 13:29