Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Memory adress in register, how to acces it indirectly (newbie question)

9 views
Skip to first unread message

Nathan Dean

unread,
Dec 29, 2020, 4:12:39 PM12/29/20
to
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
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 to read about this, that would be helpful also)

Any help would be appreciated!

Dick Wesseling

unread,
Dec 29, 2020, 9:30:19 PM12/29/20
to
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.

R.Wieser

unread,
Dec 30, 2020, 2:31:01 AM12/30/20
to
Nathan,

> I would like to acces that array by using something like this:
> MOV [ecx + 4*8], eax

And you are. :-)

> 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)

Ask your self : What do you think it does instead ?

Regards,
Rudy Wieser


Kerr-Mudd,John

unread,
Dec 30, 2020, 4:46:24 AM12/30/20
to
As a retro guy, I'd use SI for the arraybase & BX for the offset

--
Bah, and indeed, Humbug.

Rod Pemberton

unread,
Dec 30, 2020, 5:31:53 AM12/30/20
to
x86 has a special instruction that simplifies computing addressing of
array elements. It's called LEA. It's "just like" using MOV, but LEA
works with the address, instead of retrieving the value. See here for
an example of the two:

https://stackoverflow.com/questions/1658294/whats-the-purpose-of-the-lea-instruction

BTW, we don't do homework. To truly learn something, you have to do it
yourself. You have to put in the time. There is no other way.

--
Sigh ...

0 new messages