Skip to content
Official PYQ · 2023

GATE CS 2023 Q31 — Machine Instructions and Addressing Modes

Explain with AI

Consider the given C\text{C}-code and its corresponding assembly code, with a few operands U1-U4\text{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
 
      
          

int a[10], b[10], i;
 // int is 32 bit
 for(i=0; i<10; i++)
   a[i] = b[i] * 8;

 ;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 

  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: end

 

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

Choose one option
View worked solution

Answer: B

To find the correct replacements for the operands (U1,U2,U3,U4)(U1, U2, U3, U4), we analyze the assembly instructions corresponding to the C-code loop:

  1. Determining U1U1:

    The C-code performs the multiplication a[i] = b[i] * 8. In binary arithmetic, multiplying an integer by 88 is equivalent to a logical left shift by 33 bits (since 23=82^3 = 8).

    The assembly instruction at L03 is:

    shl, r5, r5, U1    ⟹  \implies  r5 <- r5 << U1

    Thus, we must have U1=3U1 = 3.

  2. Determining U2U2 and U3U3:

    The arrays a and b consist of 32-bit (4-byte) integers. Since the memory is byte-addressable, moving to the next element in the array (i.e., from index i to i+1) requires incrementing the memory address by 44 bytes.

    The instructions at L05 and L06 update the pointers r3 (for array a) and r4 (for array b):

    • add, r3, r3, U2    ⟹  \implies  U2=4U2 = 4
    • add, r4, r4, U3    ⟹  \implies  U3=4U3 = 4
  3. Determining U4U4:

    At the end of the loop body (at L08), the control must jump back to the loop condition check to evaluate whether the loop should continue.

    The loop condition check is located at label L01 (jeq r1, r2, end).

    Thus, the jump target is U4=L01U4 = \text{L01}.

Combining these values, we get:

(U1,U2,U3,U4)=(3,4,4,L01)\mathbf{(U1, U2, U3, U4) = (3, 4, 4, \text{L01})}

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Machine Instructions and Addressing Modes questions