Consider the following instruction sequence where registers \(R1,R2\) and…
2021
Consider the following instruction sequence where registers \(R1,R2\) and \(R3\) are general purpose and \(MEMORY[X]\) denotes the content at the memory location \(X\).
\(\begin{array}{llc} \textbf{Instruction} & \textbf{Semantics} & \textbf{Instruction Size} \text{ (bytes)} \\ \hline \text{MOV } \text{R1}, (5000) & \text{R1} \leftarrow \text{MEMORY}[5000] & 4 \\ \hline \text{MOV } \text{R2}, (\text{R3}) & \text{R2} \leftarrow \text{MEMORY[R3]} & 4 \\ \hline \text{ADD} \text{R2}, \text{R1} & \text{R2} \leftarrow \text{R1} + \text{R2} & 2 \\ \hline \text{MOV } (\text{R3}), \text{R2} & \text{MEMORY[R3]} \leftarrow \text{R2} & 4 \\ \hline \text{INC } \text{R3} & \text{R3} \leftarrow \text{R3}+1 & 2 \\ \hline \text{DEC } \text{R1} & \text{R1} \leftarrow \text{R1}-1 & 2 \\ \hline \text{BNZ } 1004 & \text{Branch if not zero to the} & 2 \\ & \text{given absolute address}& \\ \hline \text{HALT} & \text{Stop} & 1 \\ \hline \end{array}\)
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 ____________ .
Attempted by 50 students.
Show answer & explanation
Correct answer: 50
Final answer: 50
Initial state: MEMORY[5000] = 10, R3 = 3000, and MEMORY[3000..3010] = 50. The program starts at address 1000.
Instruction layout (with sizes) from address 1000:
1000: MOV R1, (5000) (4 bytes) -> next 1004
1004: MOV R2, (R3) (4 bytes) -> next 1008 (this is the loop start)
1008: ADD R2, R1 (2 bytes) -> 1010
1010: MOV (R3), R2 (4 bytes) -> 1014
1014: INC R3 (2 bytes) -> 1016
1016: DEC R1 (2 bytes) -> 1018
1018: BNZ 1004 (2 bytes) -> loops back to 1004 while R1 != 0
Loop behavior: MOV R1,(5000) sets R1 = 10. Each loop iteration does:
Load the value at MEMORY[R3] (initially 50), add the current R1, and write the result back to MEMORY[R3].
Increment R3 by 1 and decrement R1 by 1. If R1 is not zero, repeat.
Concrete effect over iterations (R1 starts at 10, R3 starts at 3000):
Iteration 1: MEMORY[3000] = 50 + 10 = 60; R3 -> 3001; R1 -> 9
Iteration 2: MEMORY[3001] = 50 + 9 = 59; R3 -> 3002; R1 -> 8
Iteration 3: MEMORY[3002] = 50 + 8 = 58; R3 -> 3003; R1 -> 7
Iteration 4: MEMORY[3003] = 50 + 7 = 57; R3 -> 3004; R1 -> 6
Iteration 5: MEMORY[3004] = 50 + 6 = 56; R3 -> 3005; R1 -> 5
Iteration 6: MEMORY[3005] = 50 + 5 = 55; R3 -> 3006; R1 -> 4
Iteration 7: MEMORY[3006] = 50 + 4 = 54; R3 -> 3007; R1 -> 3
Iteration 8: MEMORY[3007] = 50 + 3 = 53; R3 -> 3008; R1 -> 2
Iteration 9: MEMORY[3008] = 50 + 2 = 52; R3 -> 3009; R1 -> 1
Iteration 10: MEMORY[3009] = 50 + 1 = 51; R3 -> 3010; R1 -> 0
When R1 becomes 0 the branch is not taken and the loop ends. The loop updates addresses 3000 through 3009, so MEMORY[3010] is never written and remains its initial value.
Therefore the content of memory location 3010 after program execution is 50.
A video solution is available for this question — log in and enroll to watch it.