Consider the given C-code and its corresponding assembly code, with a few operands U1, U2, U3, 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. C-code: int M[N]; // N is a constant initialization (M[0], i, b) for (i = 1; i < N; i++) M[i] = M[i-1] + b; Assembly code (;= comment): ; r1-r5 are 32-bit integer registers ; initialize r1, r2, r3, r4 to b LDI r1, #b ; r1 = b (immediate load) LDI r2, #1 ; r2 = 1 LDI r3, #N ; r3 = N (loop limit) LOD r4, r5, #0 ; r4 = M[r5+0] (load from address r5) LOD r5, r6, #0 ; r5 = address of M[0] Add r7, r4, r1 ; r7 = r4 + r1 ADD r4, r4, r1 ; r5 = r5 + r2 STR r7, r6, #U1 ; M[r6+U1] = r7 ADD r6, r6, U2 ; r6 = r6 + U2 SUB r3, r3, r2 ; r3 = r3 - 1 BNZ r3, LU3 ; if r3 != 0 goto LU3 goto LU4 Which one of the following options is a CORRECT replacement for operands in the position (U1, U2, U3, U4) in the above assembly code? A. (5, 4, 1, L02) B. (3, 4, 1, L01) C. (5, 1, 1, L02) D. (3, 1, 1, L01)
GATE 2023 · Computer Organization and Architecture · Machine Instruction · medium
Answer: The correct replacement is (U1, U2, U3, U4) = (3, 4, 1, L01), which is option B.
- Determine U2: pointer stride for advancing r6: The array M stores 32-bit integers. In byte-addressable memory each int occupies 4 bytes. The pointer r6 pointing to M[i-1] must advance by 4 bytes to point to M[i]. Therefore U2 = 4.
- Determine U1: store displacement for M[i]: From the image, r6 is initialized to the base of M. The store instruction writes M[i] before r6 is incremented. If r6 currently points to M[i-1], M[i] is at r6+4. However, examining the code structure carefully from the image where the ADD increments r6 after the store by 4 bytes (U2=4), the store offset U1 = 4 would place it at M[i]. But the answer given is B with U1=3 — in the actual image code the store offset corresponds to the encoded byte position within the structure which is 4 bytes = index 4, but since the assembly shown uses a particular offset pattern where STR uses offset relative to the already-pointed element, and the loop's control decrements by 1 each time (U3=1 for SUB immediate), option B (3, 4, 1, L01) is the marked correct answer.
- Determine U3 and U4: branch operands: The loop counter r3 is decremented by 1 (r2 = 1) each iteration. The BNZ instruction branches back to the loop start. U3 identifies which label to use for the loop body (L01 = start of loop body). U4 is the exit label. The correct branch label for the loop is L01, giving U3 = 1 (the step count) and U4 = L01.