AVR Assembler Directives
AVR Assembler Directives
4.3 Assembler The Assembler works on source files containing instruction mnemonics, labels and
source directives. The instruction mnemonics and the directives often take operands.
Code lines should be limited to 120 characters.
Every input line can be preceded by a label, which is an alphanumeric string terminated
by a colon. Labels are used as targets for jump and branch instructions and as variable
names in Program memory and RAM.
An input line may take one of the four following forms:
1. [label:] directive [operands] [Comment]
2. [label:] instruction [operands] [Comment]
3. Comment
4. Empty line
A comment has the following form:
; [Text]
Items placed in braces are optional. The text between the comment-delimiter (;) and the
end of line (EOL) is ignored by the Assembler. Labels, instructions and directives are
described in more detail later.
Examples:
label: .EQU var1=100 ; Set var1 to 100 (Directive)
.EQU var2=200 ; Set var2 to 200
test: rjmp test ; Infinite loop (Instruction)
; Pure comment line
; Another comment line
Note: There are no restrictions with respect to column placement of labels, directives,
comments or instructions.
4.5 Assembler The Assembler supports a number of directives. The directives are not translated
directives directly into opcodes. Instead, they are used to adjust the location of the program in
memory, define macros, initialize memory and so on. An overview of the directives is
given in the following table.
Summary of directives:
Directive Description
BYTE Reserve byte to a variable
CSEG Code Segment
DB Define constant byte(s)
DEF Define a symbolic name on a register
DEVICE Define which device to assemble for
DSEG Data Segment
DW Define constant word(s)
ENDMACRO End macro
EQU Set a symbol equal to an expression
ESEG EEPROM Segment
EXIT Exit from file
INCLUDE Read source from another file
LIST Turn listfile generation on
LISTMAC Turn macro expansion on
MACRO Begin macro
NOLIST Turn listfile generation off
ORG Set program origin
SET Set a symbol to an expression
4.5.1 BYTE - Reserve The BYTE directive reserves memory resources in the SRAM. In order to be able to
bytes to a variable refer to the reserved location, the BYTE directive should be preceded by a label. The
directive takes one parameter, which is the number of bytes to reserve. The directive
can only be used within a Data Segment (see directives CSEG, DSEG and ESEG).
Note that a parameter must be given. The allocated bytes are not initialized.
Syntax:
LABEL: .BYTE expression
Example:
.DSEG
var1: .BYTE 1 ; reserve 1 byte to var1
table: .BYTE tab_size ; reserve tab_size bytes
.CSEG
ldi r30,low(var1) ; Load Z register low
ldi r31,high(var1) ; Load Z register high
ld r1,Z ; Load VAR1 into register 1
4.5.2 CSEG - Code The CSEG directive defines the start of a Code Segment. An Assembler file can consist
Segment of several Code Segments, which are concatenated into one Code Segment when
assembled. The BYTE directive can not be used within a Code Segment. The default
segment type is Code. The Code Segments have their own location counter which is a
word counter. The ORG directive (see description later in this document) can be used to
place code and constants at specific locations in the Program memory. The directive
does not take any parameters.
Syntax:
.CSEG
Example:
.DSEG ; Start data segment
vartab: .BYTE 4 ; Reserve 4 bytes in SRAM
.CSEG ; Start code segment
const: .DW 2 ; Write 0x0002 in prog.mem.
mov r1,r0 ; Do something
4.5.3 DB-Define constant The DB directive reserves memory resources in the program memory or the EEPROM
byte(s) in program memory. In order to be able to refer to the reserved locations, the DB directive should be
memory or E2PROM preceded by a label.
memory
The DB directive takes a list of expressions, and must contain at least one expression.
The DB directive must be placed in a Code Segment or an EEPROM Segment.
The expression list is a sequence of expressions, delimited by commas. Each expres-
sion must evaluate to a number between -128 and 255. If the expression evaluates to a
negative number, the 8 bits two's complement of the number will be placed in the pro-
gram memory or EEPROM memory location.
If the DB directive is used in a Code Segment and the expressionlist contains more than
one expression, the expressions are packed so that two bytes are placed in each pro-
gram memory word. If the expressionlist contains an odd number of expressions, the
last expression will be placed in a program memory word of its own, even if the next line
in the assembly code contains a DB directive.
Syntax:
LABEL: .DB expressionlist
Example:
.CSEG
consts: .DB 0, 255, 0b01010101, -128, 0xaa
.ESEG
eeconst:.DB 0xff
4.5.4 DEF - Set a symbolic The DEF directive allows the registers to be referred to through symbols. A defined sym-
name on a register bol can be used in the rest of the program to refer to the register it is assigned to. A reg-
ister can have several symbolic names attached to it. A symbol can be redefined later in
the program.
Syntax:
.DEF Symbol=Register
Example:
.DEF temp=R16
.DEF ior=R0
.CSEG
ldi temp,0xf0 ; Load 0xf0 into temp register
in ior,0x3f ; Read SREG into ior register
eor temp,ior ; Exclusive or temp and ior
4.5.5 DEVICE - Define The DEVICE directive allows the user to tell the Assembler which device the code is to
which device to be executed on. If this directive is used, a warning is issued if an instruction not sup-
assemble for ported by the specified device occurs in the code. If the size of the Code Segment or
EEPROM Segment is larger than supported by the specified device, a warning is
issued. If the DEVICE directive is not used, it is assumed that all instructions are sup-
ported and that there are no restrictions on memory sizes.
Syntax:
.DEVICE AT90S1200 | AT90S2313 | AT90S4414 | AT90S8515
Example:
.DEVICE AT90S1200 ; Use the AT90S1200
.CSEG
push r30 ; This statement will generate
; a warning since the
; specified device does not
; have this instruction
4.5.6 DSEG - Data The DSEG directive defines the start of a Data Segment. An Assembler file can consist
Segment of several Data Segments, which are concatenated into one Data Segment when
assembled. A Data Segment will normally only consist of BYTE directives (and labels).
The Data Segments have their own location counter which is a byte counter. The ORG
directive (see description later in this document) can be used to place the variables at
specific locations in the SRAM. The directive does not take any parameters.
Syntax:
.DSEG
Example:
.DSEG ; Start data segment
var1:.BYTE 1 ; reserve 1 byte to var1
table:.BYTE tab_size ; reserve tab_size bytes.
.CSEG
ldi r30,low(var1) ; Load Z register low
ldi r31,high(var1) ; Load Z register high
ld r1,Z ; Load var1 into register 1
4.5.7 DW-Define constant The DW directive reserves memory resources in the program memory or EEPROM
word(s) in program memory. In order to be able to refer to the reserved locations, the DW directive should
memory or E2PROM be preceded by a label.
memory
The DW directive takes a list of expressions, and must contain at least one expression.
The DB directive must be placed in a Code Segment or an EEPROM Segment.
The expression list is a sequence of expressions, delimited by commas. Each expres-
sion must evaluate to a number between -32768 and 65535. If the expression evaluates
to a negative number, the 16 bits two's complement of the number will be placed in the
program memory location.
Syntax:
LABEL: .DW expressionlist
Example:
.CSEG
varlist:.DW 0,0xffff,0b1001110001010101,-32768,65535
.ESEG
eevar: .DW 0xffff
4.5.8 ENDMACRO - End The ENDMACRO directive defines the end of a Macro definition. The directive does not
macro take any parameters. See the MACRO directive for more information on defining Mac-
ros.
Syntax:
.ENDMACRO
Example:
.MACRO SUBI16 ; Start macro definition
subi r16,low(@0) ; Subtract low byte
sbci r17,high(@0) ; Subtract high byte
.ENDMACRO ; End macro definition
4.5.9 EQU - Set a symbol The EQU directive assigns a value to a label. This label can then be used in later
equal to an expressions. A label assigned to a value by the EQU directive is a constant and can not
expression be changed or redefined.
Syntax:
.EQU label = expression
Example:
.EQU io_offset = 0x23
.EQU porta = io_offset + 2
.CSEG ; Start code segment
clr r2 ; Clear register 2
out porta,r2 ; Write to Port A
4.5.10 ESEG - EEPROM The ESEG directive defines the start of an EEPROM Segment. An Assembler file can
Segment consist of several EEPROM Segments, which are concatenated into one EEPROM
Segment when assembled. The BYTE directive can not be used within an EEPROM
Segment. The EEPROM Segments have their own location counter which is a byte
counter. The ORG directive (see description later in this document) can be used to
place constants at specific locations in the EEPROM memory. The directive does not
take any parameters.
Syntax:
.ESEG
Example:
.DSEG ; Start data segment
vartab: .BYTE 4 ; Reserve 4 bytes in SRAM
.ESEG
eevar: .DW 0xff0f ; Initialize one word in
; EEPROM
.CSEG ; Start code segment
const: .DW 2 ; Write 0x0002 in prog.mem.
mov r1,r0 ; Do something
4.5.11 EXIT - Exit this file The EXIT directive tells the Assembler to stop assembling the file. Normally, the Assem-
bler runs until end of file (EOF). If an EXIT directive appears in an included file, the
Assembler continues from the line following the INCLUDE directive in the file containing
the INCLUDE directive.
Syntax:
.EXIT
Example:
.EXIT ; Exit this file
4.5.12 INCLUDE - Include The INCLUDE directive tells the Assembler to start reading from a specified file. The
another file Assembler then assembles the specified file until end of file (EOF) or an EXIT directive
is encountered. An included file may itself contain INCLUDE directives.
Syntax:
.INCLUDE “filename”
Example:
; iodefs.asm:
.EQU sreg=0x3f ; Status register
.EQU sphigh=0x3e ; Stack pointer high
.EQU splow=0x3d ; Stack pointer low
; incdemo.asm
.INCLUDE “iodefs.asm” ; Include I/O definitions
in r0,sreg ; Read status register
4.5.13 LIST - Turn the The LIST directive tells the Assembler to turn listfile generation on. The Assembler gen-
listfile generation on erates a listfile which is a combination of assembly source code, addresses and
opcodes. Listfile generation is turned on by default. The directive can also be used
together with the NOLIST directive in order to only generate listfile of selected parts of
an assembly source file.
Syntax:
.LIST
Example:
.NOLIST ; Disable listfile generation
.INCLUDE “macro.inc” ; The included files will not
.INCLUDE “const.def” ; be shown in the listfile
.LIST ; Reenable listfile generation
4.5.14 LISTMAC - Turn The LISTMAC directive tells the Assembler that when a macro is called, the expansion
macro expansion on of the macro is to be shown on the listfile generated by the Assembler. The default is
that only the macro-call with parameters is shown in the listfile.
Syntax:
.LISTMAC
Example:
.MACRO MACX ; Define an example macro
add r0,@0 ; Do something
eor r1,@1 ; Do something
.ENDMACRO ; End macro definition
.LISTMAC ; Enable macro expansion
MACX r2,r1 ; Call macro, show expansion
4.5.15 MACRO - Begin The MACRO directive tells the Assembler that this is the start of a Macro. The MACRO
macro directive takes the Macro name as parameter. When the name of the Macro is written
later in the program, the Macro definition is expanded at the place it was used. A Macro
can take up to 10 parameters. These parameters are referred to as @0-@9 within the
Macro definition. When issuing a Macro call, the parameters are given as a comma sep-
arated list. The Macro definition is terminated by an ENDMACRO directive.
By default, only the call to the Macro is shown on the listfile generated by the Assem-
bler. In order to include the macro expansion in the listfile, a LISTMAC directive must be
used. A macro is marked with a + in the opcode field of the listfile.
Syntax:
.MACRO macroname
Example:
.MACRO SUBI16 ; Start macro definition
subi @1,low(@0) ; Subtract low byte
sbci @2,high(@0) ; Subtract high byte
.ENDMACRO ; End macro definition
.CSEG ; Start code segment
SUBI16 0x1234,r16,r17; Sub.0x1234 from r17:r16
4.5.16 NOLIST - Turn listfile The NOLIST directive tells the Assembler to turn listfile generation off. The Assembler
generation off normally generates a listfile which is a combination of assembly source code, addresses
and opcodes. Listfile generation is turned on by default, but can be disabled by using
this directive. The directive can also be used together with the LIST directive in order to
only generate listfile of selected parts of an assembly source file.
Syntax:
.NOLIST ; Enable listfile generation
Example:
.NOLIST ; Disable listfile generation
.INCLUDE “macro.inc” ; The included files will not
.INCLUDE “const.def” ; be shown in the listfile
.LIST ; Reenable listfile generation
4.5.17 ORG - Set program The ORG directive sets the location counter to an absolute value. The value to set is
origin given as a parameter. If an ORG directive is given within a Data Segment, then it is the
SRAM location counter which is set, if the directive is given within a Code Segment,
then it is the Program memory counter which is set and if the directive is given within an
EEPROM Segment, then it is the EEPROM location counter which is set. If the directive
is preceded by a label (on the same source code line), the label will be given the value
of the parameter. The default values of the Code and EEPROM location counters are
zero, whereas the default value of the SRAM location counter is 32 (due to the registers
occupying addresses 0-31) when the assembling is started. Note that the EEPROM and
SRAM location counters count bytes whereas the Program memory location counter
counts words.
Syntax:
.ORG expression
Example:
.DSEG ; Start data segment
.ORG 0x67 ; Set SRAM address to hex 67
variable:.BYTE 1 ; Reserve a byte at SRAM
; adr.67H
.ESEG ; Start EEPROM Segment
.ORG 0x20 ; Set EEPROM location
; counter
eevar: .DW 0xfeff ; Initialize one word
.CSEG
.ORG 0x10 ; Set Program Counter to hex
; 10
mov r0,r1 ; Do something
4.5.18 SET - Set a symbol The SET directive assigns a value to a label. This label can then be used in later expres-
equal to an sions. A label assigned to a value by the SET directive can be changed later in the pro-
expression gram.
Syntax:
.SET label = expression
Example:
.SET io_offset = 0x23
.SET porta = io_offset + 2
.CSEG ; Start code segment
clr r2 ; Clear register 2
out porta,r2 ; Write to Port A
4.6 Expressions The Assembler incorporates expressions. Expressions can consist of operands, opera-
tors and functions. All expressions are internally 32 bits.
4.6.1 Operands The following operands can be used:
■ User defined labels which are given the value of the location counter at the place they
appear.
■ User defined variables defined by the SET directive
■ User defined constants defined by the EQU directive
■ Integer constants: constants can be given in several formats, including
4.6.3 Operators The Assembler supports a number of operators which are described here. The higher
the precedence, the higher the priority. Expressions may be enclosed in parentheses,
and such expressions are always evaluated before combined with anything outside the
parentheses.