0

How would I go about indexing and declaring arrays in AT&T assembly?

Declaring do I do it like this:

array:
    .zero 256

Creates array of 256, with values of zero.

Indexing it do I do it like this:

movq $array, %r14                //Set array to a register name
                                 //Say that r11 has the number 5 stored
movq (%r14, %r11, 8), %r15       //This will make r15 at index 5 of array
movq %rbx ,%r15                  //This will store value of rbx into r15

Is this how I do it? If not, how do I go about creating and indexing arrays in AT&T assembly?

rkhb
  • 14,159
  • 7
  • 32
  • 60
  • the language is defined by the tool, what assembler (tool(chain)) are you using? – old_timer May 25 '18 at 19:00
  • Use `lea array(%rip), %r14` or `movl $array, %r14d`. (Or skip it entirely and use `movq array(,%r11,8), %r15`.) Never use `movq $symbol_name`; it's never the best way to get a symbol address into a register. (See [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427) for more about when you need an LEA to avoid 32-bit absolute addresses like you're using here. `movq` still uses a 32-bit immediate.) – Peter Cordes May 25 '18 at 20:15
  • @Peter, I don't understand your last sentence; 48b8 does use a 64-bit immediate. – prl May 26 '18 at 00:53
  • @prl: `mov r64,imm64` is movabs, not movq. AT&T syntax added a different mnemonic for the 64-bit immediate or absolute-address forms of MO: https://web.archive.org/web/20160609221003/http://www.x86-64.org/documentation/assembly.html. (I didn't mention `movabs` because it's definitely worse than RIP-relative LEA for code-size.) – Peter Cordes May 26 '18 at 01:26

1 Answers1

0

Your assembly will store the address of the start of the array in r14, move the value of the r11th element of the array into r15, then move the value in rbx into r15. It will not move any value into the array. If you want to move the address of the r11th element into r15 then move the value in rbx to the r11th element of the array, you would need to use leaq (%r14, %r11, 8), %r15 to move the address of the r11th element of the array into r15, then use movq %rbx, (%r15) to move the value in rbx to the r11th element of the array.

JustinCB
  • 513
  • 4
  • 12