0% found this document useful (0 votes)
6 views7 pages

3-Address Code For Array1

Three-address code (TAC) for arrays involves using temporary variables and explicit address calculations (loads and stores) to access memory locations, as each instruction can have at most three operands. Array access typically uses indexed assignment statements.

Uploaded by

r04929130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

3-Address Code For Array1

Three-address code (TAC) for arrays involves using temporary variables and explicit address calculations (loads and stores) to access memory locations, as each instruction can have at most three operands. Array access typically uses indexed assignment statements.

Uploaded by

r04929130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

3-address code for Array:-

Ques i=1
a[i] = 10; (1 dimensional)
Ans - 3- address code is written for index of array & not for array.
a[i] = base + ( i - lb ) * w
base - base address
lb - lower bound (starting index)
w - width (int → 2, float → 4)
a[i] = base + i*w - lb*w
= base - ( lb*w ) + ( i*w )
t1 = i * w
t2 = lb * w
t3 = base - t2
t3 [t1] = 10

Ques b = a[ i ]
Ans - t1 = i * w
t2 = lb * w
t3 = base - t2
t4 = t3 [ t1 ]
b = t4

For 2- dimensional
1. Row Major Order ( RMO )
2. Column Major Order ( CMO )

Row Major Order


RMO : a [ i ][j] = base + [ ( i - lb1 ) * nc + ( j - lb2 ) ] * w
= base + i*nc*w - lb1*nc*w + j*w - lb2*w
= ( base - lb1*nc*w - lb2*w ) + ( i*nc*w + j*w )
|---------------------| |---------------|
Constant Variable
Ques3. i = 1
j=1
a [i] [j] = 10
Ans -
RMO : i = 1 CMO : i = 1
j=1 j=1
t1 = lb1 * nc t1 = lb1 * w
t2 = t1 * w t2 = lb2 * nr
t3 = lb2 * w t3 = t2 * w
t4 = base - t2 t4 = base - t1
t5 = t4 - t3 t5 = t4 - t3
t6 = i * nc t6 = i * w
t7 = t6 * w t7 = j * nr
t8 = j * w t8 = t7 * w
t9 = t7 + t8 t9 = t6 + t8
t5 [ t9 ] = 10 t5 [ t9 ] = 10
Column Major Order
CMO : a [ i ] [j]= base + [ ( i - lb1 ) + ( j - lb2 ) * nr ] * w
= base + i*w - lb1*w + j*nr*w - lb2*nr*w
= ( base - lb1*w - lb2*nr*w ) + ( i*w + j*nr*w )
|--------------------| |---------------|
Constant Variable
Ques. int a [10], b [10], i, dp = 0,
for ( i=0; i<10; i++) {
dp += a [i] * b [i];
}
Ans -

1. dp = 0

2. i = 0

3. if ( i<10 ) goto (5)

4. goto (17)

5. t1 = addr (a) |

6. t2 = i * 4 | a[ i ]

7. t3 = t1 [t2] |

8. t4 = addr (b) |

9. t5 = i * 4 | b [ i ]

10. t6 = t4 [t5] |

11. t7 = t3 * t6

12. t8 = dp + t7

13. dp = t8

14. t9 = i +1

15. i = t9

16. goto (3)

17. goto Last


Ques. m X n is order of a
p X q is order of b
int a[10][10] , b[10][10] , i , dp = 0 ;
for ( i = 0; i < 10; i++ ) {
dp += a [i] [j] * b [i] [j];
}

Ans -

1. dp = 0

2. i = 0

3. if i < 10 goto (5)

4. goto (21)

5. t1 = addr (a)

6. t2 = i * n

7. t3 = t2 + j

8. t4 = t3 * 4

9. t5 = t1 [ t4 ]

10. t6 = addr (b)

11. t7 = i * q

12. t8 = t7 + j

13. t9 = t8 * 4

14. t10 = t6 [ t9 ]

15. t11 = t5 * t10

16. t12 = dp + t11

17. dp = t12

18. t13 = i + 1
19. i = t13

20. goto (3)

21.

(Column- Major)
m X n is order
a [i] [j] = Base + [ ( i - lb1 ) + ( j - lb2 ) * m ] * w
a [i] [j] = Base + [ i + ( j * m ) ] * w
1. t1 = addr (a)
2. t2 = j * m
3. t3 = i + t2
4. t4 = t3 * 4
5. t5 = t1 [ t4 ]

Ques.
begin
Here, 20 X 20 is the order
prod = 0;
i = 0;
j = 0;
do
begin
prod = prod + a [i] [j] * b [i] [j];
i = i + 1;
j = j + 1;
end
while i <= 20 and j <= 20
end

Ans -
1. prod = 0

2. i = 0

3. j = 0

4. t1 = addr (a) |

5. t2 = i * 20 |

6. t3 = t2 + j | a[ i ][ j ]

7. t4 = t3 * 4 |

8. t5 = t1 [ t4 ] |

9. t6 = addr (b) |

10. t7 = i * 20 |

11. t8 = t7 + j | b[ i ][ j ]

12. t9 = t8 * 4 |

13. t10 = t6 [ t9 ] |

14. t11 = t5 * t10

15. t12 = prod + t11

16. prod = t12

17. t13 = i + 1

18. i = t13

19. t14 = j + 1

20. j = t14

21. if i <= 20 goto (23)

22. goto (25)

23. if i <= 20 goto (4)

24. goto (25)


25.

You might also like