Using Macros to call Procedures


In the previous question you discovered a disadvantage to macros: since each macro statement is replaced by a block of text, the expanded source file can get very large (and the object module it translates into can get very large.)

Procedures do not have this disadvantage. The code that makes up a procedure is implemented just once, and it is activated by transfers of control to it (and it returns control to the caller.) 10 procedure calls result in just one copy of the code, plus 10 fairly simple call statements.

So in general: use macros for short, useful operations; use procedures for large blocks of code.

A very common combination is to write a macro that sets up code for a complicated procedure call. The annoying detail of setting up the call is the right size and complication for a macro; the actual block of code is implemented once as a procedure. For example, here it the definition of the READLINE macro that you have been using all semester (found on page 445 of the text):

.MACRO    READLINE    WHERETO
          PUSHAB      WHERETO   ; Stack user buffer addr.
          CALLS       #1,RDLINE
.ENDM     READLINE
The macro body is short: it expands into the assembly language statements that push the address of the user's input buffer on the processor stack, then CALLS a procedure.

Re-examine the DCL commands you have been using to assemble and run your programs:

$MACRO/LIST/SHOW=EXPAND ANYPROG.MAR + [CS.KJELL.MYMACROS]IOMAC.MLB/LIB

$LINK ANYPROG.OBJ + [CS.KJELL.MYMACROS]IOMOD.OBJ

$RUN ANYPROG.EXE

In the first command, IOMAC.MLB is a macro library that contains the above definition of the macro READLINE. The MACRO assembler uses a macro processor to substitute the macro definition for any READLINE macro statement in the source file. The expanded source file will assemble into an object module, ANYPROG.OBJ which contains a reference to the procedure RDLINE used in the macro body.

The LINK command asks that ANYPROG.OBJ be combined with object code from the library IOMOD.OBJ. The RDLINE procedure is defined in that code library, so the final executable ANYPROG.EXE has everything defined (all symbols resolved) and will run.


QUESTION 10:

Groan... what needless complication. Why not use MS Visual Basic and avoid all that...?


ANSWERS:

  1. Good idea. All these ideas are old fashioned anyway.
  2. Great idea. Nobody but assembly programmers use macros.
  3. Fantastic Idea!! All programming can be done with a mouse.
  4. Perhaps all these things are happening with Visual Basic, just hidden behind scenes.