Why can assembly instructions contain multiplications in the “lea” instruction?

前端 未结 3 1262
醉酒成梦
醉酒成梦 2021-02-05 18:58

I am working on a very low level part of the application in which performance is critical.

While investigating the generated assembly, I noticed the following instructio

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-05 19:38

    To expand on your last question:

    Is the multiplication limited to powers of 2 (I would assume this is the case)?

    Note that you get the result of base + scale * index, so while scale has to be 1, 2, 4 or 8 (the size of x86 integer datatypes), you can get the equivalent of a multiplication by some different constants by using the same register as base and index, e.g.:

    lea eax, [eax*4 + eax]   ; multiply by 5
    

    This is used by the compiler to do strength reduction, e.g: for a multiplication by 100, depending on compiler options (target CPU model, optimization options), you may get:

    lea    (%edx,%edx,4),%eax   ; eax = orig_edx * 5
    lea    (%eax,%eax,4),%eax   ; eax = eax * 5 = orig_edx * 25
    shl    $0x2,%eax            ; eax = eax * 4 = orig_edx * 100
    

提交回复
热议问题