How can I retrieve data from a .long statement?
For example:
.data
data_items:
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0
.text
.globl _main
_main:
movl $0, %edi
movl data_items(,%edi,4), %eax
Gives a large series of errors about absolute addressing not being allowed in x86_64. How can I access this data? I’m fairly new to assembly, so I apologize if my terminology is confusing.
EDIT: I am using GNU Assembler/GCC
The problem is that your data is in the data segment and your code is in the text segment. The linker is set up to require relocatable code, which means you can’t use an absolute address, since you cannot know the absolute address until runtime.
To use relocatable code, you need to access
data_itemsas an offset from the instruction pointer,rip.The
leaqinstruction gets the address ofdata_itemsusing an offset of the instruction pointer, which can be calculated at link time. Then themovlinstruction uses that address as the base for loading the data. Note that I usedrdiin the addressing. When you write toedi, the upper 32 bits ofrdiare automatically cleared, so this will work unmodified as long as the value inediis unsigned. You could useediandeax, but that would truncate addresses which use more than 32 bits, and the compiled code would be larger since the default address size is 64 bits.