asked 103k views
0 votes
Generate code for the following three-address statements assuming a and b are arrays whose elements are 4-byte values. Also, compute the cost involved.

(a) x=a[i]+b[j]
(b) x=a[i]∗b[j]
(c) x=a[i]/b[j]
(d) x=a[i]−b[j]

asked
User Palladin
by
7.5k points

1 Answer

3 votes

Final answer:

The correct code for the three-address statement Option A. x = a[i] + b[j] involves loading the addresses of a and b, adding the indexes multiplied by the size of each element, loading the values from memory, adding them together, and storing the sum in the register holding x.

Step-by-step explanation:

The correct answer is option (a)

The three-address code for the statement x = a[i] + b[j] would be:

  1. Load the address of a into a register (e.g., R1)
  2. Add the index i multiplied by the size of each element (4 bytes) to the register holding the address of a to get the address of a[i] (e.g., R2)
  3. Load the value from the address in R2 into a register (e.g., R3)
  4. Load the address of b into a register (e.g., R4)
  5. Add the index j multiplied by the size of each element (4 bytes) to the register holding the address of b to get the address of b[j] (e.g., R5)
  6. Load the value from the address in R5 into a register (e.g., R6)
  7. Add the values in R3 and R6
  8. Store the sum in the register holding x (e.g., R7)

The cost involved in this operation would be the cost of loading the addresses and values from memory, which depends on the architecture being used.

answered
User Korakot
by
6.7k points