Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
69
Добавлен:
14.04.2015
Размер:
2.03 Mб
Скачать

140

Chapter 5. Assembler Macros

 

 

Defining a Macro

Macros must be defined in the program before they can be used. A macro definition begins with the MACRO directive which declares the name of the macro as well as the formal parameters. The macro definition must be terminated with the ENDM directive. The text between the MACRO and ENDM directives is called the macro body.

Example

WAIT

MACRO

X

; macro definition

 

REPT

X

; generate X NOP instructions

 

NOP

 

 

 

ENDM

 

; end REPT

 

ENDM

 

; end MACRO

 

 

In this example,

WAIT is the name of the macro and X is the only formal

 

 

parameter.

 

 

 

 

 

In addition to the ENDM directive, the EXITM directive can be used to

 

 

immediately terminate a macro expansion. When an EXITM directive is

 

 

detected, the macro processor stops expanding the current macro and resumes

 

 

processing after the next ENDM directive. The EXITM directive is useful in

5

 

Exampleconditional statements.

 

 

 

 

 

 

 

 

 

 

WAIT

MACRO

X

; macro definition

 

 

 

IF NUL X

 

; make sure X has a value

 

 

 

EXITM

 

; if not then exit

 

 

 

ENDIF

 

 

 

 

 

REPT

X

; generate X NOP instructions

 

 

 

NOP

 

 

 

 

 

ENDM

 

; end REPT

 

 

 

ENDM

 

; end MACRO

Keil Software — A51/AX51/A251 Macro Assembler and Utilities

141

 

 

Parameters

Up to 16 parameters can be passed to a macro in the invocation line. Formal parameter names must be defined using the MACRO directive.

Example

MNAME MACRO P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16

defines a macro with 16 parameters. Parameters must be separated by commas in both the macro definition and invocation. The invocation line for the above macro would appear as follows:

MNAME A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P

where A, B, C, … O, P are parameters that correspond to the format parameter

names P1, P2, P3, … P15, P16.

Null parameters can be passed to a macro. Null parameters have the value NULL and can be tested for using the NUL operator described later in this chapter. If a parameter is omitted from the parameter list in the macro invocation, that parameter is assigned a value of NULL.

Example

 

 

5

 

 

 

MNAME A,,C,,E,,G,,I,,K,,M,,O,

 

 

P2, P4, P6, P8, P10, P12, P14, and

P16 will all be assigned the value NULL

 

 

when the macro is invoked. You should note that there are no spaces between the comma separators in the above invocation line. A space has an ASCII value of 20h and is not equivalent to a NULL.

142

Chapter 5. Assembler Macros

 

 

Labels

You can use labels within a macro definition. By default, labels used in a macro are global and if the macro is used more than once in a module, Ax51 will generate an error.

Example

LOC OBJ

LINE

SOURCE

 

 

 

1

GLABEL

MACRO

 

 

2

LOOP:

NOP

 

 

3

 

JMP

LOOP

 

4

 

ENDM

 

 

5

 

 

 

 

6

 

 

 

 

7

 

GLABEL

 

0000 00

8+1

LOOP:

NOP

 

0001 80FD

9+1

 

JMP

LOOP

 

10

 

GLABEL

 

 

11+1

LOOP:

NOP

 

***_________________________^

***ERROR #9, LINE #11, ATTEMPT TO DEFINE AN ALREADY DEFINED LABEL

 

0003 80FB

12+1

JMP

LOOP

 

 

13

 

 

 

 

14

 

 

 

 

15

END

 

5

Labels used in a macro should be local labels. Local labels are visible only

within the macro and will not generate errors if the macro is used multiple times

in one source file. You can define a label (or any symbol) used in a macro to be local with the LOCAL directive. Up to 16 local symbols may be defined using the LOCAL directive.

NOTE

LOCAL must be in the next line after the MACRO definition.

Keil Software — A51/AX51/A251 Macro Assembler and Utilities

143

 

 

Example

CLRMEM

MACRO

ADDR, LEN

 

LOCAL

LOOP

 

MOV

R7, #LEN

 

MOV

R0, #ADDR

 

MOV

A, #0

LOOP:

MOV

@R0, A

 

INC

R0

 

DJNZ

R7, LOOP

 

ENDM

 

In this example, the label LOOP

is local because it is defined with the LOCAL

directive. Any symbol that is not defined using the LOCAL directive will be a global symbol.

Ax51 generates an internal symbol for local symbols defined in a macro. The internal symbol has the form ??0000 and is incremented each time the macro is invoked. Therefore, local labels used in a macro are unique and will not generate errors.

5

Соседние файлы в папке HLP