Conner
Conner

Reputation: 677

Assembly Code Instructions

What does the operation

movl (%esi, %ecx,4), %eax do?

To my understanding it would store in %eax the equivalent of %ecx * 4 + %esi.

Upvotes: 1

Views: 151

Answers (2)

Sam
Sam

Reputation: 1912

Yes, you are write.
It is called as the index addressing mode.

It's syntax is:

<constant1/label> (%reg1, %reg2, constant2)

i.e. either constant1 or label without < & >.

%reg1 and %reg2 must be GPRs.
<constant1/label> is optional.
%reg1 is optional.

It results in: constant1 + %reg1 + %reg2 * constant2

Generally, constant1 or label and %reg1 are used for the base address of an array. And %reg2 & constant2 are used for the index.

For example:

Let's say, you have a global array:

.section .data
    .globl   arr
    .type    arr,    @object
    .size    arr,    20
    .align   4
arr:
    .long    10, 20, 30, 40, 50

Then you may write...

movl    $2,    %eax                # Move the index into %eax
movl    arr( , %eax, 4),    %edx

This means: %edx = arr + %eax * 4. i.e. Base address + 8 bytes.
You may think of it like %edx = arr[2]

Generally,

If you have a global array having a lable, then you use label for the base address.
If you have a local array, then you use the %reg2 fot the base address.
If you have a global structure containing array, then you use both label of the structure + %reg2 containing the byte-offset of the member array.

That's what happens generally... but it depends on the situation...

Upvotes: 1

Michael
Michael

Reputation: 58507

It's equivalent to the following in Intel syntax:

mov eax,[esi + ecx*4]

What it will do is read 32 bits from memory at the address formed by esi + ecx*4 and put that value in eax.

Upvotes: 3

Related Questions