0

I am currently accessing an array like this:

ARRAY:   .long 1,2,3,4
# put index in register ebx
mov $4, %ebx
# put nums value in eax
mov nums(,%ebx,4), %eax

Is it possible to, instead of using a register for the array to use an address on the stack? For example if I have the value 4 stored at %rbp-4, something like:

mov nums(-4,%rbp,4)
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David542
  • 104,438
  • 178
  • 489
  • 842
  • 3
    No. See https://stackoverflow.com/questions/34058101/referencing-the-contents-of-a-memory-location-x86-addressing-modes for a rundown of x86-64 addressing modes. There is no "doubly indirect" addressing mode like this. You'll have to load the index into another register first. – Nate Eldredge Sep 16 '20 at 03:48
  • @NateEldredge I see. Thanks for the feedback. – David542 Sep 16 '20 at 04:12
  • `nums-4(,%rbp,4)` is a valid addressing mode but it doesn't access memory twice. – Peter Cordes Sep 16 '20 at 12:56
  • @PeterCordes could you please clarify what you mean by 'it doesnt access twice' ? – David542 Sep 16 '20 at 17:28
  • I mean it's not double-indirection like you want. It calculates an address based on registers and the constant displacement, `[num - 4 + rbp*4]`, and accesses memory once at that address. I just found it weird that you thought that your almost-valid syntax of mov from `nums(-4,%rbp,4)` might do 2 loads, because the valid version of that syntax doesn't. – Peter Cordes Sep 16 '20 at 18:21

0 Answers0