Question
Consider the following instruction sequence where registers R1, R2 and R3 are general purpose and denotes the content at the memory location .
| Instruction | Semantics | Instruction Size (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 if not zero to the given absolute address | 2 |
| HALT | Stop | 1 |
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)R1 = MEMORY[5000] = 10. InitialR3 = 3000. - Loop starts at address 1004:
MOV R2, (R3)ADD R2, R1MOV (R3), R2INC R3DEC R1BNZ 1004
Trace the loop iterations:
- Iteration 1:
R1 = 10,R3 = 3000. Memory location 3000 is updated.R3becomes 3001,R1becomes 9. Branch taken. - Iteration 2:
R1 = 9,R3 = 3001. Memory location 3001 is updated.R3becomes 3002,R1becomes 8. Branch taken.
... - Iteration 10:
R1 = 1,R3 = 3009. Memory location 3009 is updated.R3becomes 3010,R1becomes 0.
NowR1 == 0, soBNZ 1004evaluates to false. The branch is NOT taken and the CPU executesHALTat 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.
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.