Consider the following pseudo-code. ๐ฟ1: ๐‘ก1 = โˆ’1 ๐ฟ2: ๐‘ก2 = 0 ๐ฟ3: ๐‘ก3 = 0โ€ฆ

2024

Consider the following pseudo-code.

ย  ย  ย  ย  ๐ฟ1: ๐‘ก1 = โˆ’1

ย  ย  ย  ย  ๐ฟ2: ๐‘ก2 = 0

ย  ย  ย  ย  ๐ฟ3: ๐‘ก3 = 0

ย  ย  ย  ย  ๐ฟ4: ๐‘ก4 = 4 โˆ— ๐‘ก3

ย  ย  ย  ย  ๐ฟ5: ๐‘ก5 = 4 โˆ— ๐‘ก2

ย  ย  ย  ย  ๐ฟ6: ๐‘ก6 = ๐‘ก5 โˆ— ๐‘€

ย  ย  ย  ย  ๐ฟ7: ๐‘ก7 = ๐‘ก4 + ๐‘ก6

ย  ย  ย  ย  ๐ฟ8: ๐‘ก8 = ๐‘Ž[๐‘ก7]

ย  ย  ย  ย  ๐ฟ9: if ๐‘ก8 <= ๐‘š๐‘Ž๐‘ฅ goto ๐ฟ11

ย  ย  ย  ย  ๐ฟ10: ๐‘ก1 = ๐‘ก8

ย  ย  ย  ย  ๐ฟ11: ๐‘ก3 = ๐‘ก3 + 1

ย  ย  ย  ย  ๐ฟ12: if ๐‘ก3 < ๐‘€ goto ๐ฟ4

ย  ย  ย  ย  ๐ฟ13: ๐‘ก2 = ๐‘ก2 + 1

ย  ย  ย  ย  ๐ฟ14: if ๐‘ก2 < ๐‘ goto ๐ฟ3

ย  ย  ย  ย  ๐ฟ15: ๐‘š๐‘Ž๐‘ฅ = ๐‘ก1

Which one of the following options CORRECTLY specifies the number of basic

blocks and the number of instructions in the largest basic block, respectively ?

Answer: D. 7 and 6 โ€” tep 1: Identify leadersRules for leaders (start of basic blocks): First instruction is a leader โ†’ L1 Targets of jumps are leaders โ†’ L3, L4, L11, L15โ€ฆ

  1. A.

    6 and 6

  2. B.

    6 and 7

  3. C.

    7 and 7

  4. D.

    7 and 6

Attempted by 119 students.

Show answer & explanation

Correct answer: D

tep 1: Identify leaders

Rules for leaders (start of basic blocks):

  1. First instruction is a leader โ†’ L1

  2. Targets of jumps are leaders โ†’ L3, L4, L11, L15

  3. Instruction following a conditional or unconditional jump is a leader โ†’ L10, L13

Leaders: L1, L3, L4, L10, L11, L13, L15

So, 7 basic blocks (one per leader).

Step 2: Form basic blocks

  1. BB1: L1, L2 โ†’ straight-line until L3 (next leader)

  2. BB2: L3 โ†’ only L3

  3. BB3: L4, L5, L6, L7, L8, L9 โ†’ ends with conditional jump

  4. BB4: L10 โ†’ single instruction after jump

  5. BB5: L11, L12 โ†’ loop back to L4

  6. BB6: L13, L14 โ†’ loop back to L3

  7. BB7: L15 โ†’ final instruction

Step 3: Count instructions in each BB

BB

Instructions

BB1

2 (L1-L2)

BB2

1 (L3)

BB3

6 (L4-L9)

BB4

1 (L10)

BB5

2 (L11-L12)

BB6

2 (L13-L14)

BB7

1 (L15)

Largest BB = 7? Actually check: BB3 has 6 instructions (L4-L9), which is largest.

  • So largest BB = 6 instructions

  • Number of BB = 7

Step 4: Correct Answer

Numberย ofย BBย =ย 7,ย Largestย BBย =ย 6ย instructions\text{Number of BB = 7, Largest BB = 6 instructions}Numberย ofย BBย =ย 7,ย Largestย BBย =ย 6ย instructions

Answer: D โ†’ 7 and 6

A video solution is available for this question โ€” log in and enroll to watch it.

Explore the full course: Compiler Design

Loading lessonโ€ฆ