Given is Load Store Architecture, that means we can access memory using Load and Store Instructions.
Key Idea:- Pick new register only when it is required.
We want to add c and d, and initially both are in memory, therefore copy these into registers.
- load $R1, c$ $(R1 \leftarrow c)$
- load $R2, d$ $(R2 \leftarrow d)$
(here no compensation can be done, we need two registers)
- add $R1, R1, R2 \ ( R1 \leftarrow R1 + R2)$
(at this point $R1$ is holding $\mathbf{c+d}$ and $R2$ is holding $\mathbf{d}$, i.e. $R1 \leftarrow \mathbf{c+d}$ and $R2 \leftarrow \mathbf{d})$
Now, $\mathbf{e}$ comes into picture and my question is, Can i make use of $R1$ or $R2$ to store $\mathbf{e}$?
I can not use R1 to store e as its value will be needed later but I can use R2.
(currently $R1 \leftarrow \mathbf{c+d}$ and $R2 \leftarrow \mathbf{e})$
- Sub $R1, R2, R1$ $(R1 \leftarrow R2 - R1)$
Doing this all gives, final value of right sub-tree is stored in $R1$, and $R2$ stores $\mathbf{e}$.
Now, coming to left subtree, to perform "$a-b$" we need to copy both variables in registers.
We can copy one of the variable in $R2$, but we can not obviously copy in $R1$ as value of $R1$ will be required later.
- Load $R2, a$
- Load $R3, b$ (here comes extra register, and we can not avoid using it.)
Current mapping is $R2 \leftarrow a$, $R3 \leftarrow b$ and $R1$ contains final value of Right subtree.
- SUB $R2, R2, R3 (R2 \leftarrow R2 - R3)$
- ADD $R1, R1 , R2$
Hence answer is $3$ i.e. D