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

    From Nathan Dean@21:1/5 to All on Tue Dec 29 10:38:20 2020
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dick Wesseling@21:1/5 to Nathan Dean on Wed Dec 30 02:20:29 2020
    In article <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com>,
    Nathan Dean <nagyonkamuprofil@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Wed Dec 30 08:19:32 2020
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kerr-Mudd,John@21:1/5 to Dick Wesseling on Wed Dec 30 09:41:43 2020
    On Wed, 30 Dec 2020 02:20:29 GMT, free@nospicedham.securityaudit.val.newsbank.net (Dick Wesseling) wrote:

    In article <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com>,
    Nathan Dean <nagyonkamuprofil@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.



    As a retro guy, I'd use SI for the arraybase & BX for the offset

    --
    Bah, and indeed, Humbug.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rod Pemberton@21:1/5 to Nathan Dean on Wed Dec 30 05:19:22 2020
    On Tue, 29 Dec 2020 10:38:20 -0800 (PST)
    Nathan Dean <nagyonkamuprofil@nospicedham.gmail.com> wrote:

    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)


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)