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

144

Chapter 5. Assembler Macros

 

 

Repeating Blocks

Ax51 provides the ability to repeat a block of text within a macro. The REPT, IRP, and IRPC directives are used to specify text to repeat within a macro. Each of these directives must be terminated with an ENDM directive.

REPT

The REPT directive repeats a block of text a fixed number of times. The following macro:

 

 

DELAY

MACRO

 

;macro definition

 

 

 

REPT

5

;insert 5 NOP instructions

 

 

 

NOP

 

 

 

 

 

ENDM

 

;end REPT block

 

 

 

ENDM

 

;end macro definition

 

 

inserts 5 NOP instructions when it is invoked.

 

 

 

Example

 

 

 

 

 

 

 

 

 

5

 

NOP

 

 

 

 

NOP

 

 

 

 

NOP

 

 

 

 

NOP

 

 

 

 

NOP

 

 

 

 

 

 

 

 

 

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

145

 

 

IRP

The IRP directive repeats a block once for each argument in a specified list. A specified parameter in the text block is replaced by each argument. The following macro:

CLRREGS

MACRO

 

; macro definition

 

IRP

RNUM,

<R0,R1,R2,R3,R4,R5,R6,R7>

 

 

MOV

RNUM, #0

 

ENDM

 

; end IRP

 

ENDM

 

; end MACRO

replaces the argument

RNUM

with R0, R1, R2, … R7.

It generates the following code when invoked:

MOV

R0, #0

MOV

R1, #0

MOV

R2, #0

MOV

R3, #0

MOV

R4, #0

MOV

R5, #0

MOV

R6, #0

MOV

R7, #0

5

146

Chapter 5. Assembler Macros

 

 

IRPC

The IRPC directive repeats a block once for each character in the specified argument. A specified parameter in the text block is replaced by each character. The following macro:

DEBUGOUT

MACRO

 

; macro definition

 

IRPC

CHR, <TEST>

 

 

JNB

TI, $

; wait for xmitter

 

CLR

TI

 

 

MOV

A,#'CHR'

 

 

MOV

SBUF,A

; xmit CHR

 

ENDM

 

; end IRPC

 

ENDM

 

; end MACRO

replaces the argument CHR with the characters T, E, S, and T and generates the following code when invoked:

 

 

JNB

TI, $

; WAIT FOR XMITTER

 

 

CLR

TI

 

 

 

MOV

A,#'T'

 

 

 

MOV

SBUF,A

; XMIT T

 

 

JNB

TI, $

; WAIT FOR XMITTER

 

 

CLR

TI

 

 

 

MOV

A,#'E'

 

 

 

MOV

SBUF,A

; XMIT E

5

 

JNB

TI, $

; WAIT FOR XMITTER

 

CLR

TI

 

 

MOV

A,#'S'

 

 

MOV

SBUF,A

; XMIT S

 

 

JNB

TI, $

; WAIT FOR XMITTER

 

 

CLR

TI

 

 

 

MOV

A,#'T'

 

 

 

MOV

SBUF,A

; XMIT T

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

147

 

 

Nested Definitions

Macro definitions can be nested up to nine levels deep.

Example

L1

MACRO

 

 

LOCAL

L2

 

L2

MACRO

 

 

INC R0

 

 

ENDM

 

MOV

R0, #0

 

L2

 

 

ENDM

 

The macro L2 is defined within the macro definition of L1. Since the LOCAL directive is used to define L2 as a local symbol, it is not visible outside L1. If you want to use L2 outside of L1, exclude L2 from the LOCAL directive symbol list.

Invocation of the L1 macro generates the following:

MOV

R0, #0

 

 

 

 

INC

R0

 

 

 

 

 

 

 

 

 

 

Nested Repeating Blocks

 

 

5

 

 

 

You can also nest repeating blocks, specified with the REPT, IRP, and IRPC

directives.

 

 

 

 

Example

 

 

 

 

 

 

 

 

PORTOUT

MACRO

 

; macro definition

 

 

IRPC

CHR, <Hello>

 

 

 

 

REPT

4

; wait for 4 cycles

 

 

NOP

 

 

 

 

 

ENDM

 

; end REPT

 

 

MOV

A,#'CHR'

 

 

 

 

MOV

P0,A

; write CHR to P0

 

 

ENDM

 

; end IRPC

 

 

ENDM

 

; end MACRO

 

This macro nests a REPT block within an IRPC block.

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