Question

Consider the following instruction sequence where registers R1, R2 and R3 are general purpose and MEMORY[X]\text{MEMORY}[X] denotes the content at the memory location XX.

Instruction sequence specification
InstructionSemanticsInstruction Size (bytes)
MOV R1, (5000)R1 <- MEMORY[5000]4
MOV R2, (R3)R2 <- MEMORY[R3]4
ADD R2, R1R2 <- R1 + R22
MOV (R3), R2MEMORY[R3] <- R24
INC R3R3 <- R3 + 12
DEC R1R1 <- R1 - 12
BNZ 1004Branch if not zero to the given absolute address2
HALTStop1

Assume that the content of the memory location 5000 is 10, and the content of the register R3 is 3000. The content of each of the memory locations from 3000 to 3010 is 50. The instruction sequence starts from the memory location 1000. All the numbers are in decimal format. Assume that the memory is byte addressable.

After the execution of the program, the content of memory location 3010 is __________

Official answer and solution
Official answer
50
From
Official GATE 2021 answer key

Solution

Trace the loop execution and register pointer increments to identify exactly which memory addresses are modified.

Let us trace the instruction sequence:

  • Address 1000: MOV R1, (5000)   ⟹  \implies R1 = MEMORY[5000] = 10. Initial R3 = 3000.
  • Loop starts at address 1004:
    • MOV R2, (R3)
    • ADD R2, R1
    • MOV (R3), R2
    • INC R3
    • DEC R1
    • BNZ 1004

Trace the loop iterations:

  • Iteration 1: R1 = 10, R3 = 3000. Memory location 3000 is updated. R3 becomes 3001, R1 becomes 9. Branch taken.
  • Iteration 2: R1 = 9, R3 = 3001. Memory location 3001 is updated. R3 becomes 3002, R1 becomes 8. Branch taken.
    ...
  • Iteration 10: R1 = 1, R3 = 3009. Memory location 3009 is updated. R3 becomes 3010, R1 becomes 0.
    Now R1 == 0, so BNZ 1004 evaluates to false. The branch is NOT taken and the CPU executes HALT at 1020.

Notice that the loop executed exactly 10 times, updating memory locations 3000 through 3009. Memory location 3010 was pointed to by R3 at loop exit, but was NEVER written to by MOV (R3), R2!

Therefore, the content of memory location 3010 remains completely unchanged at its initial value, which is 50.

Final answer

50

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2021 Computer Science and Information Technology (CS, Set-1)
Question
65 of 65, printed as CS Q.55
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.