-2

I've seen the following definition for how an indexed address is generally used in assembly (from the book Programming from the Group Up):

movl BEGINNINGADDRESS(,%INDEXREGISTER,WORDSIZE)

I've used something like this on arrays (my most recent question) to do something like:

movl my_array(,%rdi,4), %r10 # move the int at the start of my_array into %r10

What is the first element before the first comma, or is this usually left blank? Is the "Beginning Address" always a variable name, or is it ever often something else? Instead of %INDEXREGISTER, is an absolute memory address or immediate value ever used?


Additionally, here is a good answer related to this: Referencing the contents of a memory location. (x86 addressing modes). I suppose my main question is on the purpose of the base item (which is left blank in the explanation/example above).

carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • Like I explained in [Referencing the contents of a memory location. (x86 addressing modes)](https://stackoverflow.com/a/34058400), it's a register that's simply added to the effective address without shifting. Often you can use just that instead of a scaled index, sometimes you even want both, even when the displacement part is an absolute address (a symbol) rather than a small integer. – Peter Cordes Aug 23 '20 at 07:07
  • assembly language is very specific to the assembler, the tool. not the target, in the future specify this kind of information so a proper answer (if not already answered) can be created. – old_timer Aug 23 '20 at 10:25

1 Answers1

2

The actual syntax is offset(base, index, scale), where offset and base are optional, and index,scale is also optional (but if one is present, both must be).

offset is an 8- or 32-bit constant, while base and index are registers, and scale is 1, 2, 4, or 8

These are all pretty flexible, so can be used many different ways.

  • base can be any register, so it might be the base of some segment (and the offset is the location of the array in the segment), or it might be the address of the array (so offset will be 0, or some fixed index with the array)
  • index can be any register except rsp (the stack pointer), and is multiplied by the scale factor before adding it to the base and offset. So it might be an index into the array, or it might be part of the calculation of the address of the array (with a fixed index in the offset)
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • thanks, I think I understand what `scale` is -- it's the size of the array element. Could you please add a bit about what the other three items are (`offset` and `base` are a bit unclear to me). – carl.hiass Aug 23 '20 at 06:02
  • 1
    @carl.hiass: scale is just a multiplier (encoded as a 2-bit shift count for the index). If you happened to have a counter that you incremented by 2 every iteration for some reason, you could use `mov array(,%rcx,2), %eax` to load every 4-byte dword. There's no need for the address calculation you use in asm to match or be transliterated from any specific high-level semantics, or to match the operand-size. – Peter Cordes Aug 23 '20 at 07:05