LEA a power commandAssembler/8086

This gem shows various ways to use LEA. LEA is very usefull, not only can you make fast multiplies with like like:

        lea     ebx,[ebx+ebx*4] ; ebx:=ebx*5
Then, is it the fastest way in real mode, when upperword of EBX is useless? No. The machine code will be: 66,67,8d,1c,9b. As one can see, it contains two prefixes: 66 and 67. Both the operand and the address-size prefix. What happens if the first one is missing, like
        lea     bx,[ebx+ebx*4]  ; (67,8d,1c,9b)
You can also use it to replace
        movzx   eax,si          ; or similar
just type:
        lea     eax,[si]
It is shorter and quicker in both 16 and 32-bit code. The only drawback is that only a few register combinations fit between the brackets: [BX], [BP], [SI], [DI], [BX/BP+SI/DI+immediate].

You can add and subract with LEA, too. Consider this:

        lea     eax,[eax+12]    ; replaces add  eax,12
This is smaller than ADD in 32-bit code and does not corrupt your flags.
You can do even more, you can move with LEA:
        mov     eax,ebx
can be preplaced by:
        lea     eax,[ebx]
And if you like to be really tricky you can do many combinations with immediates, index and base registers too. Like this:
        mov     eax,[ebx*2]
can be replaced with the smaller
        mov     eax,[ebx+ebx]
The first is 7 bytes, the second 3. Please note that the risk for penalties is high, especially on the Pentium and above processors.
Gem writer: Ervin Toth
last updated: 1998-03-16