Which of the following macros can put a macro assembler into an infinite loop?
-
.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
-
.MACRO M2, X .IF EQ, X M2 X .ENDC .IF NE, X .WORD X + 1 .ENDC .ENDM
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 .
1. Analysis of Macro (i)
Let us call M1 0 (i.e., ):
- The condition
.IF EQ, Xis evaluated. Since , this condition is true. - The macro recursively calls
M1 X + 1, which evaluates toM1 1. - In the expansion of
M1 1(where ):.IF EQ, Xis false (since )..IF NE, Xis true (since ), which generates.WORD 1.- The expansion of
M1 1terminates.
- Returning to
M1 0, the next block.IF NE, Xis false (since ). - The expansion of
M1 0terminates successfully.
Since the recursion has a well-defined base case that increments to break the condition, Macro (i) does not cause an infinite loop.
2. Analysis of Macro (ii)
Let us call M2 0 (i.e., ):
- The condition
.IF EQ, Xis evaluated. Since , this condition is true. - The macro recursively calls
M2 X, which isM2 0. - This recursive call
M2 0again finds , evaluates theEQcondition to true, and callsM2 0again.
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.
