Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider an array A of n elements of integer type for assignment statement

Value = A[i]

Which of the following is three address code representation for address computation and value assignment above ?

  • A.Temp1 = i * sizeof (integer) / Temp2 = &A + Temp1 / Value = *(Temp2)
  • B.Temp1 = i * sizeof (integer) / Temp2 = &A + Temp2 / *(Temp2) = value
  • C.Temp1 = &A * sizeof (integer) / Temp2 = Temp1 + i / Value = *(Temp2)
  • D.Temp1 = &A * i / Temp2 = Temp1 + sizeof (integer) / Value = *(Temp2)

Answer: A

The correct three-address code is Temp1 = i * sizeof (integer), Temp2 = &A + Temp1, Value = *(Temp2).

The address of A[i] is the base address of A plus i times the size of one element. So the code first scales the index (Temp1 = i * sizeof(integer)), then adds it to the base address (Temp2 = &A + Temp1), and finally loads the value stored at that address (Value = *(Temp2)).

Option (B) uses Temp2 before it has been computed and stores into the array instead of reading from it. Options (C) and (D) multiply the base address itself, which has no meaning in address arithmetic: the base must be added, not scaled.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.