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.This Cheat Sheet is part of the Bladabuska's AVR Cheat Sheet Collection. This cheat sheet shows the basic syntax of the Assembly language for AVRs. All information in this cheat sheet is related to the AVR Assembler v2.0 from Microchip Technology Incorporated (formerly Atmel Corporation). The Assembly language for AVR microcontrollers is not case sensitive.
Note: If you use a different assembler software, you must adapt the information to your software.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 documentation, and to disable code sections for debugging purpose.
A backslash character (\) placed at the end of a line is used to inform the preprocessor and the Assembler that the command continues on the next line.
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 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 "0b", in hexadecimal notation with "0x" 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 readability.
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 Null Character Alert Bell Horizontal Tab Line Feed "or newline" Vertical Tab Carriage Return Hexadecimal Number Where H is the digit in hexadecimal notation (0-9,A-F) Octal Number Where o is the digit in octal notation (0-7)A string 7 is any number of characters enclosed in double quotes (") taken literally, no escape sequences are recognized, and it is not NULL-terminated. Quoted strings may be concatenated according to the ANSI C convention (space character) or with the backslash character (line continuation) to form long strings and multiple line-spanning strings.
7 Strings can only be used in conjunction with the DB directive or with the MESSAGE, WARNING or ERROR directives.
An operator is a (single or multiple) character(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 conditional operator takes three operands. Operators can be grouped with paranthesis to receive maximum precedence.
Operator Operation Precedence 3 Arithmetic Operators Subtraction Unary minus Multiplication Bitwise Operators Bitwise NOT Bitwise AND Bitwise OR Bitwise XOR Shift Left Shift Right Logic Operators Logical NOT Logical AND Logical OR Relational Operators Less than or equal to Greater than Greater than or equal to Different from Ternary Conditional Operator Ternary Conditional 53 The higher the precedence, 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 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, conditional 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 directives.Converts a fractional floating point expression to a form suitable for the multiplication instructions. (Sign + 7-bit fraction)
Q15(expression)Converts a fractional floating point expression to a form suitable for the multiplication instructions. (Sign + 15-bit fraction)
ABS(expression) Returns the absolute value of a constant expression. DEFINED(symbol)Returns 1 if symbol was previously defined (using .SET, .DEF, or .EQU directives), 0 otherwise. Normally used in conjunction with .IF and .ELIF directives. It does not require parentheses around its argument.
STRLEN(string) Returns the length of a string constant, in bytes.Defines the start of a CODE segment. CODE segments have their own location counter (in words), starting at 0.
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 peripherals).
Defines the start of an EEPROM segment. EEPROM segments have their own location counter (in bytes), starting at 0.
Sets the location counter (of the segment were it was pĺaced) to an absolute value. Reserve Memory SpaceReserves a number of BYTES in SRAM or EEPROM memories. The directive takes one parameter, which is the number of bytes to reserve.
Reserves a number of BYTES in PROGRAM or EEPROM memory. Reserves a number of WORDS (2 BYTES) in PROGRAM or EEPROM memory. Reserves a number of DOUBLE-WORDS (4 BYTES) in PROGRAM or EEPROM memory. Reserves a number of QUAD-WORDS (8 BYTES) in PROGRAM or EEPROM memory. User Defined SymbolsAssigns a user defined symbol to a value or expression. A symbol assigned to a value by the EQU directive is a constant and cannot be changed or redefined.
Assigns a user defined symbol to a value or expression. A symbol assigned to a value by the SET directive can be changed or redefined later in the program.
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.
Undefine a symbol previously defined with the DEF directive. This provides a way to obtain a simple scoping of register definitions, avoiding warnings about register reuse.
Macro DefinitionDefines 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.
.ENDMACRO/.ENDM Defines the end of a macro definition. ENDM is a synonym to ENDMACRO directive. File ManagementTells 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 encountered.
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.
Conditional Assembly If the symbol is defined, it will include code untill the corresponding ELSE directive. If the symbol is not defined, it will include code untill the corresponding ELSE directive.If expression is evaluated different from 0, it will include code untill the corresponding ELSE, ELIF or ENDIF directive.
It will include code until the corresponding 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.
It will include code until the corresponding ENDIF, if the initial IF clause and its following ELIF clauses (if any) are also false.
Defines the end for the conditional IF, IFDEF, or IFNDEF directives. Assembler Program Output Output a message string. Outputs a warning string, but does not halt assembling. Outputs an error message string and halts the assembling. Listfile Generation ControlTurn the listfile generation ON. The listfile is a combination of assembly source code, addresses, and opcodes. Listfile generation is turned on by default.
Turn listfile generation OFF.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 FuncitonsThis 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 Programmable System Level Integrated Circuit Devices.
This directive is for projects with special needs and should normally not be used. .NOOVERLAP V This directive is for projects with special needs and should normally not be used.A All segments of the same type will be concatenated 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 conditional 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.
Preprocessor directives are lines whose first non-empty character is a hash symbol (#). They are used to issue commands to the preprocessor.
Macro DefinitionDefine a preprocessor object-like macros that basically define a constant. If value is not specified, it is set to 1.
Define a preprocessor function-like macros that do parameter substitution. This macro must be called with the same number of arguments it is defined with. The left parenthesis must appear immediately after name (no spaces between), otherwise it will be interpreted as part of the value of a object-like macro. If value is not specified, it is set to 1.
Undefine macro name previously defined with a #define directive. If name is not previously defined, the .undef directive is silently ignored.
Conditional AssemblyAll the following lines until the corresponding #endif, #else, or #elif are conditionally assembled if name is previously #defined. Shorthand for #if defined (name).
All the following lines until the corresponding #endif, #else, or #elif are conditionally assembled if name is not #defined. Shorthand for #if !defined (name).
All the following lines until the corresponding #endif, #else, or #elif are conditionally assembled if expression evaluates to true (not equal to 0). Any undefined symbols used in expression are silently evaluated to 0.
All the following lines until the corresponding #endif, #else, or #elif are conditionally assembled if expression evaluates to true (not equal to 0),and all previous #if or #elif expressions were evaluated to false. Any undefined symbols used in expression are silently evaluated to 0.
All the following lines until the corresponding #endif are conditionally assembled if all previous #if or #elif expressions were evaluated to false.
Terminates a conditional block initiated with an #if, #ifdef, or #ifndef directive. Preprocessor Program Output Outputs string to standard output, but does not affect assembler error or warning counters. Outputs string to standard error, and increments the assembler warning counter.Outputs string to standard error, increments the assembler error counter, and prevents the program of being successfully assembled.
File Management #include "file"Include a file, searching in the current working directory first, then searching in the built-in known place.
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.