No - I actually did mean 7 bytes for "lea (,%eax,2),%eax" when assembled to 32 bit by GAS, as this is exactly what GAS generated (as posted previously):DennisCGc wrote:I think you meant "Yes - if I assembled to 32 bit it'd be 3 bytes instead of 5."
[tt]8d 04 45 00 00 00 00 lea 0x0(,%eax,2),%eax[/tt]
This is:
[tt] 0x8D = LEA
0x04 = ModR/M byte = [Mod = 0, nnn = 0, R/M = 4] = EAX is destination and SIB byte needed
0x45 = SIB byte = [Scaled Index = 0, Scale = 0, Index = 0] = EAX * 2 + disp32
0x00, 0x00, 0x00, 0x00 = 32 bit displacement[/tt]
When a SIB byte is present (which is needed for the scale *2) it's impossible to avoid a displacement (or to have an 8 bit displacement) without using a base register.
NASM is better at optimizing than GAS, but it still isn't the best. The shortest/fastest possible encoding for "lea (,%eax,2),%eax" would be" 0x01, 0xC0" but I don't think NASM developers want this level of optimization...
Cheers,
Brendan