简体   繁体   中英

Array addressing in AT&T GAS assembly. Register offset from RIP doesn't work

I'm trying to work with arrays in GNU assembly. In my opinion the following code must exit with value 3. But it exits with 13.

.section __DATA,__data
  inArr:
    .word 13, 2, 3, 4, 5, 6, 7, 8, 9, 10

  outArr:
    .fill 10, 2
.section __TEXT,__text
.globl _main
_main:


  movq $3, %rcx

  movw inArr(%rip, %rcx, 2), %di  # load  *((rcx * 2)+ rip + &inArray) into %di, isn't it?
  movl $0x2000001, %eax           # exit
  syscall

In my opinion movw inArr(%rip, %rcx, 2), %di command is equivalent to something like %di = inArr[%rcx] . Unfortunately I can't find any examples with array in GAS.

What's wrong with that code? And how shall I address n-th element of array?

There is no such thing as indexed RIP-relative addressing mode. Your assembler should give an error. Use this instead:

    lea inArr(%rip), %rdi
    movzwl (%rdi, %rcx, 2), %edi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM