Macros. NXP CodeWarrior Development Studio
Annonce
Annonce
Macros
.size symbol, expression
Parameters
symbol
Symbol name.
expression
Number of bytes.
12.2.9.5 type
Specifies the type of a symbol.
.type symbol, @function | @object
Parameters
symbol
Symbol name.
@function
Function type specifier.
@object
Variable specifier.
12.3 Macros
This chapter explains how to define and use macros. You can use the same macro language regardless of your target processor.
This chapter includes these topics:
•
•
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
202 Freescale Semiconductor, Inc.
Chapter 12 Assembler
12.3.1 Defining Macros
This section explains how to define macros.
•
•
•
Creating Unique Labels and Equates
•
Referring to the Number of Arguments
12.3.1.1 Macro Definition Syntax
A macro definition is one or more assembly statements that define:
• the name of a macro
• the format of the macro call
• the assembly statements of the macro
To define a macro, use either the
,macro
or the
#define
directive.
NOTE
If you use a local label in a macro, the scope of the label is limited to the expansion of the macro. (Local labels begin with the @ character.)
12.3.1.1.1 Using the .macro Directive
The
.macro
directive is part of the first line of a macro definition. Every macro definition ends with the
.endm
directive . Listing 12-3 on page 203
shows the full syntax, and Table
12-13 explains the syntax elements.
Listing 12-3. Macro Definition Syntax: .macro Directive
name: .macro [
parameter ] [ ,
parameter ] ...
macro_body
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
Freescale Semiconductor, Inc.
203
Macros
.endm
Table 12-13. Syntax Elements: .macro Directive
name parameter macro_body
Element Description
Label that invokes the macro.
Operand the assembler passes to the macro for us in the macro body.
One or more assembly language statements. Invoking the macro tell the assembler to substitutes these statements.
The body of a simple macro consists of just one or two statements for the assembler to execute. Then, in response to the
.endm
directive, the assembler resumes program execution at the statement immediately after the macro call.
But not all macros are so simple. For example, a macro can contain a conditional assembly block, The conditional test could lead to the
.mexit
directive stopping execution early, before it reaches the
.endm
directive.
is the definition of macro addto
, which includes an
.mexit
directive.
shows the assembly-language code that calls the addto
macro.
Listing 12-6 on page 205 shows the expanded
addto
macro calls.
Listing 12-4. Conditional Macro Definition
//define a macro addto: .macro dest,val
.if val==0
nop
.elseif val >= -32768 && val <= 32767
addi dest,dest,val // use compact instruction
.else
addi dest,dest,val@l // use 32-bit add
addis dest,dest,val@ha
.endif
// end macro definition
.endm
Listing 12-5. Assembly Code that Calls addto Macro
// specify an executable code section
.text
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
204 Freescale Semiconductor, Inc.
Chapter 12 Assembler
li r3,0
// call the addto macro addto r3,0 addto r3,1 addto r3,2 addto r3,0x12345678
Listing 12-6. Expanded addto Macro Calls
li r3,0 nop addi r3,r3,1 addi r3,r3,2 addi r3,r3,0x12345678@l addis r3,r3,0x12345678@ha
12.3.1.2 Using Macro Arguments
You can refer to parameters directly by name. Listing 12-7 on page 205
shows the setup macro, which moves an integer into a register and branches to the label
_final_setup
.
shows a way to invoke the setup
macro., and Listing 12-9 on page 205
shows how the assembler expands the setup
macro.
Listing 12-7. Setup Macro Definition
setup: .macro name
li r3,name
bl _final_setup
.endm
Listing 12-8. Calling Setup Macro
VECT: .equ 0
setup VECT
Listing 12-9. Expanding Setup Macro
li r3,VECT
bl _final_setup
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
Freescale Semiconductor, Inc.
205
Macros
If you refer to named macro parameters in the macro body, you can precede or follow the macro parameter with
&&
. This lets you embed the parameter in a string. For example,
Listing 12-10 on page 206 shows the
smallnum
macro, which creates a small float by appending the string
E-20
to the macro argument. Listing 12-11 on page 206
shows a way to invoke the smallnum
macro, and
shows how the assembler expands the smallnum
macro.
Listing 12-10. Smallnum Macro Definition
smallnum: .macro mantissa
.float mantissa&&E-20
endm
Listing 12-11. Invoking Smallnum Macro
smallnum 10
Listing 12-12. Expanding Smallnum Macro
.float 10E-20
12.3.1.3 Creating Unique Labels and Equates
Use the backslash and at characters (\@) to have the assembler generate unique labels and equates within a macro. Each time you invoke the macro, the assembler generates a unique symbol of the form
??
nnnn
, such as
??0001
or
??0002
.
In your code, you refer to such unique labels and equates just as you do for regular labels and equates. But each time you invoke the macro, the assembler replaces the \@ sequence with a unique numeric string and increments the string value.
Listing 12-13 on page 206 shows a macro that uses unique labels and equates.
12-14 on page 207 shows two calls to the
putstr
macro.
shows the expanded code after the two calls.
Listing 12-13. Unique Label Macro Definition
putstr: .macro string
lis r3,(str\@)@h
oris r3,r3,(str\@)@l
bl put_string
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
206 Freescale Semiconductor, Inc.
Chapter 12 Assembler
b skip\@ str\@: .asciz string
.align 4 skip\@:
.endm
Listing 12-14. Invoking putstr Macro
putstr 'SuperSoft Version 1.3' putstr 'Initializing...'
Listing 12-15. Expanding putstr Calls
lis r3,(str??0000)@h
oris r3,r3,(str??0000)@l
bl put_string
b skip??0000
str??0000: .asciz 'SuperSoft Version
.align 4 skip??0000:
lis r3,(str??0001)@h
oris r3,r3,(str??0001)@l
bl put_string
b skip??0001
str??0001: .asciz 'Initializing...'
.align 4 skip??0001:
12.3.1.4 Referring to the Number of Arguments
To refer to the number of non-null arguments passed to a macro, use the special symbol narg
. You can use this symbol during macro expansion.
12.3.2 Invoking Macros
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
Freescale Semiconductor, Inc.
207
Macros
To invoke a macro, use its name in your assembler listing, separating parameters with commas. To pass a parameter that includes a comma, enclose the parameter in angle brackets.
For example, Listing 12-16 on page 208 shows macro
pattern
, which repeats a pattern of
bytes passed to it the number of times specified in the macro call. Listing 12-17 on page
208 shows a statement that calls
pattern
, passing a parameter that includes a comma.
Listing 12-18 on page 208 is another example calling statement; the assembler generates
the same code in response to the calling statement of either
Listing 12-16. Pattern Macro Definition
pattern: .macro times,bytes
.rept times
.byte bytes
.endr
.endm
Listing 12-17. Macro Argument with Commas
.data
halfgrey: pattern 4,<0xAA,0x55>
Listing 12-18. Alternate Byte-Pattern Method
halfgrey: .byte 0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55
12.3.2.1 Using the #define Directive
Another way to define a macro is to use the #define directive. This will define a preprocessor style macro using a syntax that will be familiar to C programmers. Note that C pre-processor macros are complementary to the assembler's main
MACRO...ENDM
macro language.
Listing 12-19 on page 208 shows the full syntax, and explains the syntax elements.
Listing 12-19. Macro Definition Syntax: #define Directive
#define
name [ (
parms) ]
assembly_statement [ ; ] [ \ ]
assembly_statement [ ; ] [ \ ]
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
208 Freescale Semiconductor, Inc.
Download
Annonce