Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

Call me bored, but don't call me boring. -- Larry Wall in <199705101952.MAA00756@wall.org>


programming / comp.lang.asm.x86 / Memory adress in register, how to acces it indirectly (newbie question)

SubjectAuthor
* Memory adress in register, how to acces it indirectly (newbie questionNathan Dean
+* Re: Memory adress in register, how to acces it indirectly (newbie questionDick Wesseling
|`- Re: Memory adress in register, how to acces it indirectly (newbie questionKerr-Mudd,John
+- Re: Memory adress in register, how to acces it indirectly (newbie questionR.Wieser
`- Re: Memory adress in register, how to acces it indirectly (newbieRod Pemberton

1
Subject: Memory adress in register, how to acces it indirectly (newbie question)
From: Nathan Dean
Newsgroups: comp.lang.asm.x86
Organization: A noiseless patient Spider
Date: Tue, 29 Dec 2020 18:38 UTC
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: nagyonka...@nospicedham.gmail.com (Nathan Dean)
Newsgroups: comp.lang.asm.x86
Subject: Memory adress in register, how to acces it indirectly (newbie question)
Date: Tue, 29 Dec 2020 10:38:20 -0800 (PST)
Organization: A noiseless patient Spider
Lines: 9
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Injection-Date: Tue, 29 Dec 2020 18:38:21 +0000
Injection-Info: reader02.eternal-september.org; posting-host="fec1ec9b20e4bf77691fe895b22e3231";
logging-data="12270"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OREyxV0FqBp/zgOMZJXQxm/J3ljqo7sM="
User-Agent: G2/1.0
Cancel-Lock: sha1:SDqBZsfCBQ2aSxDhGk8V+8eYLsk=
View all headers
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!



Subject: Re: Memory adress in register, how to acces it indirectly (newbie question)
From: Dick Wesseling
Newsgroups: comp.lang.asm.x86
Organization: A noiseless patient Spider
Date: Wed, 30 Dec 2020 02:20 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: fre...@nospicedham.securityaudit.val.newsbank.net (Dick Wesseling)
Newsgroups: comp.lang.asm.x86
Subject: Re: Memory adress in register, how to acces it indirectly (newbie question)
Date: 30 Dec 2020 02:20:29 GMT
Organization: A noiseless patient Spider
Lines: 54
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <5febe3ed$0$296$e4fe514c@news.xs4all.nl>
References: <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="b982b8e7fbeb142b8f131d8adf692d07";
logging-data="26374"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180t6N2q5O6Bpg4OiAiLWVE6ZG3O72IA3U="
Cancel-Lock: sha1:3Zn3hv9gZh5kIy39L0o04wdCX1A=
View all headers
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.



Subject: Re: Memory adress in register, how to acces it indirectly (newbie question)
From: R.Wieser
Newsgroups: comp.lang.asm.x86
Organization: Aioe.org NNTP Server
Date: Wed, 30 Dec 2020 07:19 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: addr...@nospicedham.not.available (R.Wieser)
Newsgroups: comp.lang.asm.x86
Subject: Re: Memory adress in register, how to acces it indirectly (newbie question)
Date: Wed, 30 Dec 2020 08:19:32 +0100
Organization: Aioe.org NNTP Server
Lines: 19
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <rsh9mi$10lv$1@gioia.aioe.org>
References: <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com>
Injection-Info: reader02.eternal-september.org; posting-host="b982b8e7fbeb142b8f131d8adf692d07";
logging-data="24349"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18seqSzm3x/oltF7qwGToR2T7XQ0asxLwU="
Cancel-Lock: sha1:mThkCkSg557C6mag0rZH70qvRoc=
View all headers
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




Subject: Re: Memory adress in register, how to acces it indirectly (newbie question)
From: Kerr-Mudd,John
Newsgroups: comp.lang.asm.x86
Organization: dis
Date: Wed, 30 Dec 2020 09:41 UTC
References: 1 2
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: notsay...@nospicedham.127.0.0.1 (Kerr-Mudd,John)
Newsgroups: comp.lang.asm.x86
Subject: Re: Memory adress in register, how to acces it indirectly (newbie question)
Date: Wed, 30 Dec 2020 09:41:43 -0000 (UTC)
Organization: dis
Lines: 66
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <XnsACA362A0F8A65admin127001@144.76.35.252>
References: <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com> <5febe3ed$0$296$e4fe514c@news.xs4all.nl>
Injection-Info: reader02.eternal-september.org; posting-host="b982b8e7fbeb142b8f131d8adf692d07";
logging-data="8206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+e/ACsTM3InwUVT+YaPW3xyCGqTVMDFM4="
User-Agent: Xnews/2009.05.01
Cancel-Lock: sha1:okmtedS5yvpp6k4vUHfsTTqbr+k=
View all headers
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.



Subject: Re: Memory adress in register, how to acces it indirectly (newbie
From: Rod Pemberton
Newsgroups: comp.lang.asm.x86
Organization: Aioe.org NNTP Server
Date: Wed, 30 Dec 2020 10:19 UTC
References: 1
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: nomaili...@nospicedham.trgzxcqvbe.cpm (Rod Pemberton)
Newsgroups: comp.lang.asm.x86
Subject: Re: Memory adress in register, how to acces it indirectly (newbie
Date: Wed, 30 Dec 2020 05:19:22 -0500
Organization: Aioe.org NNTP Server
Lines: 27
Approved: fbkotler@myfairpoint.net - comp.lang.asm.x86 moderation team.
Message-ID: <rshk5i$kpo$2@gioia.aioe.org>
References: <78bd42c1-3bcb-4c91-b357-b3e5060cd327n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="b982b8e7fbeb142b8f131d8adf692d07";
logging-data="26801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SY/mbT0EIhQRliExrqyhTH82By/r8D1o="
Cancel-Lock: sha1:PMGEKZZ6iX/SGv3glcYQF2MnTwo=
View all headers
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 ...



1
rocksolid light 0.7.2
clearneti2ptor