Write a program to evaluate the arithmetic statement. P = ((X - Y + Z) * (A ^ B))/( C ^ D * E) By using (i) Two address instructions (ii) One address instructions (iii) Zero address instructions
Write a program to evaluate the arithmetic statement. P = ((X - Y + Z) * (A ^ B))/( C ^ D * E) By using (i) Two address instructions (ii) One address instructions (iii) Zero address instructions
The statement is
$$P = \frac{(X - Y + Z) \times (A ^ B)}{(C ^ D \times E)}$$
Here the caret is taken as a binary operator, written POW. So $A ^ B$ is the instruction POW. The same order of evaluation is used in all three programs: first $(X - Y + Z)$, then $A ^ B$, then the product of both. Then $C ^ D$, then multiply by $E$, and last the division.
(i) Two-address instructions. The first operand is both a source and the destination.
| Instruction | Meaning |
|---|---|
| MOV R1, X | R1 = X |
| SUB R1, Y | R1 = X - Y |
| ADD R1, Z | R1 = X - Y + Z |
| MOV R2, A | R2 = A |
| POW R2, B | R2 = A ^ B |
| MUL R1, R2 | R1 = (X - Y + Z) * (A ^ B) |
| MOV R2, C | R2 = C |
| POW R2, D | R2 = C ^ D |
| MUL R2, E | R2 = C ^ D * E |
| DIV R1, R2 | R1 = numerator / denominator |
| MOV P, R1 | P = R1 |
(ii) One-address instructions. Every instruction uses the accumulator AC as the other operand, and T1 and T2 are temporary memory locations.
| Instruction | Meaning |
|---|---|
| LOAD X | AC = X |
| SUB Y | AC = X - Y |
| ADD Z | AC = X - Y + Z |
| STORE T1 | T1 = AC |
| LOAD A | AC = A |
| POW B | AC = A ^ B |
| MUL T1 | AC = (A ^ B) * T1 |
| STORE T1 | T1 = numerator |
| LOAD C | AC = C |
| POW D | AC = C ^ D |
| MUL E | AC = C ^ D * E |
| STORE T2 | T2 = denominator |
| LOAD T1 | AC = numerator |
| DIV T2 | AC = numerator / denominator |
| STORE P | P = AC |
(iii) Zero-address instructions. These use a stack. PUSH puts a value on the stack. An operation takes the top two values, applies the operation, and pushes the result. For SUB and DIV, the lower element is the left operand. The expression in reverse Polish form is $X,Y - Z + A,B ^ \times C,D ^ E \times /$, and the program is:
| Instruction | Stack after it (top on right) |
|---|---|
| PUSH X | X |
| PUSH Y | X, Y |
| SUB | X-Y |
| PUSH Z | X-Y, Z |
| ADD | X-Y+Z |
| PUSH A | X-Y+Z, A |
| PUSH B | X-Y+Z, A, B |
| POW | X-Y+Z, A^B |
| MUL | N (numerator) |
| PUSH C | N, C |
| PUSH D | N, C, D |
| POW | N, C^D |
| PUSH E | N, C^D, E |
| MUL | N, C^D*E |
| DIV | N / (C^D*E) |
| POP P | stack empty |
The three programs use 11, 15 and 16 instructions. As the number of address fields drops, the program becomes longer, but each instruction becomes shorter.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.
Up nextWhat are the differences between hardwired and micro-programmed control unit?