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

180 Chapter 6. Macro Processing Language

Conditional MPL Processing

Some MPL functions accept logical expressions as arguments. The MPL uses the value 1 and 0 to determine TRUE or FALSE. If the value is one, then the expression is TRUE. If the value is zero, then the expression is FALSE.

Typically, you will use either the relational operators (EQ, NE, LE, LT, GT, or GE) or the string comparison functions (EQS, NES, LES, LTS, GTS, or GES) to specify a logical value.

IF Function

The IF MPL function evaluates a logical expression, and based on that expression, expands or skips its text arguments. The syntax of the MPL processor function IF is:

%IF (expression) THEN (balanced-text1) [ ELSE (balanced-text2) ] FI

IF first evaluates the expression, if it is TRUE, then balanced-text1 is expanded; if it is FALSE and the optional ELSE clause is included, then balanced-text2 is expanded. If it is FALSE and the ELSE clause is not included, the IF call returns a null string. FI must be included to terminate the call.

 

 

IF calls can be nested; when they are, the ELSE clause refers to the most recent

6

 

IF call that is still open (not terminated by FI). FI terminates the most recent IF

 

call that is still open.

 

 

Source text

 

 

 

 

 

 

 

 

 

 

 

%*DEFINE (ADDSUB (op,p1,p2))

(

 

 

%IF (%EQS (%op,ADD)) THEN

(

 

 

ADD

%p1,%p2

 

 

 

)ELSE (%IF (%EQS (%op,SUB)) THEN (

 

 

SUB

%p1,%p2

 

 

 

) FI

 

 

 

 

) FI

 

 

 

 

)

 

 

 

 

%ADDSUB (ADD,R15,R3)

%' generate ADD R15,R3'

 

 

%ADDSUB (SUB,R15,R9)

%' generate SUB R15,R9'

 

 

%ADDSUB (MUL,R15,R4)

%' generates nothing !'

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

181

 

 

Output text

ADD R15,R3

SUB R15,R9

WHILE Function

Often you may wish to perform macro operations until a certain condition is met. The MPL processor function WHILE provides this facility.

The syntax for the MPL processor function WHILE is:

%WHILE (expression) (balanced-text)

WHILE first evaluates the expression. If it is TRUE, then the balanced-text is expanded; otherwise, it is not. Once the balanced-text has been expanded, the logical argument is retested and if it is still TRUE, then the balanced-text is again expanded. This loop continues until the logical argument proves FALSE.

Since the MPL continues processing until expression evaluates to FALSE, the balanced-text should modify the expression, or the WHILE may never terminate.

A call to the MPL processor function EXIT will always terminate a WHILE function. EXIT is described later.

Source text

 

 

 

 

 

6

%SET (count, 5)

 

%' initialize count to 5'

 

)

 

0)

 

 

%WHILE (%count GT

 

 

 

(

ADD R15,R15

%SET (count, %count - 1)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output text

ADD R15,R15

ADD R15,R15

ADD R15,R15

ADD R15,R15

ADD R15,R15

182

Chapter 6. Macro Processing Language

 

 

REPEAT Function

The MPL processor function REPEAT expands its balanced-text a specified number of times. The syntax for the MPL processor function REPEAT is:

%REPEAT (expression) (balanced-test)

REPEAT uses the expression for a numerical value that specifies the number of times the balanced-text will be expanded. The expression is evaluated once when the macro is first called, then the specified number of iterations is performed.

Source text

%REPEAT (5)

( -enter any key to shut down-

)

%REPEAT (5) (+%REPEAT (9) (-))+

Output text

-enter any key to shut down- -enter any key to shut down- -enter any key to shut down- -enter any key to shut down- -enter any key to shut down-

6

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

183

 

 

EXIT Function

The EXIT MPL processor function terminates expansion of the most recently called REPEAT, WHILE or user-defined macro function. It is most commonly used to avoid infinite loops (example: a WHILE that never becomes FALSE, or a recursive user-defined macro that never terminates). It allows several exit points in the same macro.

The syntax for the MPL processor function EXIT is:

%EXIT

Source text

%SET (count, 0)

%WHILE (1)

(%IF (%count GT 5) THEN (%EXIT) FI DW %count, -%count

%SET (count, %count + 1))

Output text

DW 0, -0

DW 1, -1

DW 2, -2

DW 3, -3

DW 4, -4

DW 5, -5

6

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