at above page it says
The instruction leal (%edx, %edx, 4), %eax
means:eax = edx * 5
but how ?
as i know %edx
is base next %edx
is index and 4 is scale
i.e. disp(base,index,scale).
at above page it says
The instruction leal (%edx, %edx, 4), %eax
means:eax = edx * 5
but how ?
as i know %edx
is base next %edx
is index and 4 is scale
i.e. disp(base,index,scale).
disp(base,index,scale).
You know how this instruction works (base + index * scale) but your troubles come from the fact that you keep considering the first component to always be an address ("i.e. any adress in memory address space") and the second component to always be an index ("i.e. anything of 0,1,2.....").
To the CPU that executes this instruction the contents of %edx
can have any meaning you choose! It's always just a number and that's precisely why we use instructions like leal (%edx, %edx, 4), %eax
to calculate %eax = %edx * 5
.
In general, disp(base,index,scale)
gives address:
base + scale * index + disp
therefore, address (%edx, %edx, 4)
is interpreted as
disp := 0
base := %edx
index := %edx
disp := 4
In together, it gives:
%edx + %edx * 4 + 0
which gives %edx * 5
.
Now, leal
is used for address assignment. It assigns the above address to %eax
, hence you get %eax := %edx * 5
.
This is commonly seen, because the scaling factor scale
can only be 1, 2, 4, 8, hence there is no way to multiply %edx by 5 directly. Similarly, if want to get %eax = %edx * 3
, we have to do leal (%edx, %edx, 2), %eax
.