News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

forward keyword in macro

Started by Emil_halim, November 13, 2010, 06:39:15 PM

Previous topic - Next topic

Emil_halim

what does the keyword forward meaning in the next program?

how is job of the "theVar" in the macro and how it replaced with the i variable?


program InitDemo;
#include( "stdlib.hhf" )

    #macro init_int32( initVal ):theVar;
   
        forward( theVar );
        theVar:int32;
        ?_initialize_ :=
                _initialize_ +
                "mov( " +
                @string:initVal +
                ", " +
                @string:theVar +
                " );";
    #endmacro
                       
var
    i:init_int32( 1 );
   
begin InitDemo;

    stdout.put( "i=", i, nl );
                   
end InitDemo;


Sevag.K

Quote from: Emil_halim on November 13, 2010, 06:39:15 PM
what does the keyword forward meaning in the next program?

how is job of the "theVar" in the macro and how it replaced with the i variable?

forward is a special keyword that 'looks back' on the line of the macro declaration to find the identifier declared right before the macro in the source and text equates the identifier with the parameter passed in forward.  doing so will make the entire declaration a part of the macro expansion.

so in the example below, having
i:init_32( 1 );

theVar in the macro will become a text constant of the the letter 'i'

now that theVar represents 'i', the next line in the macro
theVar:int32;

is the same as saying i:int32;

the next part, ?_initialize will expand after the begin statement.
so after begin InitDemo, the macro will expand this code:

mov( 1, i  );



program InitDemo;
#include( "stdlib.hhf" )

    #macro init_int32( initVal ):theVar;
   
        forward( theVar );
        theVar:int32;
        ?_initialize_ :=
                _initialize_ +
                "mov( " +
                @string:initVal +
                ", " +
                @string:theVar +
                " );";
    #endmacro
                       
var
    i:init_int32( 1 );
   
begin InitDemo;

    stdout.put( "i=", i, nl );
                   
end InitDemo;


Quote