9 using macros, 1 test-and-branch macro example, Using macros -48 – ARM VERSION 1.2 User Manual

Page 60

Advertising
background image

Writing ARM and Thumb Assembly Language

2-48

Copyright © 2000, 2001 ARM Limited. All rights reserved.

ARM DUI 0068B

2.9

Using macros

A macro definition is a block of code enclosed between

MACRO

and

MEND

directives. It

defines a name that can be used instead of repeating the whole block of code. This has
two main uses:

to make it easier to follow the logic of the source code, by replacing a block of
code with a single, meaningful name

to avoid repeating a block of code several times.

Refer to MACRO and MEND on page 7-27 for more details.

2.9.1

Test-and-branch macro example

A test-and-branch operation requires two ARM instructions to implement.

You can define a macro definition such as this:

MACRO
$label TestAndBranch $dest, $reg, $cc

$label CMP $reg, #0
B$cc $dest
MEND

The line after the

MACRO

directive is the macro prototype statement. The macro prototype

statement defines the name (

TestAndBranch

) you use to invoke the macro. It also defines

parameters (

$label

,

$dest

,

$reg

, and

$cc

). You must give values to the parameters when

you invoke the macro. The assembler substitutes the values you give into the code.

This macro can be invoked as follows:

test TestAndBranch NonZero, r0, NE
...
...
NonZero

After substitution this becomes:

test CMP r0, #0
BNE NonZero
...
...
NonZero

Advertising