0

For accessing arrays, I have the seen the following two notations used quite often:

# Addressing in the general form of: 
#  address_or_offset(%base_or_offset, %index, scale)

# (1) using a label, which resolves to an address
movzwq myarray(, %rdi, 2), %rbx           # 0x400078

# (2) using an offset based on a register, usually %rbp
movzwq -8(%rbp, %rdi, 2), %rcx

In the first form it uses the notation of address(offset, index, scale) and in the second it uses the notation of offset(base, index, scale). My question is whether it is every practical to use the first notation with including an index register, something like:

movzwq myarray(%r11, %rdi, 2), %rbx

Of is that never used, and it's just always left blank (like in the first example) when using that address(, index, scale) notation?

Finally, is offset/base and address/offset just two ways of doing the same thing, that is, it allows arriving at start memory location by doing an offset from a register in the first case and from an address/label in the second case?

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 1
    My answer on [Referencing the contents of a memory location. (x86 addressing modes)](https://stackoverflow.com/q/34058101) shows a hypothetical example of `mov al, [global_array + ecx + edx*2 + 10]` for indexing a 2D char array with 2 separate indices, if the row width happens to be small power of 2 you can do with the scale factor. Or of course if you wanted to access `arr[i*5]`, you might use `arr(%rax, %rax, 4)` to do the math in the addressing mode. It's not hard to come up with hypothetical examples where a compiler could and maybe would do that, try it on https://godbolt.org/ – Peter Cordes Aug 25 '20 at 02:54
  • And yes, it doesn't matter how you got there, only what the final result of the address calculation is. Like always in asm, multiple ways to implement high-level logic. (Modulo some code-size and performance differences, like indexed addressing modes [being slower in some cases](https://stackoverflow.com/questions/26046634/micro-fusion-and-addressing-modes/31027695#31027695), or [Is there a penalty when base+offset is in a different page than the base?](https://stackoverflow.com/q/52351397) which only happens with `0..2047(%reg)` addressing modes, and only if the reg came from a load.) – Peter Cordes Aug 25 '20 at 02:57

0 Answers0