MACROS

WHAT IS A MACRO?

Probably the best way to answer this is to begin by looking at an example. In C/C++, there were two ways you could define a constant:

The first method basically allocated storage for a variable whose value was set at initialization and could not be altered later. Memory for the constant was allocated at some address and whenever you used it the computer got its value from that address during runtime. Because it was actually a variable, the compiler could do runtime type checking to make sure it behaved in a defined way when involved in arithmetic with other variables.

The second method, though it may have seemed the same, is actually radically different. When you compiled your code, the preprocessor went through it and everywhere it saw CONSTANT used, it literally cut it out and pasted VALUE in its places, as if you had gone through and typed VALUE everywhere you wanted CONSTANT used. This had some serious ramifications. While it saved you memory and sped up performance of your program since VALUE was actually hard-coded and the computer did not need to constantly look in memory for the constant’s value, it also was more dangerous because the computer could not do any sort of runtime type checking.

In general, we prefer safety over speed and memory benefits (issues we try not to worry about unless they turn out to be critical at the very end). This means that we would prefer to use "const int/double/etc." over "#define" whenever possible.

This is basically the situation we have with functions and macros. The former is safe; the latter is faster and saves memory. But macros are even more dangerous than the constants described above; if you make one syntactic mistake inside a macro it gets copied into every spot you used the macro. And, if that weren’t enough, the syntax checker is not smart enough to realize that such an error occurred within the macro section of the code you see and not the various spots in your code where the compiler copied it. For these reasons we would much rather use functions over macros, except that ABEL does not support functions. So, since we still like the idea of code reuse, we will go ahead and use macros, just being sure to keep in mind that they can be very dangerous if not used carefully.

STYLE

Same rules apply for brackets as with logical expressions. For a short input list, the list should be placed on the same line as the macro name with no space between the name and the opening parenthesis. Inputs in this case should be separated by commas and no spaces. For longer lists, the opening and closing parenthesis should be indented to level of the macro’s body and each be given their own line. In this case each input should be given its own line.

EXAMPLES

ALUOutput(iProcCtrl,iCout)
        {
        procCtrl        = ?iProcCtrl;
        cout            = ?iCout;
        };

ProcOutput
        (
        iSum,
        iQuotient,
        iDivisor,
        iRemainder,
        iProduct
        )
        {
        sum             = ?iSum;
        quotient        = ?iQuotient;
        divisor         = ?iDivisor;
        remainder       = ?iRemainder;
        product         = ?iProduct;
        };