Structured Assembler Macros: The IF statement

Back in the 70′s many thoughtful computer scientists weighed in on a now-dead issue of the GoTo statement in high-level languages.  The issue seems to be pretty much settled now, with many new languages avoiding GoTo altogether, and high-level programmers avoiding their use except in very specialized circumstances.   But assembler programmers depend on branch statements (conditional and unconditional) as a required part of any program logic. We can’t avoid coding Branch statements, so the goal is to use them in a reasonably straightforward way, avoiding spaghetti logic.  Good assembler programmers do this intuitively after a while, but IBM has included some high-level macros that make the job even easier now.  The macros are distributed in the HLASM Toolkit.  The macros are stored in a library called hlg.SASMMAC2 (where hlq is a high-level qualifier that varies from system to system).  If your system has the toolkit, include the macro library in your SYSLIB for the compile.  On my system, the JCL looks like this:

//C.SYSLIB   DD   DSN=SYS1.MACLIB,DISP=SHR
//                  DD   DSN=HLA.SASMMAC2,DISP=SHR

In the next few postings, we will look at the macros that are available.  In this first note, we look at three macros that support if-statements:  If, ELSE, and ENDIF.

   The IF statement comes in a variety of formats, but I especially like the format below:

        IF (CLC,X,LT,Y)                   
            MVC  SMALLER,X                 
            MVC  LARGER,Y                  
         ELSE                              
            MVC  SMALLER,Y                 
            MVC  LARGER,X                  
         ENDIF               

The predicate in the If statement has four parts:

1)  A compare instruction mnemonic (like CLC or CP)

2)  The operand 1 field

3)  A condition parameter (H – high, GT -greater than, L – low, LT – less than, E or EQ – equal)

4) The operand 2 field

Like if-statements in high-level languages, the true block occurs between the IF and the ELSE, and the false block occurs after the ELSE and before the ENDIF.

When the macros are assembled, the required branch statements and labels are inserted.  IF statements can be nested and used in conjuction with other structured macros, just as you would hope for. 

    Examine the generated code by coding PRINT ON,NODATA,GEN just before the if statement, and PRINT ON,NODATA, NOGEN just after the ENDIF.

   Give the IF statement a try – you’ll like it.

Leave a comment

February 12, 2013 · 12:44 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s