-5

lea assembly instruction

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).

Community
  • 1
  • 1
andrew01239
  • 11
  • 1
  • 2
  • 4
    The question you linked already explains it. It's just `edx+edx*4`which is of course `edx*5` (you know, `1+4=5`) – Jester May 15 '16 at 15:27
  • @Jester can u explain step by step how `edx+edx*4` become `edx*5` (how 4 becomes 5 )? – andrew01239 May 15 '16 at 18:10
  • 1
    That's elementary school maths ...if you get 1 apple and 4 more apples you will get 5 apples ... what's unclear? – Jester May 15 '16 at 18:13
  • @Jester I know but its written format is confusing ... can you please explain these as example `nums dd 1,2,3,4 ` – andrew01239 May 15 '16 at 18:49
  • `lea` does not access memory, it only does `eax = edx * 5`. It does not need `nums dd` and it doesn't use any of the numbers there. – Jester May 15 '16 at 18:50
  • how come a + a*4 not equal to 5*a? use Intel format: `lea eax, [edx+edx*4]`, it's much less confusing. – phuclv May 18 '16 at 06:23
  • Possible duplicate of [lea assembly instruction](https://stackoverflow.com/questions/9153282/lea-assembly-instruction) – phuclv Aug 09 '18 at 01:50

2 Answers2

3

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.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 2
    I like to explain `lea` by saying it's a shift-and-add instruction that just happens to use the addressing-mode syntax and machine-code encoding. – Peter Cordes May 15 '16 at 15:54
2

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.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • can you explain it with example. I mean with assigning numbers to %edx – andrew01239 May 15 '16 at 15:16
  • `(%edx, %edx, 4) ` first `%edx` is base address i.e. any adress in memory address space , second `%edx` is index i.e. anything of 0,1,2..... and scale is `4` then how it is interpreted as `%edx * 5`? – andrew01239 May 15 '16 at 15:21
  • 1
    No, both `%edx` are the same, just numbers (which could be a memory address but are most likely not in this case). So you have the base `%edx` and you are adding the index `%edx` with scale `4`, i.e. you are adding the index `%edx*4` to the base `%edx`, which makes `%edx+%edx*4` which is, using simple math, `%edx*(1+4)`, which makes `%edx*5`. With numbers: Assuming `%edx == 10` then you have `%edx := %edx + %edx * 4`, `%edx := 10 + 10 * 4`, `%edx := 10 + 40`, `%edx := 50`. – CherryDT May 17 '16 at 12:43