GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPUTER ORGANIZATION & ARCHITECTURE

Trace a loop to identify an untouched memory endpoint

2 questions solved the same way, newest first. Back to Computer Organization & Architecture

How these are solved

Trace the counter and address-register updates of ten load/add/store iterations and determine the final value at the location immediately beyond the written range.

2 marks · NAT
Report issue

Instructions & Addressing Modes

Question 55, GATE 2021: A byte-addressable machine starts this sequence at address 1000. Instruction, semantics, size in bytes: MOV R1,(5000): R1=MEMORY[5000],4; MOV R2,(R3): R2=MEMORY[R3],4; ADD R2,R1: R2=R1+R2,2; MOV (R3),R2: MEMORY[R3]=R2,4; INC R3: R3=R3+1,2; DEC R1: R1=R1−1,2; BNZ 1004: branch to absolute address 1004 if not zero,2; HALT: stop,1. Initially MEMORY[5000]=10, R3=3000, and MEMORY[3000] through MEMORY[3010] are all 50. All numbers are decimal. Find MEMORY[3010] after execution.
Tap to enlarge
Answer & solution

Official answer: 50

The four-byte first instruction at address 1000 initializes R1=10, and address 1004 is the next instruction, the loop entry. Each loop iteration updates MEMORY[R3], then increments R3 and decrements R1. BNZ tests the zero condition left by that decrement and returns to 1004 while R1 is nonzero. Exactly ten iterations update locations 3000 through 3009. After the tenth, R3 is 3010 and R1 is zero, so the program halts before writing location 3010. Its original value 50 is unchanged.

Instruction addresses are 1000,1004,1008,1010,1014,1016,1018,1020. Thus branching to 1004 skips the initialization at 1000. At iteration k=1,...,10 the address updated is 2999+k and R1 before decrement is 11−k, so memory values written are 60,59,...,51 at 3000,...,3009. The final increment makes R3=3010, but DEC makes R1=0 and the branch falls through to HALT. Merely pointing at 3010 does not write to it.

Original paper, page 41 · Answer key

2 marks · MCQ
Report issue

Final value beyond loop write range

Instructions & Addressing Modes

Question 72, GATE 2007: R1,R2,R3 are general-purpose registers. The program, operation, and size in words are:
MOV R1,(3000): R1←M[3000], 2 words
LOOP: MOV R2,(R3): R2←M[R3], 1 word
ADD R2,R1: R2←R1+R2, 1 word
MOV (R3),R2: M[R3]←R2, 1 word
INC R3: R3←R3+1, 1 word
DEC R1: R1←R1−1, 1 word
BNZ LOOP: branch if the result is nonzero, 2 words
HALT: stop, 1 word.
Initially M[3000]=10, R3=2000 and each location 2000–2010 contains 100. The program begins at location 1000; all these numbers are decimal.
Assume word-addressable memory. What is the final content of location 2010?
Options:
(A) 100
(B) 101
(C) 102
(D) 110
Question 72, GATE 2007: R1,R2,R3 are general-purpose registers. The program, operation, and size in words are:
MOV R1,(3000): R1←M[3000], 2 words
LOOP: MOV R2,(R3): R2←M[R3], 1 word
ADD R2,R1: R2←R1+R2, 1 word
MOV (R3),R2: M[R3]←R2, 1 word
INC R3: R3←R3+1, 1 word
DEC R1: R1←R1−1, 1 word
BNZ LOOP: branch if the result is nonzero, 2 words
HALT: stop, 1 word.
Initially M[3000]=10, R3=2000 and each location 2000–2010 contains 100. The program begins at location 1000; all these numbers are decimal.
Assume word-addressable memory. What is the final content of location 2010?
Options:
(A) 100
(B) 101
(C) 102
(D) 110
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Approach. Track the address used before INC R3, not after it. The first iteration uses 2000; after ten iterations R3 reaches 2010 only when the loop is about to terminate.

2. Derivation and interpretation. R3 starts at 2000. The 10 loop iterations access addresses 2000 through 2009, then R3 becomes 2010 and R1 becomes zero, ending the loop. Address 2010 is never written, so its initial content 100 remains. Answer A.

Original paper, page 18