.MACRO

Why is MACRO called MACRO? MACRO is a macro assembler, so the people at DEC decided to call the language MACRO. A macro assembler is one capable of creating macros. A macro is a section of code that you can assign a name to, and invoke by "saying" that name in your code. At this point you might be saying, "Isn't that a subroutine?". A subroutine is a section of code that you invoke by "saying" it's name, but there are difference between a subroutine (or procedure) and a macro. Getting back to my first question; why MACRO? DEC could've named their assembler PROCEDURE, but it wouldn't have been distinctive. All assemblers must support procedures to be viable languages, but not all assemblers have supported macros (they all do today, but not in the early days of computing). The name MACRO is lke claiming bragging rights, "We support macros.".

A procedure, like a macro, is just a section of code. When you load a program into memory the procedures (if any) are loaded in just once per subroutine. I.e., the code for each procedure goes in only once. When your program invokes the procedure it does so by calling. In MACRO there are several ways to call a procedure, but the basic way to call is for the program to record where it's next address to execute is (if the call weren't being made) and then goto the address in memory where the procedure resides. When the procedure is finished executing a "return" is performed; the program jumps back to the address that was recorded before invoking the call. If arguments are passed to the procedure then another mechanism has to be in place to pass these to the procedure. MACRO gives alternative methods for passing arguments, but they all require additional work, and hence slow down your program.

A macro isn't called; it's inserted into your code. I.e., each invocation of a macro in your code is just a string replacement of the macro's code. Here's an example of a macro:

.MACRO DOUBLONG X
ADDL2 X, X
.ENDM

DOUBLONG doubles the value of a longword. If you enter the code above into your MACRO program you can double the value of any longword with the code

DOUBLONG along ; where along is a longword in this program.

DOUBLONG is a very short section of code, and therefore a better candidate for being a macro instead of a procedure. Since every instance of DOUBLONG will result in a string replacement of the code for DOUBLONG we won't have any of the overhead associated with calling. I.E, we won't have to push arguments on a stack and retrieve them (CALLS is the MACRO instruction that uses this technique), or save the value of the next instruction, or have to jump around memory to get to our code. The steps in calling use up a lot of computer time, which is really a shame when you consider that you're probably using assembly language to save time in the first place.

On the downside, all those string replacements of code use up more memory then procedures do. If your section of code is very long, then making it a macro could result in it having a very big footprint in memory. This could even become a big performance issue if the memory footprint becomes large enough to result in too much paging. Paging is what the computer does when it doesn't have enough memory to fit the entire program in. The computer (actually, the operating system) writes a part of the program not being used to disk, and puts a section of the program that was on disk (and now needs) into memory (overwriting what was the code that was just saved to disk). These reads and writes are done in "pages", and hence the name "paging". So keeping your programs small is also keeping them faster, and saves on machine resources. That's why DOUBLONG is a good candidate for becoming a macro; it'll never use up much memory, and we save a lot of processing time. A routine with 40 lines of code would make a bad macro, but a great procedure (subroutine).

One more thing you can do with macros is make libraries out of them. A library is a collection of routines. Procedures can be independently assembled (or compiled if written in a hll) and stuck together to form an "object library". Object libraries aren't programs (though they are called "service programs" on the IBM AS/400) since there is no "main" routine to actually execute them. But object libraries can be linked into a program, and the procedures can be called just as if they were coded into the program. A macro library consists of macros. You don't assemble the macro code, but you can put it in a macro library. If you assemble the macro library with your program the macros can be used just as if the code was inserted into your program. The steps are:

(1) First create a file with your macro in it: MYMAC.MAR
(2) $LIBRARY/CREATE/MACRO LIBNAME
(3) $LIBRARY/INSERT/MACRO LIBNAME MYMAC
(4) MACRO filename+LIBNAME/LIB

filename is a macro program that uses macros in the file LIBNAME.

Back to the VIOTW page.