Conditional Assembly and Portability Macros
Conditional Assembly and Portability Macros
macros
8-bit
Features Microcontrollers
• Increased portability
• Easier Code Writing
• Simplified I/O Register Access
• Improved Assembly Status Feedback Application Note
1 Introduction
This application note describes the Conditional Assembly feature present in the
AVR Assembler® version 1.74 and later. The AVR assembler is by default installed
with AVR Studio®, available from the AVR section of the Atmel web site.
Examples of how to use Conditional Assembly are presented. One of the examples
is a set of macros that will enable the software writer to write a generic code that
will assemble to any AVR® without other modifications than changing the device
definition file.
Rev. 2550E-AVR-04/08
2 Theory of operation
Conditional Assembly (CA) was introduced in the AVR assembler in version 1.74. CA
is based on a series of directives similar to the preprocessor directives available in C.
Since version 2.0, AVR assembler also has a preprocessor modeled after the C
preprocessor. This allows for, e.g., C header files for devices to be used.
The difference between the regular preprocessor and the CA directives is that the
latter are evaluated at compile time, and may therefore be used as a means of
overloading macros. The regular preprocessor may be used for, e.g., configuration
and selecting device dependent code, although this can be done with the CA
directives as well. (This application note deals with CA directives only.)
Please see AVR Studio help on Assembler for a complete list of directives.
2 AVR001
2550E-AVR-04/08
AVR001
The directives can be nested in up to 5 levels. CA directives can be combined with
the .MACRO directive to make macros assemble differently depending on the
enclosed CA directives. This is used in the example code for this application note.
.ifdef ATmega128
.message "UART Module assembled for ATmega128."
.if UART == 0
.message "UART0 used."
sbi DDRE, PE1 ;Configure TxD as output
.elif UART == 1
.message "UART0 used."
sbi DDRD, PD3 ;Configure TxD as output
.else
.error "UART number not specified"
.endif
.elif ATmega16
.message "UART Module assembled for ATmega128."
3
2550E-AVR-04/08
.if UART == 0
.message "UART0 used."
sbi DDRD, PD1 ;Configure TxD as output
.else
.error "UART number not specified"
.endif
.endif
As seen from the code example, a device specific initialization of (e.g. a UART) can
be contained in one file. This allows for better control of the code modules reused.
The reason for using these macros to access I/0 space (and extended I/0 space) is
that the code writer need not consider where in the I/0 space the accessed registers
are located. This would be required if the macros were not used as not all instructions
reach all addresses in the I/0 space. The advantages are therefore numerous:
• The author doesn't need to know the I/0 map, just the names of the registers.
• The standard definition files for register and bit names can be used.
• The most code size efficient instructions are used to access a register.
• The assembly code can be ported to any device without modifying the code.
4 AVR001
2550E-AVR-04/08
AVR001
Three arguments are specified for the SETB macro: a destination address, a bit mask
and a register. The register is only used if the address is higher than 0x001F, but it is
recommended to specify it anyway to ensure correct assembly and best portability
opportunities.
The range of the Bit mask is verified to be between 0 and 7, if this condition is
violated an error is issued using the .error directive.
If the address is below 0x1F the SBI instruction is used to set the bit. If the Address is
between 0x1F and 0x3F IN and OUT instruction is used to access the address.
Finally, if the address is above 0x3F the LDS and STS instructions are used.
Figure 2-1 shows how the assembler handles the SETB macro using CA.
SETB macro
No
Yes in ...
Arg 0 >0x1F? sbr ...
out ...
No
sbi ...
End macro
2.5 Improvements
At the expense of one of the registers Y or Z the LOAD and STORE macros could be
improved to execute faster and be more code compact: If one of these registers are
reserved for indirect access the STS and LDS instructions could be replaced by STD
and LDD. As this is a constraint to the code writer this has not been added in the
present implementation.
5
2550E-AVR-04/08
Disclaimer
Headquarters International
Atmel Corporation Atmel Asia Atmel Europe Atmel Japan
2325 Orchard Parkway Room 1219 Le Krebs 9F, Tonetsu Shinkawa Bldg.
San Jose, CA 95131 Chinachem Golden Plaza 8, Rue Jean-Pierre Timbaud 1-24-8 Shinkawa
USA 77 Mody Road Tsimshatsui BP 309 Chuo-ku, Tokyo 104-0033
Tel: 1(408) 441-0311 East Kowloon 78054 Saint-Quentin-en- Japan
Fax: 1(408) 487-2600 Hong Kong Yvelines Cedex Tel: (81) 3-3523-3551
Tel: (852) 2721-9778 France Fax: (81) 3-3523-7581
Fax: (852) 2722-1369 Tel: (33) 1-30-60-70-00
Fax: (33) 1-30-60-71-11
Product Contact
Literature Request
www.atmel.com/literature
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any
intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND
CONDITIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED
OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS,
BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the
contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any
commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in,
automotive applications. Atmel’s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.
© 2008 Atmel Corporation. All rights reserved. Atmel®, logo and combinations thereof AVR®, and others, are the registered trademarks or
trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.
2550E-AVR-04/08