Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does lea instruction get the operand's address? [duplicate]

Tags:

x86

assembly

How does this work exactly? I know that lea is efficient compared to using add/mov instructions because it doesn't go through the ALU or set any flags. So how is lea getting its addresses? What makes it better than add/mov?

like image 901
Instinct Avatar asked Dec 06 '25 04:12

Instinct


2 Answers

The idea that lea doesn't go through the ALU is outdated and has been wrong for over a decade. It goes through one of the ALUs - of which there will be several, and that is extremely unlikely to be a bottleneck. It actually takes time. It's not faster than an add.

But that doesn't mean it isn't useful. Unlike add, it can have a destination that differs from both inputs, so it can save a mov, and it can take an extra constant so you can do two adds and a mov all in one. The scaling is also nice, and all combined you can do something like a = b * 9 + 5 all in one lea a, [b + b * 8 + 5]. Such "complex" forms of lea are often slower than than simpler 2-operand forms of lea.

like image 148
harold Avatar answered Dec 07 '25 19:12

harold


The LEA instruction uses the effective address (EA) part of the CPU to do certain kinds of arithmetic without using the ALU. The EA module can do very specific kinds of arithmetic that is used for address calculation, but can also be used for other things if what you need is one of the things it specifically provides.

By not using the ALU for this EA calculation, the ALU can be busy doing something else at the same time and you can avoid a pipeline stall.

like image 31
Greg Hewgill Avatar answered Dec 07 '25 21:12

Greg Hewgill