To solve the expression A+B + (C*D+E) using different instruction formats,
Zero-address instruction format (Stack-based)
This format uses a Last-In, First-Out (LIFO) stack to hold operands implicitly. All
operations are performed on the topmost elements of the stack.
Expression in Postfix notation: A B + C D * E + +
Instructions:
o PUSH A // Push A onto the stack. Stack: [A]
o PUSH B // Push B onto the stack. Stack: [A, B]
o ADD // Pop B and A, compute A+B , push result. Stack: [A+B]
o PUSH C // Push C onto the stack. Stack: [A+B, C]
o PUSH D // Push D onto the stack. Stack: [A+B, C, D]
o MUL // Pop D and C, compute C*D , push result. Stack: [A+B, C*D]
o PUSH E // Push E onto the stack. Stack: [A+B, C*D, E]
o ADD // Pop E and C*D , compute C*D+E , push result. Stack: [A+B,
zz
o ADD // Pop C*D+E and A+B , compute A+B + (C*D+E) , push
result. Stack: [A+B+(C*D+E)]
o POP Result // Pop the final result and store it in a memory location
named Result. Stack: []
One-address instruction format (Accumulator-based)
This format uses a special register called the Accumulator (AC) to store one of the
operands and the result of the operation.
Instructions:
o LOAD C // Load the value of C into the Accumulator. AC = C
o MUL D // Multiply the Accumulator by D. AC = C * D
o ADD E // Add E to the Accumulator. AC = C * D + E
o STORE Temp // Store the Accumulator's content into a temporary
memory location. Temp = C * D + E
o LOAD A // Load the value of A into the Accumulator. AC = A
o ADD B // Add B to the Accumulator. AC = A + B
o ADD Temp // Add the content of the temporary memory location to the
Accumulator. AC = (A + B) + Temp
o STORE Result // Store the Accumulator's content into the final
memory location. Result = A+B + (C*D+E)
Two-address instruction format (Register-based)
This format allows two operands to be specified. The first operand also serves as the
destination for the result. We will use registers R1 and R2.
Instructions:
o MOV R1, C // Move the value of C into register R1. R1 = C
o MUL R1, D // Multiply the contents of R1 by D, store result in R1. R1 =
C * D
o ADD R1, E // Add E to R1, store result in R1. R1 = C * D + E
o MOV R2, A // Move the value of A into register R2. R2 = A
o ADD R2, B // Add B to R2, store result in R2. R2 = A + B
o ADD R2, R1 // Add the contents of R1 to R2, store result in R2. R2 =
(A + B) + (C * D + E)
o MOV Result, R2 // Move the contents of R2 to the final memory
location. Result = A+B + (C*D+E)
Three-address instruction format (Register-based)
This format specifies three addresses in each instruction: two source operands and
one destination. This makes for shorter programs, as each instruction can perform a
complete operation.
Instructions:
o MUL R1, C, D // Multiply C and D, store the result in register R1. R1 =
C * D
o ADD R1, R1, E // Add R1 and E, store the result in R1. R1 = (C * D)
+ E
o ADD R2, A, B // Add A and B, store the result in register R2. R2 = A
+ B
o ADD Result, R2, R1 // Add R2 and R1, store the result in the memory
location named Result. Result = (A + B) + (C * D + E)
Here is a table listing common addressing modes:
Addressing Description
Mode
Implied The operand's location is implicitly defined by the instruction itself, with no explicit address
needed.
Immediate The operand's actual value is directly contained within the instruction.
Register The operand is located in a CPU register.
Register Indirect The instruction contains the address of the operand, which is stored in a register.
Direct The operand's memory address is directly specified in the instruction.
Indirect The operand's address is stored at a location specified by the instruction.
Indexed The operand's address is found by adding a fixed offset to the contents of an index register.
Auto-Increment The operand's address is in a register, and the register is automatically incremented after the
data is accessed.
Auto-Decrement The operand's address is in a register, and the register is automatically decremented before the
data is accessed.
Displacement The operand's address is found by adding the contents of a register to a value from the
instruction.
Relative A form of displacement where the address is calculated relative to the program counter.
s