[comp.os.vms] MACRO Puzzle

CURLEY@WHARTON.UPENN.EDU ("Curley, Robert F.") (06/15/87)

This is not new, but it came up in discussion recently and I thought
that those who are interested in MACRO might be interested.

Consider the following program (originally written in MACRO-11):

	.MACRO	BLUE
	.IF NDF BLACK
BLACK=0
	.ENDC
BLACK=BLACK+1
	.ENDM
;
	BLUE
RED:	.WORD	BLACK
	.END

The question is: "What value does the assembler place in RED?"

The interesting observation: "The value depends upon whether you run this
code through the VAX MACRO or through the MACRO-11 assembler."

--Bob Curley

.__. ._ _.._  ..._ ___ _... .. ... _._. .._ __  _.. .  .__ ...__ .... .__. __.
PPPPP   EEEEEE N    N N    N !Robert F. Curley        Curley@Wharton.UPENN.edu
P    P  E      NN   N NN   N !Division of Medical Physics
PPPPP   EEEE   N N  N N N  N !Department of Radiation Therapy
P       E      N  N N N  N N !University of Pennsylvania
P       E      N   NN N   NN !P.O. Box 7806
P       EEEEEE N    N N    N !Philadelphia, PA 19101             (215)662-3083
.__. ._ _.._  ..._ ___ _... .. ... _._. .._ __  _.. .  .__ ...__ .... .__. __.

carl@CITHEX.CALTECH.EDU (Carl J Lydick) (06/18/87)

 > Consider the following program (originally written in MACRO-11):

 >               .MACRO  BLUE
 >               .IF NDF BLACK
 > BLACK=0
 >               .ENDC
 > BLACK=BLACK+1
 >               .ENDM
 > ;
 >               BLUE
 > RED:  .WORD   BLACK
 >               .END

 > The question is:  "What value does the assembler place in RED?"

 > The interesting observation:  "The value depends upon whether you run  this
 > code through the VAX MACRO or through the MACRO-11 assembler."

A further observation: When you assemble this program in with the command:
	> MAC TEMP,TEMP/LI:ME/-SP=TEMP
the expansion of the macro and conditional is as follows:

      1						.MACRO	BLUE
      2						.IF NDF BLACK
      3					BLACK=0
      4						.ENDC
      5					BLACK=BLACK+1
      6						.ENDM
      7					;
      8	000000					BLUE
						.IF NDF BLACK
					BLACK=0
					  	.ENDC
		000002 			BLACK=BLACK+1
      9	000000	000002 			RED:	.WORD	BLACK
     10		000001 				.END

Notice that the the condition "NDF BLACK"  apparently  evaluated  FALSE:   the
MACRO-11  assembler  is apparently expanding the MACRO twice:  presumably once
during an earlier pass, at the end of which it  stored  the  value  BLACK  had
attained,  and  in the first encounter this pass, BLACK is already defined and
resumes its value from the first pass.  If you initialize BLACK  by  inserting
the record:
        BLACK=0
either prior to the first reference to it as an  r-value  or  after  the  last
other reference to it as an l-value, then the MACRO-32 and MACRO-11 assemblers
produce the same results.