GATE 2023 CS – Question 41
Consider the given C-code and its corresponding assembly code, with a few operands U1–U4 being unknown. The memory is byte-addressable.
int a[10], b[10], i;
// int is 32-bit
for (i=0; i<10;i++)
a[i] = b[i] * 8;;r1-r5 are 32-bit integer registers
;initialize r1=0, r2=10
;initialize r3, r4 with base address of a, b
L01: jeq r1, r2, end ;if(r1==r2) goto end
L02: lw r5, 0(r4) ;r5 <- Memory[r4+0]
L03: shl r5, r5, U1 ;r5 <- r5 << U1
L04: sw r5, 0(r3) ;Memory[r3+0] <- r5
L05: add r3, r3, U2 ;r3 <- r3+U2
L06: add r4, r4, U3
L07: add r1, r1, 1
L08: jmp U4 ;goto U4
L09: endWhich one of the following options is a CORRECT replacement for operands in the position (U1, U2, U3, U4) in the above assembly code?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (B) (3, 4, 4, L01)
Explanation
Multiplying by $8=2^3$ is a left shift by 3, so $U_1=3$ (option A and C use 8, which would multiply by $2^8$).
`int` is 32 bits $=4$ bytes and memory is byte-addressable, so to move to the next array element both pointers `r3` (into `a`) and `r4` (into `b`) must advance by 4: $U_2=U_3=4$.
The loop must go back to the test `jeq` at `L01`, so $U_4=\texttt{L01}$ (jumping to `L02` would skip the termination check).
$(U_1,U_2,U_3,U_4)=(3,4,4,\texttt{L01})$: option (B).