Skip to content
Official PYQ · 1996

GATE CS 1996 Q2.16 — Lexical Analysis

Explain with AI

Which of the following macros can put a macro assembler into an infinite loop?

  1. .MACRO M1, X
    .IF EQ, X   ;if X=0 then
    M1 X + 1
    .ENDC
    .IF NE, X   ;if X ≠ O then
    .WORD X  ;address (X) is stored here
    .ENDC
    .ENDM
    
  2. .MACRO M2, X
    .IF EQ, X
    M2 X
    .ENDC
    .IF NE, X
    .WORD X + 1
    .ENDC
    .ENDM
Choose one option
View worked solution

Answer: A

To determine which macro can cause an infinite loop during macro expansion, let us trace the expansion of both macros with an initial argument of X=0X = 0.

1. Analysis of Macro (i)

Let us call M1 0 (i.e., X=0X = 0):

  • The condition .IF EQ, X is evaluated. Since X=0X = 0, this condition is true.
  • The macro recursively calls M1 X + 1, which evaluates to M1 1.
  • In the expansion of M1 1 (where X=1X = 1):
    • .IF EQ, X is false (since 1≠01 \neq 0).
    • .IF NE, X is true (since 1≠01 \neq 0), which generates .WORD 1.
    • The expansion of M1 1 terminates.
  • Returning to M1 0, the next block .IF NE, X is false (since X=0X = 0).
  • The expansion of M1 0 terminates successfully.

Since the recursion has a well-defined base case that increments XX to break the condition, Macro (i) does not cause an infinite loop.

2. Analysis of Macro (ii)

Let us call M2 0 (i.e., X=0X = 0):

  • The condition .IF EQ, X is evaluated. Since X=0X = 0, this condition is true.
  • The macro recursively calls M2 X, which is M2 0.
  • This recursive call M2 0 again finds X=0X = 0, evaluates the EQ condition to true, and calls M2 0 again.

This creates an infinite recursion of M2 0 → M2 0 → M2 0 → ..., which puts the macro assembler into an infinite loop.

Thus, only macro (ii) can put the macro assembler into an infinite loop.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Lexical Analysis questions