we will be using a stand-alone macro processor called m4 for this course. Essentially it is a UNIX filter program that copies its input to its output, checking all alphanumeric tokens to see if they are macro definitions or expansions. Macros may be defined using the define macro. This takes two arguments, the macro name and the text of the definition of the macro. Later in the processing of the input, if the macro name appears in the text it is replaced by the definition. Macros may make use of up to nine arguments, using a $n notation similar to that used in UNIX shell scripts. For example, a macro to define an assembler constant and an example of its use are as follows:
define(const, $1 = $2)
When passed through m4 the result would be:
a2 = 7
The arguments to a macro are themselves checked to see if they are macros and will be expanded before being substituted for the formal arguments. In addition, macro definitions may be quoted to prevent them from being expanded when the macro is defined but only when it is expanded. This is rather unusual as it uses the open and close single quotes (e.g. ‘hello’—note that the open quote character on most computer keyboards often looks like an accent: `).
To run the macro processor we would typically do something along the following lines:
$ m4 prog.m > prog.s
$ gcc -g prog.s -o prog
Note the convention we use that assembler files containing macro definitions and expansions are given a.m suffix. We will see more of the uses of m4 as we proceed through the course.