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

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 Null Character Alert Bell Horizontal Tab Line Feed "or newlin­e" Vertical Tab Carriage Return Hexade­cimal Number Where H is the digit in hexade­cimal notation (0-9,A-F) Octal Number Where o is the digit in octal notation (0-7)

String constant

A string 7 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 Precedence 3 Arithmetic Operators Subtra­ction Unary minus Multip­lic­ation 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 Condit­ional Operator Ternary Condit­ional 5

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

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 periph­erals).

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 Space

Reserves 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 Symbols

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.

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.

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 defini­tions, avoiding warnings about register reuse.

Macro Definition

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

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.

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 If the symbol is defined, it will include code untill the corres­ponding ELSE directive. If the symbol is not defined, it will include code untill the corres­ponding ELSE directive.

If expression is evaluated different from 0, it will include code untill the corres­ponding ELSE, ELIF or ENDIF directive.

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.

It will include code until the corres­ponding ENDIF, if the initial IF clause and its following ELIF clauses (if any) are also false.

Defines the end for the condit­ional IF, IFDEF, or IFNDEF direct­ives. Assembler Program Output Output a message string. Outputs a warning string, but does not halt assemb­ling. Outputs an error message string and halts the assemb­ling. Listfile Generation Control

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.

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 Funcitons

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.

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 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 a prepro­cessor object­-like macros that basically define a constant. If value is not specified, it is set to 1.

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.

Undefine macro name previously defined with a #define directive. If name is not previously defined, the .undef directive is silently ignored.

Condit­ional Assembly

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).

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).

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.

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.

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.

Terminates a condit­ional block initiated with an #if, #ifdef, or #ifndef directive. Prepro­cessor 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 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 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.