Consider the following program segment. Here $\text{R1, R2}$ and $\text{R3}$ are the general purpose registers.
$$\begin{array}{|l|l|l|c|} \hline & \text {Instruction} & \text{Operation }& \text{Instruction Size} \\ & & & \text{(no. of words)} \\\hline & \text{MOV R1,(3000)} & \text{R1} \leftarrow \text{M[3000]} & \text{$2$} \\\hline \text{LOOP:}& \text{MOV R2,(R3)} & \text{R2} \leftarrow \text{M[R3]} & \text{$1$} \\\hline & \text{ADD R2,R1} & \text{R2} \leftarrow \text{R1 + R2} & \text{$1$} \\\hline & \text{MOV (R3),R2} & \text{M[R3]} \leftarrow \text{R2} & \text{$1$} \\\hline& \text{INC R3} & \text{R3} \leftarrow \text{R3 + 1} & \text{$1$} \\\hline & \text{DEC R1} & \text{R1} \leftarrow \text{R1 – 1} & \text{$1$} \\\hline& \text{BNZ LOOP} & \text{Branch on not zero} & \text{$2$} \\\hline & \text{HALT} & \text{Stop} & \text{$1$} \\\hline\end{array}$$

Assume that the content of memory location $3000$ is $10$ and the content of the register $\text{R3}$ is $2000$. The content of each of the memory locations from $2000$ to $2010$ is $100$. The program is loaded from the memory location $1000$. All the numbers are in decimal.

Assume that the memory is word addressable. The number of memory references for accessing the data in executing the program completely is

@Abhineet Singh each ALU instruction changes flag value, conditional branches checks only the flags , so wihchever the last alu instruction that changed the flag will be considered . For eg.

  • X : ADD r1,r2;
  • Mov r2,M[5000];
  • Dec r2;
  • Dec r3
  • Mov M[5000],r3;
  • BNZ X;
  • So Dec r3 in the above piece of code will be considered because MOV instruction does not change the flag and Dec r3 is the last alu instruction.

    And to answer you question yes if DEC R4 was after DEC R1 , then R4 value would’ve been considered.

    Loop is executed $10$ times and there are two memory reference in the loop (each MOV
    is loading $1$ word, so $1$ memory reference for each MOV inside the loop). So number
    of memory reference inside loop is

    $2 \text{(MOV)}\times 10\text{ (times iteration)}\times 1\text{(1 word access/ MOV)}
    =20\text{ memory accesses.}$

    One memory access is outside the loop for the first instruction

    MOV R1, (3000)

    So, totally $20+1 = 21$

    Correct Answer: $D$

    @VS you are right. @akash.dinkar12 accessing data is different from updating data. Just considering the loop, here, we are “accessing” data in the first MOV instruction only. In the second MOV instruction we are accessing register and not the memory for, again, “accessing” data. If we think this way then the answer would be 11.

    Ist memory reference R1←M[3000] and then in the loop which runs for 10 times, because the content of memory location 3000 is 10 given in question and loop will run 10 times as

    R2← M[R3]

    M[R3] ←R2

    There are two memory reference every iteration

    10*2=20

    So total memory access will be 21

    The number of memory references for accessing the data in executing the program completely is :-

    https://gateoverflow.in/1269/gate2007-71?show=224234#c224234

    https://gateoverflow.in/1269/gate2007-71?show=103284#c103284

    $M[3000]=10\ | \ R3=2000\ |\ M[2000]\ to\ M[2010]=100$

    $R1=10(M_{ref})||R2=100(M_{ref})||R2=110||M[2000]=110(M_{ref})||R3=2001||R1=9||\Rightarrow3 (M_{ref})$

    $R2=100(M_{ref})||R2=109||M[2001]=109(M_{ref})||R3=2002||R1=8||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=108||M[2002]=108(M_{ref})||R3=2003||R1=7||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=107||M[2003]=107(M_{ref})||R3=2004||R1=6||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=106||M[2004]=106(M_{ref})||R3=2005||R1=5||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=105||M[2005]=105(M_{ref})||R3=2006||R1=4||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=104||M[2006]=104(M_{ref})||R3=2007||R1=3||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=103||M[2007]=103(M_{ref})||R3=2008||R1=2||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=102||M[2008]=102(M_{ref})||R3=2009||R1=1||\Rightarrow2 (M_{ref})$

    $R2=100(M_{ref})||R2=101||M[2009]=101(M_{ref})||R3=2010||R1=0||\Rightarrow2 (M_{ref})$

    Total $\Rightarrow 21 (M_{ref})$

    Address after Halt instruction
    Consider that the memory is byte addressable with size 16 bits, and the program has been loaded starting from memory location (2000)10. What will be the return address saved in the stack, if an interrupt occurs while the CPU has been halted after executing the HALT instruction? I am getting 2018.