Discussion:
Memory adress in register, how to acces it indirectly (newbie question)
Nathan Dean
2020-12-29 18:38:20 UTC
Hi,
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:

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

Any help would be appreciated!
Dick Wesseling
2020-12-30 02:20:29 UTC
Post by Nathan Dean
I'm pretty new to assembly and I'm writing my homework. I can't find a
This is a good place to ask. However, since this is homework, I won't
give the full solution, just a few hints.
Post by Nathan Dean
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
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
mov [ecx + 4*1], eax ; array
mov [ecx + 4*2], eax ; array
....
mov [ecx + 4*8], eax ; array (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.
Kerr-Mudd,John
2020-12-30 09:41:43 UTC
On Wed, 30 Dec 2020 02:20:29 GMT,
Post by Dick Wesseling
Post by Nathan Dean
I'm pretty new to assembly and I'm writing my homework. I can't find a
This is a good place to ask. However, since this is homework, I won't
give the full solution, just a few hints.
Post by Nathan Dean
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
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
lea ecx,array...
mov eax,somevalue
mov [ecx + 4*0], eax ; array
mov [ecx + 4*1], eax ; array
mov [ecx + 4*2], eax ; array
....
mov [ecx + 4*8], eax ; array (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
[ 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.
As a retro guy, I'd use SI for the arraybase & BX for the offset
--
Bah, and indeed, Humbug.
R.Wieser
2020-12-30 07:19:32 UTC
Nathan,
Post by Nathan Dean
MOV [ecx + 4*8], eax
And you are. :-)
Post by Nathan Dean
but in this case, the program takes the memory adress of
the ecx, and adds to that
... and than stores EAX at that final address (and as EAX contains four
bytes, the three bytes after it too)

Regards,
Rudy Wieser
Rod Pemberton
2020-12-30 10:19:22 UTC
On Tue, 29 Dec 2020 10:38:20 -0800 (PST)
Post by Nathan Dean
I'm pretty new to assembly and I'm writing my homework. I can't find
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? (Also if you could reference something