Consider the following expression and identify minimum number of registers required to implement the following expression : (a-b)+(e+(c-d))/f
Can anyone please give the theory or notes of prerequisites -, how to solve these questions.
The solution of the above problem is :-
Let us consider that variable a is stored in register R1 and variable b is stored in register R2.
(a-b) is performed by subtracting R2 from R1 and result is stored in R1 that means R1=R1-R2, after performing this operation R2 becomes free.
Let us consider that variable c is stored in R2 because R2 is free and variable d is stored in R3. (c-d) is performed by subtracting R3 from R2 and result is stored back into R2 that means R2=R2-R3,after performing this operation R3 becomes free. And variable e is stored in R3 because R3 is free.
(e+(c-d)) can be performed by adding R2 to R3 and result is again stored back into R2 ( since R2=R3+R2). Now R3 is free, store f into R3 and perform R2/R3 and store it into R2, R2 = R2/R3.
Finally (a-b)+(e+(c-d))/f can be performed by adding R1 to the R2 and result is stored back into R1 since R1=R1+R2. Number of registers used is only 3.