'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?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |