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.

//C-code                    ;assembly-code (; indicates comments)
                            ;r1-r5 are 32-bit integer registers
                            ;initialize r1=0, r2=10
                            ;initialize r3, r4 with base address of a, b

int a[10], b[10], i;            L01: jeq r1, r2, end ;if(r1==r2) goto end
// int is 32-bit                L02: lw r5, 0(r4)        ;r5 <- Memory[r4+0]
for (i=0; i<10;i++)             L03: shl r5, r5, U1      ;r5 <- r5 << U1
    a[i] = b[i] * 8;            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: end

Which one of the following options is a CORRECT replacement for operands in the position (U1, U2, U3, U4) in the above assembly code?

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2023 answer key

Solution

Match the C code operations to the corresponding assembly instructions and operand values.

  1. Multiplication by 8 (U1U_1):
    a[i] = b[i] * 8; is implemented as shl r5, r5, U1. Since shifting left by kk bits multiplies by 2k2^k, and 8=238 = 2^3, we have U1=3U_1 = 3.
  2. Pointer increments (U2,U3U_2, U_3):
    r3 points to array a and r4 points to array b. In 32-bit systems, each int is 4 bytes. Moving to the next element (i++) advances the byte address by 4. Thus, U2=4U_2 = 4 and U3=4U_3 = 4.
  3. Loop target (U4U_4):
    After incrementing the loop index r1, jmp U4 must jump back to check the loop termination condition jeq r1, r2, end at label L01. Thus, U4=L01U_4 = L01.

Therefore, (U1,U2,U3,U4)=(3,4,4,L01)(U_1, U_2, U_3, U_4) = (3, 4, 4, L01).

Final answer

Option (B)

Common trap

Setting U1=8U_1 = 8 instead of the shift count 33, or setting byte increments to 1 instead of 4.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2023 Computer Science and Information Technology (CS)
Question
41 of 65
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.