In article <
78bd42c1-3bcb-4c91...@googlegroups.com>,
Nathan Dean <
nagyonka...@nospicedham.gmail.com> writes:
> I'm pretty new to assembly and I'm writing my homework. I can't find a
> solution to my problem, so I thought I'd ask here:
This is a good place to ask. However, since this is homework, I won't
give the full solution, just a few hints.
> I have a memory adress in one of my registers (ecx in this case), and
> that memory adress points to an array. I would like to acces that array
> by using something like this:
>
> MOV [ecx + 4*8], eax
> but in this case, the program takes the memory adress of the ecx, and
> adds to that. How could I solve this?
You probably dont't want to "access the arrray". My guess is that you
want to access *an element of* the array. And that is exactly what your
code does.
The only problem with your code code is that it always access the same
element of the array, see below.
Your code suggests that the stride of the array is either 4 or 8 (stride
is the difference between two consecutive array element addresses).
Assuming a stride of 4, one can store into the elements of the array as
follows:
lea ecx,array...
mov eax,somevalue
mov [ecx + 4*0], eax ; array[0]
mov [ecx + 4*1], eax ; array[1]
mov [ecx + 4*2], eax ; array[2]
....
mov [ecx + 4*8], eax ; array[8] (your code)
The problem with the code above is that it uses hard-wired constants as
array indices. A more general solution would use registers for both
the array (here ecx) and the index. This is where the following
base plus scaled index addressing modes come in handy:
[ reg32 + eax*n ]
[ reg32 + ebx*n ]
[ reg32 + ecx*n ]
[ reg32 + edx*n ]
[ reg32 + ebp*n ]
[ reg32 + esi*n ]
[ reg32 + edi*n ]
Here reg32 contains the array address (ecx in your code) and the other
register contains the array index.
n is the stride, it can be one of 1, 2, 4 or 8.