In: Computer Science
1.) Multiple Choice: Choose the correct letter choice. What is the correct sequence of instructions for carrying out the calculation (A * B) - (A * C) on Stack Architecture.
Assume that:
A.)
Push A Push C MUL Pop D Push B MUL Push D SUB Pop D |
B.)
Load R1 A MUL R2 R1 B MUL R3 R1 C Store R3 D SUB R2 R2 D Store R2 |
C.) Load R1 A Load R2 B MUL R2 R1 MUL R3 R1 SUB R2 R3 Store R2 D |
D.)
Load A MUL C Store D Load A Mul B Sub D Store D |
E.)
Load R1 A Load R2 B MUL R2 R1 Load R3 C MUL R3 R1 SUB R2 R3 Store R2 D |
F.)
Push A Push C MUL Push B MUL Push D SUB Pop D |
Answer : OPTION D is correct . We will derive our answer as follows :
INSTRUCTION SET ARCHITECTURE :
There are three most common form of instruction set architecture :
1. STACK - The operands are implicitly on the Top of the stack .
2. ACCUMULATOR - One operator is implicitly present in the accumulator .
3. GENERAL PURPOSE REGISTER : All operands are explicitly mentioned , they are either register or memory locations .
Small Example of assembly code : C= A+B
Stack | Accumulator | General purpose register |
PUSH A PUSH B ADD POP C |
LOAD A ADD B STORE C |
LOAD R1 , A ADD R1 , B STORE R1 , C |
4. RISC (Reduced Instruction Set computer) ARCHITECTURE :
The only memory access is through explicit LOAD / STORE .
LOAD R1 , A
LOAD R2 , B
ADD R3 , R1 , R2
STORE C , R3
The correct sequence of instruction to Calculate (A*B) - (A*C) on stack architecture is :
( A*B ) - ( A*C )
Option A :
PUSH A
PUSH C
MUL ( correct instuction till this point)
POP D ( no D is present here , so what to POP ? )
thus , this instruction set fails from this point , HENCE , INCORRECT OPTION
Option B :
LOAD R1 , A R1 <-- A (loaded A In register R1)
MUL R2 R1 B R2 <-- R1 * B (R1 is multiplied with B and stired in R2) means (A * B) = R2
MUL R3 R1 C R3 <-- R1 *C means ( A * C )
Store R3 D(incorrect) R3 <-- D (memory D is stored in R3 , BUT , the correct should be , STORE D R3 )
HENCE , INCORRECT OPTION
Option C :
LOAD R1 A R1<-- A
LOAD R2 B R2<--B
MUL R2 R1 R2 <-- R2 * R1 , means , register R2 contains (A*B)
MUL R3 R1 R3 <-- R3 * R1 , R3 is empty and R1 contains A , we want C here to be multiplied with A , but we are not getting it , so this instruction becomes incoorect at this point
HENCE , INCORRECT OPTION.
Option D :
LOAD A ACC <-- A (A is loaded in accumulator)
MUL C ACC <-- ACC*C means (A*C) (C is multiplied by the value loaded in accumulator)
STORE D D <-- ACC (A*C) (the result loaded in accumulator is stored to memory location D )
LOAD A ACC <-- A
MUL B ACC <-- A*B
SUB D ACC <-- ACC - D , means , accumulator contains (A*B) and D contains (A*C)
this instruction will result in , (A*B) - (A*C)
HENCE, CORRECT OPTION
Option E :
LOAD R1 A R1<-- A
LOAD R2 B R2<-- B
MUL R2 R1 R2 <-- R1*R2 , i.e , (A*B)
LOAD R3 C R3 <-- C
MUL R3 R1 R3 <-- R3*R1 i.e (A*C)
SUB R2 R3 R2 <-- R2 - R3 i.e . (A*B) - (A*C)
STORE R2 D R2 <-- D means , D is to be stored in R2 . BUT , here memory location D is not used so it may contain no result or some default value . Thus we well not get our desired answer .
The correct instruction should be : STORE D R2 i.e. D <-- R2
HENCE , INCORRECT OPTION
Option F :
PUSH A STACK<--A
PUSH C STACK <-- C
MUL STACK <-- A*C
PUSH B STACK <-- B
MUL STACK <-- A*C*B (this instuction becomes incorrect here )
our aim is not to calculate A*B*C , HENCE , INCORRECT OPTION