Given the following 3-address code

1: a = 1
2: b = 2
3: c = a + b
4: d = c - a
5: if( d < 20 ) goto 11
6: d = b * d
7: if( d > 20 ) goto 11
8: d = a + b
9: e = e + 1
10: goto 6
11: b = a + b
12: e = c - a
13: if( e < d ) goto 3
14: a = b * d
15: b = a – d
(a) What are the basic blocks for the code segment?
(b) Draw the control flow graph (CFG) for the code once you have determined the basic block.

To determine the basic blocks for the given 3-address code, we can look for the points where control flow decisions are made, such as conditional statements and branch instructions.

(a) The basic blocks for the code segment can be identified as follows:

Basic Block 1:
1: a = 1
2: b = 2

Basic Block 2:
3: c = a + b

Basic Block 3:
4: d = c - a
5: if( d < 20 ) goto 11

Basic Block 4:
6: d = b * d
7: if( d > 20 ) goto 11

Basic Block 5:
8: d = a + b
9: e = e + 1
10: goto 6

Basic Block 6:
11: b = a + b
12: e = c - a
13: if( e < d ) goto 3

Basic Block 7:
14: a = b * d
15: b = a – d

(b) Now we can draw the control flow graph (CFG) for the code using the identified basic blocks:

+----> BB1 ----+
| |
v |
BB2 -----+ |
| | |
v v |
BB3 <--- BB4 |
| | |
v v |
BB5 ---- BB6 --+
| |
v v
BB7 ----+

In the CFG, each basic block is represented by a node, and the directed edges represent the flow of control between the basic blocks. The CFG helps visualize the control flow of the code and can be useful for analyzing control flow dependencies and optimizing the code if needed.