Show Menu
Cheatography

AVR Programming - Part I: Assembly Cheat Sheet (DRAFT) by

This Cheat Sheet is part of the Ultimate AVR Cheat Sheet Project. This part of the project teaches the basic syntax of the Assembly language for AVRs and brings some tips and tricks on how to program AVR microcontrollers using Assembly language. Caution: All code examples were written to be compatible with AVRASM2 Assembler from Microchip Technology Incorporated (formerly Atmel Corporation). If you use a different assembler program, you must adapt the codes to your program syntax.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

The Assemby Basics (copy)

Introd­uction

This Cheat Sheet is part of the Bladab­­uska's AVR Cheat Sheet Collec­­tion. This cheat sheet shows the basic syntax of the Assembly language for AVRs. All inform­­ation in this cheat sheet is related to the AVR Assembler v2.0 from Microchip Technology Incorp­­orated (formerly Atmel Corpor­­at­ion). The Assembly language for AVR microc­ont­rollers is not case sensitive.
Note: If you use a different assembler software, you must adapt the inform­ation to your software.

The Assemby Basics

Comment

Everything between a semicolon (;) and the end of the line is a comment and is ignored by the Assembler software. Comments are primarily used for code docume­­nt­a­tion, and to disable code sections for debugging purpose.

Line contin­uation

A backslash character (\) placed at the end of a line is used to inform the prepr­­­oc­­essor and the Assembler that the command continues on the next line.

Label Field

A label is a text identifier that starts with a letter (a-z or A-Z), a question mark (?) or an underscore character (_), followed by zero or more letters, numbers, dollar signs ($), question marks (?), or underscore characters (_). Labels cannot contain spaces and must end with a colon character (:).

Integer constant

Integer constants starts by a number or a radix specifier. If an integer starts with a non-zero number character (1-9), than no radix is specified and the integer is considered to be expressed in the decimal number system. Integers in binary notation must start with "­­­0­b­", in hexade­­­cimal notation with "­­­0­x­" and in octal notation with a leading zero (0). No spaces are allowed inside the number, but underscore characters (_) can be placed inside the number to increase readab­­­i­lity.

Character constants

Character constants are characters or escape sequences enclosed in single quotes ('). They and can be used anywhere an integer expression is allowed.
sequence
name
symbol
number
\0
Null Character
NUL
0x00
\a
Alert Bell
BEL
0x07
\b
Backspace
BS
0x08
\t
Horizontal Tab
HT
0x09
\n
Line Feed "or newlin­e"
LF
0x0A
\v
Vertical Tab
VT
0x0B
\f
Form Feed
FF
0x0C
\r
Carriage Return
CR
0x0D
\\
Backslash
\
0x5C
\xHH
Hexade­cimal Number
Where H is the digit in hexade­cimal notation (0-9,A-F)
\000
Octal Number
Where o is the digit in octal notation (0-7)

String constant

A string7 is any number of characters enclosed in double quotes (") taken literally, no escape sequences are recogn­­­ized, and it is not NULL-t­­er­m­i­nated. Quoted strings may be concat­­­e­nated according to the ANSI C convention (space character) or with the backslash character (line contin­­­u­a­tion) to form long strings and multiple line-s­panning strings.
7 Strings can only be used in conjun­​ction with the DB directive or with the MESSAG​E, W​ARN​ING​ or ERROR direct­ives.

Operators

An operator is a (single or multiple) charac­ter(s) that executes an operation in one or more operands. Unary operators takes only one operand, placed after the operator. Binary operators takes two operands and are placed between then. The ternary condit­­ional operator takes three operands. Operators can be grouped with parant­hesis to receive maximum preced­ence.
Operator
Operation
Precedence3
Arithmetic Operators
+
Addition
12
-
Subtra­ction
12
-
Unary minus
14
%
Modulo4
13
*
Multip­lic­ation
13
/
Division
13
Bitwise Operators
~
Bitwise NOT
14
&
Bitwise AND
8
|
Bitwise OR
6
^
Bitwise XOR
7
<<
Shift Left
11
>>
Shift Right
11
Logic Operators
!
Logical NOT
14
&&
Logical AND
5
||
Logical OR
4
Relational Operators
<
Less than
10
>=
Less than or equal to
10
>
Greater than
10
>=
Greater than or equal to
10
==
Equal to
!=
Different from
Ternary Condit­ional Operator
? :
Ternary Condit­ional5
3
3 The higher the preced­ence, the higher the priority.
4 This operator was introduced in AVR Assembler v2.0.
5 This operator was introduced in AVR Assembler v2.1.
 

Assembler directive

Assembler directives starts with a dot (.). They are used to guide the Assembler software on how to understand or implement some part of the code. Assembly directives can be used to define macros, code segments, condit­­­ional sections and symbols, to reserve memory space for data, and to implement debug features.7
7 Refer to the Assembler Directives Table to a complete list of available Assembler direct­ives.

Function

Functions are built-in macro functions that can be used to evaluate code.
LOW(ex­pre­ssion)
Returns the low byte (bits 7-0) of an expres­sion.
HIGH(e­xpr­ession)
Returns the second byte (bits 15-8) of an expres­sion.
BYTE2(­exp­res­sion)
Returns the second byte (bits 15-8) of an expres­sion.
BYTE3(­exp­res­sion)
Returns the third byte (bits 23-16) of an expres­sion.
BYTE4(­exp­res­sion)
Returns the fourth byte (bits 31-24) of an expres­sion.
LWRD(e­xpr­esion)
Returns the low word (bits 15-0) of an expres­sion.
HWRD(e­xpr­esion)
Returns the second word (bits 31-16) of an expres­sion.
PAGE(e­xpr­ession)
Returns bits 21-16 of an expres­sion.
EXP2(e­xpr­ession)
Returns 2 to the power of expres­sion.
LOG2(e­xpr­ession)
Returns the integer part of a log2(e­xpr­ess­ion).
INT(ex­pre­ssion)
Truncates a floating point expression to integer.
FRAC(e­xpr­ession)
Extracts fractional part of a floating point expres­sion.
Q7(exp­res­sion)
Converts a fractional floating point expression to a form suitable for the multip­lic­ation instru­ctions. (Sign + 7-bit fraction)
Q15(ex­pre­ssion)
Converts a fractional floating point expression to a form suitable for the multip­lic­ation instru­ctions. (Sign + 15-bit fraction)
ABS(ex­pre­ssion)
Returns the absolute value of a constant expres­sion.
DEFINE­D(s­ymbol)
Returns 1 if symbol was previously defined (using .SET, .DEF, or .EQU direct­ives), 0 otherwise. Normally used in conjun­ction with .IF and .ELIF direct­ives. It does not require parent­heses around its argument.
STRLEN­(st­ring)
Returns the length of a string constant, in bytes.

Assembler Directives Table

Source Segments
.CSEGA
Defines the start of a CODE segment. CODE segments have their own location counter (in words), starting at 0.
.DSEGA
Defines the start of a DATA segment. DATA segments have their own location counter (in bytes), starting at the first address after the I/O registers (0x60 or 0x100, depending on the number of periph­erals).
.ESEGA
Defines the start of an EEPROM segment. EEPROM segments have their own location counter (in bytes), starting at 0.
.ORG <ad­dre­ss>
Sets the location counter (of the segment were it was pĺaced) to an absolute value.
Reserve Memory Space
.BYTELC <nu­mbe­r>
Reserves a number of BYTES in SRAM or EEPROM memories. The directive takes one parameter, which is the number of bytes to reserve.
.DBLD <li­st>
Reserves a number of BYTES in PROGRAM or EEPROM memory.
.DWLD <li­st>
Reserves a number of WORDS (2 BYTES) in PROGRAM or EEPROM memory.
.DDLD <li­st>
Reserves a number of DOUBLE­-WORDS (4 BYTES) in PROGRAM or EEPROM memory.
.DQLD <li­st>
Reserves a number of QUAD-WORDS (8 BYTES) in PROGRAM or EEPROM memory.
User Defined Symbols
.EQU <sy­mbo­l>=­<ex­pre­ssi­on>
Assigns a user defined symbol to a value or expres­sion. A symbol assigned to a value by the EQU directive is a constant and cannot be changed or redefined.
.SET <sy­mbo­l>=­<ex­pre­ssi­on>
Assigns a user defined symbol to a value or expres­sion. A symbol assigned to a value by the SET directive can be changed or redefined later in the program.
.DEF <sy­mbo­l>=­<re­gis­ter>
Assigns a user defined symbol to a register. A register can have several symbolic names attached to it. A symbol can be redefined later in the program.
.UNDEF <sy­mbo­l>
Undefine a symbol previously defined with the DEF directive. This provides a way to obtain a simple scoping of register defini­tions, avoiding warnings about register reuse.
Macro Definition
.MACRO <na­me>
Defines the start of the macro. It takes the macro name as a parameter. A macro is marked with a + in the opcode field of the listfile.
.ENDMA­CRO­/.ENDM
Defines the end of a macro defini­tion. ENDM is a synonym to ENDMACRO directive.
File Management
.INCLUDE <fi­le>
Tells the Assembler to start reading from a specified file. The Assembler then assembles the specified file until end of file (EOF) or an EXIT directive is encoun­tered.
.EXIT
Tells the Assembler to stop assembling the file. 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.
Condit­ional Assembly
.IFDEF <sy­mbo­l>
If the symbol is defined, it will include code untill the corres­ponding ELSE directive.
.IFNDEF <sy­mbo­l>
If the symbol is not defined, it will include code untill the corres­ponding ELSE directive.
.IF <ex­pre­ssi­on>
If expression is evaluated different from 0, it will include code untill the corres­ponding ELSE, ELIF or ENDIF directive.
.ELIF <ex­pre­ssi­on>
It will include code until the corres­ponding ENDIF (or the next ELIF at the same level), if the expression is true, and the initial IF clause and its following ELIF clauses (if any) are also false.
.ELSE
It will include code until the corres­ponding ENDIF, if the initial IF clause and its following ELIF clauses (if any) are also false.
.ENDIF
Defines the end for the condit­ional IF, IFDEF, or IFNDEF direct­ives.
Assembler Program Output
.MESSAGEI <st­rin­g>
Output a message string.
.WARNINGI <st­rin­g>
Outputs a warning string, but does not halt assemb­ling.
.ERRORI <st­rin­g>
Outputs an error message string and halts the assemb­ling.
Listfile Generation Control
.LIST
Turn the listfile generation ON. The listfile is a combin­ation of assembly source code, addresses, and opcodes. Listfile generation is turned on by default.
.NOLIST
Turn listfile generation OFF.
.LISTMAC
Turn macro expansion on. It tells the Assembler that when a macro is called, the expansion 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.
Special Funcitons
.CSEGSIZE = <va­lue>
This directive is used to specify the size of the program memory block at the SRAM memory. This directive can only be used with AT94K Series Field Progra­mmable System Level Integrated Circuit Devices.
.OVERLAPV
This directive is for projects with special needs and should normally not be used.
.NOOVERLAPV
This directive is for projects with special needs and should normally not be used.
A All segments of the same type will be concat­enated into one single segment of that type when assembled.
C Cannot be used inside CODE segments.
D Cannot be used inside DATA segments.
I My be used in condit­ional assembly,
L In order to be able to refer to the reserved location, the directive should be preceded by a LABEL.
V Introduced in AVR Assembler v2.1.

Prepro­cessor Directives

Prepro­cessor directives are lines whose first non-empty character is a hash symbol (#). They are used to issue commands to the prepro­­­­­c­e­­ssor.
Macro Definition
#define <na­me> [<v­alu­e>]
Define a prepro­cessor object­-like macros that basically define a constant. If value is not specified, it is set to 1.
#define <na­me(­[<a­rg>, <...>]­)> [<v­alu­e>]
Define a prepro­cessor functi­on-like macros that do parameter substi­tution. This macro must be called with the same number of arguments it is defined with. The left parent­hesis must appear immedi­ately after name (no spaces between), otherwise it will be interp­reted as part of the value of a object­-like macro. If value is not specified, it is set to 1.
#undef <na­me>
Undefine macro name previously defined with a #define directive. If name is not previously defined, the .undef directive is silently ignored.
Condit­ional Assembly
#ifdef <na­me>
All the following lines until the corres­ponding #endif, #else, or #elif are condit­ionally assembled if name is previously #defined. Shorthand for #if defined (name).
#ifndef <na­me>
All the following lines until the corres­ponding #endif, #else, or #elif are condit­ionally assembled if name is not #defined. Shorthand for #if !defined (name).
#if <ex­pre­ssi­on>
All the following lines until the corres­ponding #endif, #else, or #elif are condit­ionally assembled if expression evaluates to true (not equal to 0). Any undefined symbols used in expression are silently evaluated to 0.
#elif <ex­pre­ssi­on>
All the following lines until the corres­ponding #endif, #else, or #elif are condit­ionally assembled if expression evaluates to true (not equal to 0),and all previous #if or #elif expres­sions were evaluated to false. Any undefined symbols used in expression are silently evaluated to 0.
#else
All the following lines until the corres­ponding #endif are condit­ionally assembled if all previous #if or #elif expres­sions were evaluated to false.
#endif
Terminates a condit­ional block initiated with an #if, #ifdef, or #ifndef directive.
Prepro­cessor Program Output
#message <st­rin­g>
Outputs string to standard output, but does not affect assembler error or warning counters.
#warning <st­rin­g>
Outputs string to standard error, and increments the assembler warning counter.
#error <st­rin­g>
Outputs string to standard error, increments the assembler error counter, and prevents the program of being succes­sfully assembled.
File Management
#include "­fil­e"
Include a file, searching in the current working directory first, then searching in the built-in known place.
#include <fi­le>
Include a file, searching in the built-in known place only, unless the current working directory is explicitly specified with a dot (.) entry in the include path.
Empty Directive
#
Does nothing. The line is removed by the prepro­cessor.
Note: #pragma prepro­cessor directives will be treated in a separate topic.

Prepro­cessor Pre-de­fined Macros

__AVRA­SM_­VER­SION__
INTEGER. AVR Assembler version, encoded as (1000*­major + minor)
__CORE­_VE­RSION__
STRING. AVR core version.
__CORE­_co­rev­ers­ion__
INTEGER. AVR core version value of the coreve­rsion. For example: __CORE­_V2__
__DATE__
STRING. Build date in the format "Jun 28 2004".
__TIME__
STRING. Build time in the format: "­HH:­MM:­SS".
__CENT­URY__
INTEGER. Build time century (tipically 20).
__YEAR__
INTEGER. Build time year, less century (0-99).
__MONTH__
INTEGER. Build time month (1-12).
__DAY__
INTEGER. Build time day (1-31).
__HOUR__
INTEGER. Build time hour (0-23).
__MINUTE__
INTEGER. Build time minute (0-59).
__SECOND__
INTEGER. Build time second (0-59).
__FILE__
STRING. Sorce file name.
__LINE__
INTEGER. Current line number in source file.
__PART­_NAME__
STRING. AVR part name.
__part­name__
INTEGER. AVR part name value of the partname For example: __ATme­ga8__

Prepro­cessor directive

Any line whose first non-empty character is a hash symbol (#). It is used to issue command to the prepro­­­c­e­ssor, a software that runs before calling the Assembler program.2,3,4
2 Refer to the Prepro­cessor Directives Table for a complete list of available prepro­cessor direct­ives.
3 Refer to the Prepro­cessor Pre-De­fined Macro Table for a complete list of available prepro­cessor pre-de­fined macros.
4 Refer to the Prepro­cessor Pragma Directives for a complete list of available prepro­cessor pragma direct­ives.