Consider the expression . Let be the minimum number of registers required by an optimal code generation (without any register spill) algorithm for a load/store architecture, in which
- only load and store instructions can have memory operands and
- arithmetic instructions can have only register or immediate operands.
The value of is _____________ .
View worked solution
Answer: 2
To find the minimum number of registers required for the expression on a load/store architecture, we can write an optimal sequence of instructions. In a load/store architecture, memory variables must be loaded into registers before performing operations, while constants can be used as immediate operands.
Let us evaluate the expression using registers and :
- Evaluate :
LD R1, b(R1 = b)LD R2, c(R2 = c)ADD R1, R1, R2(R1 = b + c; R2 is now free)DIV R1, R1, #3(R1 = (b + c) / 3, using immediate operand 3)LD R2, d(R2 = d)ADD R1, R1, R2(R1 = ((b + c) / 3) + d; R2 is now free)
- Evaluate :
LD R2, a(R2 = a; R1 still holds the right subexpression)SUB R2, R2, #1(R2 = a - 1, using immediate operand 1)
- Perform the final multiplication:
MUL R1, R2, R1(R1 = (a - 1) * (((b + c) / 3) + d))
Throughout the execution, we use at most 2 registers ( and ). Since adding two memory variables and requires both to be loaded into registers, we need at least 2 registers. Thus, the minimum number of registers required is .
Free practice · Your answers on this page are not saved.
