Question:

Consider the given C-code and its corresponding assembly code, with a few operands U1–U4 being unknown. Some useful information as well as the semantics of each unique assembly instruction is annotated as inline comments in the code. The memory is byte-addressable. 

Show Hint

Array index steps in byte-addressable memory depend on element size: for 32-bit int}, add $4$ each iteration. Multiplying by $2^k$ is just a left shift by $k$.
Updated On: Aug 26, 2025
  • $(8,\,4,\,1,\,\text{L02})$
  • $(3,\,4,\,4,\,\text{L01})$
  • $(8,\,1,\,1,\,\text{L02})$
  • $(3,\,1,\,1,\,\text{L01})$
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

C-code: for(i=0; i<10; i++) a[i] = b[i] * 8;
Registers: \(r1 \leftarrow 0\), \(r2 \leftarrow 10\), \(r3 \leftarrow\) base of \(a\), \(r4 \leftarrow\) base of \(b\).

Step 1 (Multiply by 8 ⇒ shift left by 3).
L03: shl r5, r5, U1 must implement \(r5 \gets r5 \ll U1\) so that \(b[i] \times 8\) is computed ⇒ \(U1 = 3\).

Step 2 (Address increments for 32-bit ints).
Each int is 4 bytes in a byte-addressable memory.
After storing to \(a[i]\) and loading \(b[i]\), the addresses must advance to the next element: \(r3 \gets r3 + U2\), \(r4 \gets r4 + U3\).
Thus \(U2 = 4\) and \(U3 = 4\).

Step 3 (Loop back target).
After L07 increments \(i\) (\(r1\)), control must jump to the loop test.
The comparison is at L01: jeq r1, r2, end.
Hence the unconditional jump target is \(U4 = \text{L01}\).

\[ \boxed{(U1, U2, U3, U4) = (3, 4, 4, \text{L01})} \]
Was this answer helpful?
0
0

Questions Asked in GATE CS exam

View More Questions