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*5Then, 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, likelea bx,[ebx+ebx*4] ; (67,8d,1c,9b)
EBX
isn't changed
movzx eax,si ; or similarjust 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,12This is smaller than
ADD
in 32-bit code and does not corrupt your flags.LEA
:mov eax,ebxcan 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.