# Two basic questions about generated assembly

• 07-16-2008
George2
Two basic questions about generated assembly
Hello everyone,

http://www.microsoft.com/msj/0298/hood0298.aspx

1.

why using LEA to do multiplication is faster than using MUL?

"Using "LEA EAX,[EAX*4+EAX]" turns out to be faster than the MUL instruction."

2.

"The TEB's linear address can be found at offset 0x18 in the TEB." -- what means linear address? Something like array, which elements are put next to each other? What means non-linear address?

George
• 07-16-2008
Cactus_Hugger
for #1:
LEA can only be used for some multiplications. LEA is only capable of multiplying by a constant, and that constant must be 1, 2, 4, or 8. These are all powers of two, and so can be done by bitshifting, which is easier than straight up multiplying. Additionally, you can add the register (as shown in your example), so you get:
*1, *2, *3, *4, *5, *8, and *9 with LEA.

Straight from the manual (AMD's version, the page(s) on the LEA instruction):
Quote:

The LEA instruction has a limited capability to perform multiplication of operands in general-purpose
registers using scaled-index addressing. For example:
Code:

`lea eax, [ebx+ebx*8]`
loads the value of the EBX register, multiplied by 9, into the EAX register. Possible values of
multipliers are 2, 4, 8, 3, 5, and 9.
• 07-16-2008
matsp
And the fact is that LEA is fast because it works on multiples of 2, so it's a simple shift of 0, 1, 2 or 3 bits on one of the inputs and an optional base to add the original value (leading to the 3, 5, 9 multipliers). This can be done by the hardware in a single cycle or perhaps 2 cycles on older (486-Pentium I generation) processors.

Multiply, on the other hand, will take a few cycles (without looking it up, I'd say about 5-10 on a modern processor, and around 20-30 on a 486 or Pentium) to perform a 32 by 32 bit multiply using generic methods of repeating addiition.

"Linear address" is the same as a virtual address in most cases. A non-linear address is the physical location (where it may well be that the next 4KB section in linear space is physically located 3.999GB away from the current 4KB section).

--
Mats