img
Question:
Published on: 12 October, 2024

Generate the three address code for the following code segment.

main()

{

       int a=1, z=0;

       int b[10];

       while (a<=10)

             b[a]=2*a;

       z=b[a]+a;

}

Answer:

Three address code for the above code segment:

 

I0

func begin main

I1

a:=1

I2

z:=0

I3

Label .L0

I4

if a<=10 goto .L1

I5

goto .L2

I6

label .L1

I7

_t0:=2*a

I8

_t1:=4*a

I9

_t2:=&b

I10

_t2[_t1]:=_t0;   // assuming the (size of int) = 4

I11

goto .L0

I12

label .L2

I13

_t3:=a

I14

_t4:=4*a

I15

_t5:=&b

I16

_t6:=_t5[_t4]

I17

_t7:=_t6+_t3

I18

z:=_t7

I19

func end main

Random questions