Free2Code
 
Time: 2009-01-06, 12:35am
Base Address + Index * Scale + Displacement
Subject: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-21, 09:28am
Rank: ? (37)
Member #: 26960
Code:
  1. mov eax, [ebx+ecx*4+32]


This is a code included with a tutorial in MASM32 package. I know why the square bracket is there. My problem is that how to do it the other way round. I can't do this:

Code:
  1. mov ebx+ecx*4+32, eax


So how do I move the value the other way round?
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-22, 03:14am
Rank: ? (4821)
Member #: 3416
the brackets mean that you want to use the value inside them as a memory address -- you should be able to use the exact same syntax (meaning with the brackets) on either side of the mov, at least when the other side is simply a register.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-27, 03:12am
Rank: ? (37)
Member #: 26960
I asked that because I am not sure how to address variables when I am in arrays.

Another question is that I awant to ask if the following is the same.

Code:
  1. var_name dd ?
  2. var_name_a dd ?
  3. var_name_b dd ?
  4. var_name_c dd ?
  5. var_name_d dd ?


Code:
  1. var_name dd ?,?,?,?,?


Also, is the only way to address the variables if it is an array is by the "Base Address + Index * Scale + Displacement" thing?

So to move values into it:
Code:
  1. mov [ebx+ecx*4+32], eax ; where ebx is the variable address


I remember getting an invalid use of register warning, but may be I did something else wrong.

Christopher
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-27, 06:02am
Rank: ? (119)
Member #: 28292
1. Yes, it is the same.
2. It is the common way of addressing. EBX is a pointer to the begin of an array but it could be an other register.
lea ebx, var_name
ECX is index of an element of an array and the operand 4 is the type of the array in your case it is double word (integer).1 if a byte and 2 if a word. E.g the third element of your array could be addressed as:
mov ecx, 2 ; 2 because the numeration of an array begins from 0
mov eax, [ebx + ecx * 4]
The displacement usually isn't used. It is used only if you want to become a some part of an element of an array.
E.g I want to get a high word of the third element
mov ax, ptr word [ebx + ecx * 4 + 2]

To the question above. It false to do so: mov ebx+ecx*4+32, eax
mov can move your data from a register to an other register (or to the same register) or from a memory to register (or from register to memory).
This: "ebx+ecx*4+32" is only an expression. You can't move you data to a expression. You can move your data to a memory addressed with this expression.

But there are no restrictions. You may address it so how you want.

» Post edited 2006-09-27, 09:24am by igorok.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-27, 06:14am
Rank: ? (119)
Member #: 28292
In addition I just wanna say. In c++ the arrays placed in stack are addressed in such way:
ss:[ebp - ecx * type - displacement in stack]
ebp is poiner to begin of the buffer (part of stack) which was allocated for the function.
type is a type of an array (char = 1, short = 2, integer = 4, long = 8).
displacement in stack is begin of an array

» Post edited 2006-09-27, 09:22am by igorok.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-29, 08:44am
Rank: ? (37)
Member #: 26960
Thanks. That helped.

In assembly, it's BYTE = 1, WORD = 2, DWORD = 4, QWORD = 8, I think.

In the windows.inc, LONG is defined as DWORD, just like INT.

Boucly.
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-09-29, 07:12pm
Rank: ? (119)
Member #: 28292
Because as type LONG was created there were only 32 bit processors. Thats way INT = LONG = 4. But nowadays 64 bit processors exist (AMD 64) LONG should be 8 bytes.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-01, 12:11am
Rank: ? (37)
Member #: 26960
Okay. Then a LONGLOG should be 128 bytes then, a bit too big to be useful if you ask me.

Anyway, back to topic

 misterhaan writes...
you should be able to use the exact same syntax (meaning with the brackets) on either side of the mov, at least when the other side is simply a register.


Why does it only work when it is only a register, does an immediate data work. I know a memory data wouldn't since that would be moving memory to memory.

Code:
  1. mov    ecx, 100
  2. mov    ebx, offset point_array
  3. mov    [ebx+12], ecx


would it be easier and faster if I push and pop

Code:
  1. push    100
  2. pop    [ebx+12]


or it is illegal referencing again?

Christopher
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-01, 09:42am
Rank: ? (119)
Member #: 28292
mov dword ptr point_array + 12, 100

 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-02, 02:56am
Rank: ? (4821)
Member #: 3416
Why does it only work when it is only a register, does an immediate data work.

you'd have to try it -- most of my intel assembly programming was for the 8086, where you pretty much always had to go to or from a register.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-04, 04:53am
Rank: ? (37)
Member #: 26960
Okay, I tried it and igorok's example (mov dword ptr point_array + 12, 100) worked and

mov ebx, offset point_array
mov [ebx+12], 100

or

mov [point_array+12], 100

doesn't work. Is there a difference between them?

Boucly
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-04, 06:49am
Rank: ? (4821)
Member #: 3416
mov [point_array+12], 100

you didn't say offset point_array, so it's using the value of point_array here rather than the address. i don't see any real difference between the first two.

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-04, 07:50am
Rank: ? (119)
Member #: 28292
dword ptr is the difference. You can put 100 in [point_array+12] as a byte or as a word, or as a dword. Compiler doesn't know what it should do. But if there is a 32 bit register used as a operand (mov [ebx + 12], ecx) than compiler converts them automatic to mov dword ptr [ebx + 12], ecx.
This is a reason why AT & T syntax has some advantages.

» Post edited 2006-10-04, 08:23am by igorok.

 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-06, 11:32pm
Rank: ? (37)
Member #: 26960
Thanks, igorok and misterhaan, both of you.

Let see if I got this:

ebx : the location of the register
[ebx] : the location of the value inside ebx
[ebx + 12] : 12 bytes more than the location pointed by ebx

Actually I don't get it, what next?

ptr [ebx + 12] : ?

and What's AT & T?
 
  Reply to this ·  Post link ·  Top
Subject: Re: Base Address + Index * Scale + Displacement  ·  Posted: 2006-10-07, 01:00am
Rank: ? (119)
Member #: 28292
ptr - is only operator. This operator difines how to access a operand. An operand could be accessed in many ways. The descriptions of accessing are byte, word, dword etc. This description you write before the operator ptr.
Let see follow.
you have a variable var dd 0AABBCCDDh
The dump of them looks like this:
DD CC BB AA
after mov byte ptr var, 64h the dump looks like this:
64 CC BB AA
after mov word ptr var, 64h
64 00 BB AA
after mov dword ptr var, 64h
64 00 00 00.

The follow example is correct only for Intel-like architecture or other (little Indian)

AT & T syntax is used usually in assembly languages for Unix-like operation systems.

» Post edited 2006-10-07, 01:15am by igorok.

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons