Consider the following program segment. Here R1, R2 and R3 are the general purpose registers. Instruction Operation Instruction Size (no. of words) MOV R1,(3000) R1 <- M[3000] 2 LOOP: MOV R2,(R3) R2 <- M[R3] 1 ADD R2,R1 R2 <- R2 + R1 1 MOV (R3),R2 M[R3] <- R2 1 INC R3 R3 <- R3 + 1 1 DEC R1 R1 <- R1 - 1 1 BNZ LOOP Branch if not zero 2 HALT Stop 1 Assume that the content of memory location 3000 is 10 and the content of the register 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: A. 10 B. 11 C. 20 D. 21

GATE 2007 · Computer Organization and Architecture · Machine Instruction · medium

Answer: The total number of data memory references is 21 (Option D).

  1. Identify data memory accesses outside the loop: MOV R1,(3000): reads from memory address 3000 to load the loop counter into R1. This is 1 data memory reference. No other data accesses occur before the loop.
  2. Count data memory accesses per loop iteration: Inside the loop: (1) MOV R2,(R3) reads M[R3] — 1 data read. (2) ADD R2,R1 operates on registers only — 0 data refs. (3) MOV (R3),R2 writes to M[R3] — 1 data write. (4) INC R3, DEC R1, BNZ LOOP operate on registers/PC only — 0 data refs. So each iteration contributes 2 data memory references.
  3. Compute total data memory references: R1 starts at 10 and DEC R1 runs until R1 = 0, so the loop executes exactly 10 times. Total = 1 (pre-loop load) + 10 x 2 (loop reads and writes) = 1 + 20 = 21.