• edited by
34,501 views
81 81 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$

7 Answers

Best answer
70 70 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
15 15 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

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

• edited by
1 1 vote

Option D is correct 

before loop there is 1 memory access

Loop is accessed 10 times as Branch if not zero is checked 10 times, and each time, R1 is getting decremented.

in 1 loop there are 2 memory accesses

total 

1+ 10(2) = 21

 

one could also ask what will be the memory after execution,

2010 will be stored from 2000 memory location to 2009 memory location

Answer:
Position:
Show:

Related questions

47 47 votes
3 answers 3 answers
15.6k
15.6k views
go_editor asked Apr 23, 2016
15,646 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} & \...
49 49 votes
6 answers 6 answers
14.7k
14.7k views
go_editor asked Apr 23, 2016
14,689 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...
71 71 votes
9 answers 9 answers
21.9k
21.9k views
Kathleen asked Sep 21, 2014
21,934 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...
3 3 votes
1 1 answer
2.8k
2.8k views
Shubhanshu asked Jan 2, 2018
2,839 views
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 sa...