I was wondering if I got this correctly:
As far as I know the syntax for relative addressing in x86 is like this:
base + index * scale + displacement
Now Igiven these assumptions:
int i //valuee in ecx
int arr[256] //adress in esi
I want to load the following into eax:
arr[i + 10]
My first guess was:
mov eax, dword ptr[esi + ecx*4 + 10*4]
But I am unsure about the second multiplication since it would not fit the syntax mentioned above.
Also: What if the datatype of the index and the array are different. e.g:
short arr[i + 10]
What happens to the multiplication?
its always easier to break it down a bit, following your code:
esiis a pointer toarrecxisisizeof(int)= 40so now we put it together:
mov eax, dword ptr[esi + ecx* 4 + 40]thus your statement is correct. the second multiplication should be folded away by your assembler to a constant 40, if it doesn’t then you’ll need to calculate it by hand, but you’d need to test that on your assembler (or compiler if this is for inline assembly).
Update
You’d need to adjust the constants to the new element size, registers won’t need to be changed though:
mov eax, dword ptr[esi + ecx * 2 + 20]if this is done as inline assembler, then you can use the
sizeofoperator, to make life simpler, and have it folded away, certain assemblers may also allow a SIZEOF macro.