Hca.geocities.com/JLABELLE@rogers.com/nsasm/docs/c866.htmlca.geocities.com/JLABELLE_rogers.com/nsasm/docs/c866.htmldelayedxmJ\"OKtext/html@'\"b.HTue, 07 Dec 2004 00:49:42 GMT4Mozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)en, *mJ\"Macros

Chapter 6. Macros

Table of Contents
defining a macro
calling a macro
using parameters

Macros simplify the assembly process. Duplicative or similar sequences of assembly language statements can be inserted into the program source code instead of manually entering them into the program each time they are required. Once defined, a macro will automatically, during assembly time, place repetitive code or similar code with changed parameters into the assembler source code when called by its macro name. The following sections explain the process of defining and calling macros, with and without parameters, and describe how to use assembler directives related to macro generation.

Using macros, a programmer can gradually build a library of basic routines. Variables unique to particular programming applications can be defined in and passed to a particular macro when called by main programs. Such macros can be automatically included in the assembly source code using the .incld directive.

defining a macro

The process of defining a macro involves preparing statements that perform the following functions:

Macros must be defined before they are used in a program. Macro definitions within an assembly do not generate code. Code is generated only when macros are called by the main program. Macro definitions are formed as follows:


	.macro mname[,parameters]
		.
		.
		.
		macro body
		.
		.
		.
	.endm
	

.macro is the directive mnemonic that initiates the macro definition. It must be terminated by a blank.

The mname is the name of the macro. It is legal to define a macro with the same name as an already existing macro, in which case the latest definition is used. Previous definitions are, however, retained in the macro definition table; if the existing macro is deleted by the .mdel directive, the previous definition becomes active. If mname is the same name as an instruction mnemonic, the macro definition is used in place of the normal instruction assembly.

The main program calls the macro using the macro name. The name must adhere to the rules given for symbols in the Section called symbol and label construction in Chapter 3.

Parameters is the optional list of parameters used in the macro definition. Each parameter must adhere to symbol rules in the Section called symbol and label construction in Chapter 3. Parameters are delimited from mname and successive parameters by commas.

The following are examples of legal and illegal .macro directives:

legalillegalreason illegal
.macro MAC,A,B.macro SUB,*ABspecial character used
.macro $ADD,OPT1,OPT1.macro 1MACfirst character is numeric

The macro body is a sequence of assembly language statements and may consist of simple text, text with parameters, and/or macro-time operators.

The .endm signifies the end of the macro and must be used to terminate macro definitions.

simple macros

The simplest form of macro definition is one with no parameters or macro operators. The macro body is simply a sequence of assembly language statements which are substituted for each macro call. These identical macro calls are inefficient if called repetitively within the same assembly program; a repeatedly used series of assembly language statements within a program should be coded as a subroutine. However, simple macros with no variables are useful in compiling a library of basic routines to be used within different programs. They allow the programmer to simply call the macro within the program rather then to repeatedly code all the macro body statements into each program when needed.

			; MACRO "IND1" stores one indirectly into
			; memory using B register
	.macro IND1     ; begin macro definition
		LD A,#1
		X A,[B]
	.endm
	

macros with parameters

The previous macro could be made more flexible by adding parameters to the macro definition. Parameters allow the programmer to specify what is being loaded and stored.

				; MACRO "LOAD" loads DEST with
				; SOURCE
	.macro LOAD,DEST,SOURCE ; DEST is destination,
				; SOURCE is source
		LD A,SOURCE
		X A,DEST
	.endm