mov vs lea

We give a simple explanation of why the lea command is used in compliers over mov when performing computation.

2 November 2025

The one sentence summary is function evaluation (dereferencing) commutes with algebra, so don't evaluate (dereference) until the end.

We have two separate units in a standard computation model, the data storage (RAM) and the computation (CPU). Transferring data between these two units is expensive, so it should be minimized. Also, the CPU is stupid, so it can only take in primitive instructions.

Suppose we're interested in evaluating the polynomial

p(x):=5xx\begin{equation} p(x):= 5x-x \end{equation}

for various values of xx. Hold off for the moment with identifying pp as 4x4x. The stupidity of the CPU requires that all arithmetic operations are binary operations, so write pp as the composition

p(x)=sub(q(x),x),\begin{equation} p(x)=\text{sub}(q(x),x), \end{equation}

where

q:x5xsub:x,yxy\begin{equation} \begin{split} q&: x \mapsto 5x\\ \text{sub}&: x,y \mapsto x-y\\ \end{split} \end{equation}

There are two ways to view the composition expression (2). First, we can view both q(x)q(x) and sub(q(x),x)\text{sub}(q(x),x) as numbers for a given number x. This leads us to using mov, which pulls xx from memory and feeds it into the computational machine q(x)q(x), then pulls xx again to feed it into the computational machine sub(q(x),x)\text{sub}(q(x),x). Notice that we've retrieved xx twice.

The alternative, more powerful viewpoint is to view xx as a symbol as you would in basic algebra. This in turn leads us to interpret q(x)q(x) and sub(q(x),x)\text{sub}(q(x),x) not as numbers but as symbols as well. The power here is that you can perform symbolic simplification, without ever evaluating. This is just enunciating the kneejerk evaluation of simplifying (2) to 4x4x without ever specifying a value for xx. Taking this viewpoint, we see that only one retrieval of xx is necessary. This is the idea behind lea.