0

I am a beginner in assembly language, I am bit confused about how an array is addressed.

Suppose there is an array(named "list") of 100 integers(4 bytes each) stored at a location(say 0x10010010). how do I find the address of its elements, like list[3] or list[16]?

Also, how do i write an assembly directive to specify the memory for this array?

Naveen
  • 1,363
  • 7
  • 17
  • 28
  • I'd suggest looking at compiler output to see how they do it, for any ISA you have a C compiler for. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). If you have a specific ISA and assembler syntax in mind (e.g. x86-64 NASM), a more specific answer is possible. – Peter Cordes Jun 11 '20 at 19:16

1 Answers1

2

You said it yourself; each integer is 4 bytes long. They are stored contiguously in memory. list is the address of the first of them (zeroth, to be precise). Assuming that you use zero based indexing when you say list[3], the address of list[3] will be list + 3*4, or list+12. In general, the formula is base+index*4, where base is the address of the array, and index goes from 0 to 99. 4 comes from the integer size.

To use your example, if the array is at 0x10010010, then list[3] would be at 0x1001001C, list[16] would be at 0x10010050.

As for the assembly, you didn't tell us which CPU/emulator are you using. There's more than one flavor of assembly out there.

Some types of assembly, notably Intel x86 and ARM, have built-in logic for index scaling for accessing integer arrays - the CPU will multiply the index (stored in a register) by 4 for you.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281