edited by
21,900 views
54 votes
54 votes

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

  1. $10$
  2. $11$
  3. $20$
  4. $21$
edited by

5 Answers

Best answer
54 votes
54 votes

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$

edited by
13 votes
13 votes

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

4 votes
4 votes

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})$

Answer:

Related questions

30 votes
30 votes
2 answers
1
go_editor asked Apr 23, 2016
9,840 views
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} & \...
35 votes
35 votes
5 answers
2
go_editor asked Apr 23, 2016
9,356 views
Consider the following program segment. Here $\text{R1, R2}$ and $\text{R3}$ are the general purpose registers.$$\small \begin{array}{|c|l|l||c|} \hline & \text {Instruct...
54 votes
54 votes
7 answers
3
Kathleen asked Sep 21, 2014
13,362 views
In a simplified computer the instructions are:$$\begin{array}{|l|l|} \hline \text {OP }R _j , R _i & \text{Perform }R _j \text{ OP } R _i \text{ and store the result in r...