Professional Documents
Culture Documents
Avrasm
Avrasm
Directives
Expression
Instruction set
AVR assembler
OE-KVK-MAI
schuster.gyorgy@kvk.uni-obuda.hu
2013. október 9.
Assembly
Definition
An assembly language is a low-level programming language for a computer,
or other programmable device, in which there is a very strong (generally
one-to-one) correspondence between the language and the architecture’s
machine code instructions.
Assembly
Definition
An assembly language is a low-level programming language for a computer,
or other programmable device, in which there is a very strong (generally
one-to-one) correspondence between the language and the architecture’s
machine code instructions.
Each assembly language is specific to a particular computer architecture.
Assembly
Definition
An assembly language is a low-level programming language for a computer,
or other programmable device, in which there is a very strong (generally
one-to-one) correspondence between the language and the architecture’s
machine code instructions.
Each assembly language is specific to a particular computer architecture.
Assembler
Assembly language is converted into executable machine code by a utility
program referred to as an assembler.
Variations:
[label:] directive [operands] [Comment]
Variations:
[label:] directive [operands] [Comment]
[label:] instruction [operands] [Comment]
Variations:
[label:] directive [operands] [Comment]
[label:] instruction [operands] [Comment]
Comment
Variations:
[label:] directive [operands] [Comment]
[label:] instruction [operands] [Comment]
Comment
Empty line
Parts:
[label:] is an optional mark on the given line. Reason for its usage we want to
address this line i.e. as a jump aim.
Parts:
[label:] is an optional mark on the given line. Reason for its usage we want to
address this line i.e. as a jump aim.
directive is a control command for assembler, which has special effect(s) on
the compiling procedure.
Parts:
[label:] is an optional mark on the given line. Reason for its usage we want to
address this line i.e. as a jump aim.
directive is a control command for assembler, which has special effect(s) on
the compiling procedure.
[operands] is optional variable(s) for directive.
Parts:
[label:] is an optional mark on the given line. Reason for its usage we want to
address this line i.e. as a jump aim.
directive is a control command for assembler, which has special effect(s) on
the compiling procedure.
[operands] is optional variable(s) for directive.
[comment] is some optional remark or explanation to help follow the given
program.
Parts:
[label:] is an optional mark on the given line. Reason for its usage we want to
address this line i.e. as a jump aim.
directive is a control command for assembler, which has special effect(s) on
the compiling procedure.
[operands] is optional variable(s) for directive.
[comment] is some optional remark or explanation to help follow the given
program.
instruction is the mnemonic of the current assembly instruction.
Examples:
Examples:
Examples:
Example
.DEVICE AT90S1200 | AT90S2313
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
The Code Segments have their own location counter which is a word counter.
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
The Code Segments have their own location counter which is a word counter.
The directive does not take any parameters.
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
The Code Segments have their own location counter which is a word counter.
The directive does not take any parameters.
DSEG The DSEG directive defines the start of a Data Segment. An Assembler file can
consist of several Data Segments, which are concatenated into one Data
Segment when assembled.
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
The Code Segments have their own location counter which is a word counter.
The directive does not take any parameters.
DSEG The DSEG directive defines the start of a Data Segment. An Assembler file can
consist of several Data Segments, which are concatenated into one Data
Segment when assembled.
The directive does not take any parameters.
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
The Code Segments have their own location counter which is a word counter.
The directive does not take any parameters.
DSEG The DSEG directive defines the start of a Data Segment. An Assembler file can
consist of several Data Segments, which are concatenated into one Data
Segment when assembled.
The directive does not take any parameters.
ESEG The ESEG directive defines the start of an EEPROM Segment. An Assembler
file can consist of several EEPROM Segments, which are concatenated into one
EEPROM Segment when assembled.
Segment definitions
Symbols:
CSEG The CSEG directive defines the start of a Code Segment. An Assembler file may
consist of several Code Segments, which are concatenated into one Code
Segment when assembled.
The Code Segments have their own location counter which is a word counter.
The directive does not take any parameters.
DSEG The DSEG directive defines the start of a Data Segment. An Assembler file can
consist of several Data Segments, which are concatenated into one Data
Segment when assembled.
The directive does not take any parameters.
ESEG The ESEG directive defines the start of an EEPROM Segment. An Assembler
file can consist of several EEPROM Segments, which are concatenated into one
EEPROM Segment when assembled.
The directive does not take any parameters.
The ORG directive sets the location counter to an absolute value. The value to
set is given as a parameter.
The ORG directive sets the location counter to an absolute value. The value to
set is given as a parameter.
If an ORG directive is given within a Data Segment, then it is the SRAM location
counter which is set.
The ORG directive sets the location counter to an absolute value. The value to
set is 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.
The ORG directive sets the location counter to an absolute value. The value to
set is 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.
If the directive is given within an EEPROM Segment, then it is the EEPROM
location counter which is set.
The ORG directive sets the location counter to an absolute value. The value to
set is 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.
If the directive is given within an EEPROM Segment, then it is the EEPROM
location counter which is set.
It is possible to apply symbolic labels.
The ORG directive sets the location counter to an absolute value. The value to
set is 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.
If the directive is given within an EEPROM Segment, then it is the EEPROM
location counter which is set.
It is possible to apply symbolic labels.
The ORG directive can also be used to place the
variables at specific locations in the SRAM.
The ORG directive sets the location counter to an absolute value. The value to
set is 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.
If the directive is given within an EEPROM Segment, then it is the EEPROM
location counter which is set.
It is possible to apply symbolic labels.
The ORG directive can also be used to place the
variables at specific locations in the SRAM.
The ORG directive can also be used to place
constants at specific locations in the EEPROM
memory.
The ORG directive sets the location counter to an absolute value. The value to
set is 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.
If the directive is given within an EEPROM Segment, then it is the EEPROM
location counter which is set.
It is possible to apply symbolic labels.
The ORG directive can also be used to place the
variables at specific locations in the SRAM. Example
The ORG directive can also be used to place
constants at specific locations in the EEPROM .ORG 0x100
memory.
DB
The DB directive reserves memory resources in the program memory or the EEPROM
memory. The DB directive must be placed in a Code Segment or an EEPROM
Segment.
DB
The DB directive reserves memory resources in the program memory or the EEPROM
memory. The DB directive must be placed in a Code Segment or an EEPROM
Segment.
In order to be able to refer to the reserved locations, the DB directive should be
preceded by a label.
DB
The DB directive reserves memory resources in the program memory or the EEPROM
memory. The DB directive must be placed in a Code Segment or an EEPROM
Segment.
In order to be able to refer to the reserved locations, the DB directive should be
preceded by a label.
The DB directive takes a list of expressions, and must contain at least one expression.
The expression list is a sequence of expressions, delimited by commas.
DB
The DB directive reserves memory resources in the program memory or the EEPROM
memory. The DB directive must be placed in a Code Segment or an EEPROM
Segment.
In order to be able to refer to the reserved locations, the DB directive should be
preceded by a label.
The DB directive takes a list of expressions, and must contain at least one expression.
The expression list is a sequence of expressions, delimited by commas.
Each expression 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 program memory or EEPROM memory location.
DB
The DB directive reserves memory resources in the program memory or the EEPROM
memory. The DB directive must be placed in a Code Segment or an EEPROM
Segment.
In order to be able to refer to the reserved locations, the DB directive should be
preceded by a label.
The DB directive takes a list of expressions, and must contain at least one expression.
The expression list is a sequence of expressions, delimited by commas.
Each expression 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 program memory or EEPROM memory location.
DB
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
program memory word.
DB
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
program 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.
DB
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
program 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.
Example
consts: .DB 0, 255, 0b01010101, -128, 0xaa
Example
consts: .DW 0,0xffff,0b1001110001010101,-32768,65535
BYTE
The BYTE directive reserves memory resources in the SRAM. In order to be able to
refer to the reserved location, the BYTE directive should be preceded by a label.
BYTE
The BYTE directive reserves memory resources in the SRAM. In order to be able to
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.
BYTE
The BYTE directive reserves memory resources in the SRAM. In order to be able to
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.
Note that a parameter must be given. The allocated bytes are not initialized.
BYTE
The BYTE directive reserves memory resources in the SRAM. In order to be able to
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.
Note that a parameter must be given. The allocated bytes are not initialized.
Example
var1: .BYTE 240
DEF
The DEF directive allows the registers to be referred to through symbols.
DEF
The DEF directive allows the registers to be referred to through symbols.
A defined symbol can be used in the rest of the program to refer to the register it is
assigned to.
DEF
The DEF directive allows the registers to be referred to through symbols.
A defined symbol can be used in the rest of the program to refer to the register it is
assigned to.
A register can have several symbolic names attached to it.
DEF
The DEF directive allows the registers to be referred to through symbols.
A defined symbol can be used in the rest of the program to refer to the register it is
assigned to.
A register can have several symbolic names attached to it.
A symbol can be redefined later in the program.
DEF
The DEF directive allows the registers to be referred to through symbols.
A defined symbol can be used in the rest of the program to refer to the register it is
assigned to.
A register can have several symbolic names attached to it.
A symbol can be redefined later in the program.
Example
.DEF temp = R16
INCLUDE
The INCLUDE directive tells the Assembler to start reading from a specified file.
INCLUDE
The INCLUDE directive 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 encountered.
INCLUDE
The INCLUDE directive 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 encountered.
An included file may itself contain INCLUDE directives.
INCLUDE
The INCLUDE directive 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 encountered.
An included file may itself contain INCLUDE directives.
Example
.INCLUDE "parms.inc"
ENDMACRO
The ENDMACRO directive defines the end of a macro definition. The directive does
not take any parameters.
ENDMACRO
The ENDMACRO directive defines the end of a macro definition. The directive does
not take any parameters.
Example
NOLIST
The NOLIST directive tells the Assembler to turn listfile generation off.
NOLIST
The NOLIST directive tells the Assembler to turn listfile generation off.
Example
LISTMAC
The LISTMAC directive 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.
LISTMAC
The LISTMAC directive 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.
LISTMAC
The LISTMAC directive 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.
Example
Operands
Numbers
a Decimal: 10, 255, -30,
Operands
Numbers
a Decimal: 10, 255, -30,
b Hexadecimal: 0x0a, 0x0A, $0a, 0xff, $ff,
Operands
Numbers
a Decimal: 10, 255, -30,
b Hexadecimal: 0x0a, 0x0A, $0a, 0xff, $ff,
c Binary: 0b00001010,
Operands
Numbers
a Decimal: 10, 255, -30,
b Hexadecimal: 0x0a, 0x0A, $0a, 0xff, $ff,
c Binary: 0b00001010,
d Program counter: PC
Functions
Parts
LOW(expression) returns the low byte of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
BYTE4(expression) returns the fourth byte of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
BYTE4(expression) returns the fourth byte of an expression,
LWRD(expression) returns bits 0-15 of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
BYTE4(expression) returns the fourth byte of an expression,
LWRD(expression) returns bits 0-15 of an expression,
HWRD(expression) returns bits 16-31 of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
BYTE4(expression) returns the fourth byte of an expression,
LWRD(expression) returns bits 0-15 of an expression,
HWRD(expression) returns bits 16-31 of an expression,
PAGE(expression) returns bits 16-21 of an expression,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
BYTE4(expression) returns the fourth byte of an expression,
LWRD(expression) returns bits 0-15 of an expression,
HWRD(expression) returns bits 16-31 of an expression,
PAGE(expression) returns bits 16-21 of an expression,
EXP2(expression) returns 2expression ,
Functions
Parts
LOW(expression) returns the low byte of an expression,
HIGH(expression) returns the second byte of an expression,
BYTE2(expression) is the same function as HIGH,
BYTE3(expression) returns the third byte of an expression,
BYTE4(expression) returns the fourth byte of an expression,
LWRD(expression) returns bits 0-15 of an expression,
HWRD(expression) returns bits 16-31 of an expression,
PAGE(expression) returns bits 16-21 of an expression,
EXP2(expression) returns 2expression ,
LOG2(expression) returns the integer part of log2(expression)
Operators
TRUE - FALSE
In AVR8 Assembler the:
TRUE value is ’1’,
Operators
TRUE - FALSE
In AVR8 Assembler the:
TRUE value is ’1’,
FALSE value is ’0’.
Operators
TRUE - FALSE
In AVR8 Assembler the:
TRUE value is ’1’,
FALSE value is ’0’.
Operators
AVR8 Assembler supports all the operators used in ANSI C language exept for special
group ones.
Operators
TRUE - FALSE
In AVR8 Assembler the:
TRUE value is ’1’,
FALSE value is ’0’.
Operators
AVR8 Assembler supports all the operators used in ANSI C language exept for special
group ones.
Notation of flags
Notation of flags
Notation of flags
Notation of flags
Notation of flags
Notation of flags
Notation of flags
Notation of flags
Notation of flags
Notation of operands
Notation of operands
Notation of operands
Notation of operands
Notation of operands
Notation of operands
Notation of operands
Notation of operands
15 4 0
OP Rd
31
15 9 54 0
OP Rr Rd
31
15 5 0
OP Rd/Rr A
63
31 19 16
OP Rr/Rd
Data address
15 0
RAMEND
OP
15 10 5 0
RAMEND
15 0
X, Y or Z regiszter ,
RAMEND
RAMEND
RAMEND
LSB
FLASHEND
LSB
FLASHEND
15 0
PC
FLASHEND
15 0
PC
FLASHEND
15 12 11 0
OP k
FLASHEND
MOV
This instruction makes a copy of one register into another.
MOV
This instruction makes a copy of one register into another.
The source register Rr is left unchanged while the destination register Rd is loaded
with a copy of Rr.
MOV
This instruction makes a copy of one register into another.
The source register Rr is left unchanged while the destination register Rd is loaded
with a copy of Rr.
Operation: Rd←Rr
Syntax: MOV Rd,Rr
Operands: 0≤d≤31, 0≤r≤31
Flags: no effects on flags
Words: 1
Cycles: 1
Operation: Rd+1:Rd←Rr+1:Rr
Syntax: MOVW Rd+1:Rd,Rr+1:Rr
Operands: d∈{0,2,· · · ,28,30} r∈{0,2,· · · ,28,30}
Example: MOVW R31:R30,R17:R16
Flags: no effects on flags
Words: 1
Cycles: 1
LDI
Loads an 8 bit constant directly to register 16 to 31.
LDI
Loads an 8 bit constant directly to register 16 to 31.
Operation: Rd←k
Syntax: LDI Rd,k
Operands: 16≤d≤31, 0≤k≤255
Flags: no effects on flags
Words: 1
Cycles: 1
LDS
Loads one byte from the data space to a register.
LDS
Loads one byte from the data space to a register.
For parts with SRAM, the data space consists of the Register File, I/O memory and
internal SRAM (and external SRAM if applicable).
LDS
Loads one byte from the data space to a register.
For parts with SRAM, the data space consists of the Register File, I/O memory and
internal SRAM (and external SRAM if applicable).
Operation: Rd←(k)
Syntax: LDS Rd,k
Operands: 0≤d≤31, 0≤k≤65535
Flags: no effects on flags
Words: 2
Cycles: 2
LD, LDD
Loads one byte indirect with or without displacement from the data space to a register.
LD, LDD
Loads one byte indirect with or without displacement from the data space to a register.
For parts with SRAM, the data space consists of the Register File, I/O memory and
internal SRAM (and external SRAM if applicable).
LD, LDD
Loads one byte indirect with or without displacement from the data space to a register.
For parts with SRAM, the data space consists of the Register File, I/O memory and
internal SRAM (and external SRAM if applicable).
For parts without SRAM, the data space consists of the Register File only.
LD, LDD
Loads one byte indirect with or without displacement from the data space to a register.
For parts with SRAM, the data space consists of the Register File, I/O memory and
internal SRAM (and external SRAM if applicable).
For parts without SRAM, the data space consists of the Register File only.
The data location is pointed to by the Z (16 bits) Pointer Register in the Register File.
Memory access is limited to the current data segment of 64K bytes.
Operation: (k)←Rr
Syntax: STS k,Rr
Operands: 0≤r≤31, 0≤k≤65535
Flags: no effects on flags
Words: 2
Cycles: 2
*
Dr. György Schuster AVR assembler
Notation of operands
Notation of operands
Basics The Program and Data Addressing Modes
Directives Data transfer instructions
Expression Arithmetic instructions
Instruction set Logic instructions
Bit and bit-test instructions
Branch Instruction
*
Dr. György Schuster AVR assembler
Notation of operands
Notation of operands
Basics The Program and Data Addressing Modes
Directives Data transfer instructions
Expression Arithmetic instructions
Instruction set Logic instructions
Bit and bit-test instructions
Branch Instruction
Operation: R0 ← (Z)
Rd ← (Z) ;0≤d≤31
Rd ← (Z++) ;0≤d≤31
Syntax: LPM ;R0
LPM Rd,Z
LMP Rd,Z+
Operands: 0≤d≤31, 0≤k≤65535
Flags: no effects on flags
Words: 1
Cycles: 3
IN
Loads data from the I/O Space (Ports, Timers, Configuration Registers etc.) into
register Rd in the Register File.
IN
Loads data from the I/O Space (Ports, Timers, Configuration Registers etc.) into
register Rd in the Register File.
Operation: Rd←I/O(A)
Syntax: IN Rd,A
Operands: 0≤d≤31, 0≤A≤63
Flags: no effects on flags
Words: 1
Cycles: 1
OUT
Stores data from register Rr in the Register File to I/O Space (Ports, Timers,
Configuration Registers etc.).
OUT
Stores data from register Rr in the Register File to I/O Space (Ports, Timers,
Configuration Registers etc.).
Operation: I/O(A)←Rr
Syntax: OUT A,Rr
Operands: 0≤r≤31, 0≤A≤63
Flags: no effects on flags
Words: 1
Cycles: 1
POP
This instruction loads register Rd with a byte from the STACK. The Stack Pointer is
pre-incremented by 1 before the POP.
POP
This instruction loads register Rd with a byte from the STACK. The Stack Pointer is
pre-incremented by 1 before the POP.
CLR
Clears a register. This instruction performs an Exclusive OR between a register and
itself. This will clear all bits in the register.
CLR
Clears a register. This instruction performs an Exclusive OR between a register and
itself. This will clear all bits in the register.
Operation: Rd ← Rd⊕Rd
Syntax: CLR Rd
Operands: 0≤d≤31
Flags: Z=1
Words: 1
Cycles: 1
SER
Loads $FF directly to register Rd.
SER
Loads $FF directly to register Rd.
Operation: Rd ← $FF
Syntax: SER Rd
Operands: 16≤d≤31
Flags: No effects on flags
Words: 1
Cycles: 1
Arithmetic instructions
ADD
Adds two registers without the C Flag and places the result in the destination register
Rd.
Operation: Rd,←Rd+Rr
Syntax: ADD Rd,Rr
Operands: 0 ≤ d ≤ 31 0 ≤ r ≤ 31
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
ADC
Adds two registers with the C Flag and places the result in the destination register Rd.
Operation: Rd,←Rd+Rr+C
Syntax: ADC Rd,Rr
Operands: 0 ≤ d ≤ 31 0 ≤ r ≤ 31
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
ADIW
Adds an immediate value (0 - 63) to a register pair and places the result in the register
pair.
This instruction operates on the upper four register pairs, and is well suited for
operations on the pointer registers. This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: Rd+1:Rd,←Rd+1:Rd+k
Syntax: ADIW Rd+1,Rd,k
Operands: d ∈ 24,26,28,30 0 ≤ k ≤ 63
Flags: S, V, N, Z, C
Words: 1
Cycles: 2
Arithmetic instructions
SUB
Subtracts two registers and places the result in the destination register Rd.
Operation: Rd,←Rd-Rr
Syntax: SUB Rd,Rr
Operands: 0 ≤ d ≤ 31 0 ≤ r ≤ 31
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
SUBI
Subtracts a register and a constant and places the result in the destination register Rd.
This instruction is working on Register R16 to R31 and is very well suited for
operations on the X, Y and Z-pointers.
Operation: Rd←Rd-k
Syntax: SUBI Rd,k
Operands: 16≤d≤31, 0≤k≤255
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
SBC
Subtracts two registers and subtracts with the C Flag and places the result in the
destination register Rd.
Operation: Rd,←Rd-Rr-C
Syntax: SBC Rd,Rr
Operands: 0 ≤ d ≤ 31 0 ≤ r ≤ 31
Flags: H, S, V, N, Z, C
Arithmetic instructions
SBCI
Subtracts a constant from a register and subtracts with the C Flag and places the result
in the destination register Rd.
Operation: Rd,←Rd-K-C
Syntax: SBCI Rd,K
Operands: 0 ≤ d ≤ 31 0 ≤ K ≤ 255
Flags: H, S, V, N, Z, C
Arithmetic instructions
SBIW
Subtracts an immediate value (0-63) from a register pair and places the result in the
register pair. This instruction operates on the upper four register pairs, and is well
suited for operations on the Pointer Registers.
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: Rd+1:Rd,←Rd+1:Rd-k
Syntax: SBIW Rd+1,Rd,k
Operands: d ∈ 24,26,28,30 0 ≤ k ≤ 63
Flags: S, V, N, Z, C
Words: 1
Cycles: 2
Arithmetic instructions
NEG
Replaces the contents of register Rd with its two’s complement; the value $80 is left
unchanged.
Operation: Rd ← 0-Rd
Syntax: NEG Rd
Operands: 0≤d≤31
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
INC
Adds one -1- to the contents of register Rd and places the result in the destination
register Rd.
The C Flag in SREG is not affected by the operation, thus allowing the INC instruction
to be used on a loop counter in multiple-precision computations.
When operating on unsigned numbers, only BREQ and BRNE branches can be
expected to perform consistently. When operating on twos complement values, all
signed branches are available.
Operation: Rd++
Syntax: INC Rd
Operands: 0≤d≤31
Flags: S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
DEC
Subtracts one -1- from the contents of register Rd and places the result in the
destination register Rd.
The C Flag in SREG is not affected by the operation, thus allowing the DEC instruction
to be used on a loop counter in multiple-precision computations.
When operating on unsigned values, only BREQ and BRNE branches can be expected
to perform consistently.
When operating on two’s complement values, all signed branches are available.
Operation: Rd--
Syntax: DEC Rd
Operands: 0≤d≤31
Flags: S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
MUL
This instruction performs 8-bit 8-bit 16-bit unsigned multiplication.
The multiplicand Rd and the multiplier Rr are two registers containing unsigned
numbers.
The 16-bit unsigned product is placed in R1 (high byte) and R0 (low byte).
Note that if the multiplicand or the multiplier is selected from R0 or R1 the result will
overwrite those after multiplication.
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: R1:R0 ← Rd x Rr
Syntax: MUL Rd,Rr
Operands: 0≤d≤31 0≤r≤31
Flags: Z, C
Words: 1
Cycles: 2
Dr. György Schuster AVR assembler
Notation of operands
Notation of operands
Basics The Program and Data Addressing Modes
Directives Data transfer instructions
Expression Arithmetic instructions
Instruction set Logic instructions
Bit and bit-test instructions
Branch Instruction
Arithmetic instructions
MULS
This instruction performs 8-bit 8-bit 16-bit signed multiplication.
The multiplicand Rd and the multiplier Rr are two registers containing signed numbers.
The 16-bit signed product is placed in R1 (high byte) and R0 (low byte).
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: R1:R0 ← Rd x Rr
Syntax: MULS Rd,Rr
Operands: 16≤d≤31 16≤r≤31
Flags: Z, C
Words: 1
Cycles: 2
Arithmetic instructions
MULSU
This instruction performs 8-bit 8-bit 16-bit multiplication of a signed and an unsigned
number.
The multiplicand Rd and the multiplier Rr are two registers.
The multiplicand Rd is a signed number, and the multiplier Rr is unsigned.
The 16-bit signed product is placed in R1 (high byte) and R0 (low byte).
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: R1:R0 ← Rd x Rr
Syntax: MULSU Rd,Rr
Operands: 16≤d≤23 16≤r≤23
Flags: Z, C
Words: 1
Cycles: 2
Arithmetic instructions
FMULS
This instruction performs 8-bit 8-bit 16-bit signed multiplication and shifts the result
one bit left.
Let (N.Q) denote a fractional number with N binary digits left of the radix point, and Q
binary digits right of the radix point.
A multiplication between two numbers in the formats (N1.Q1) and (N2.Q2) results in
the format ((N1+N2).(Q1+Q2)).
A left shift is required for the high byte of the product to be in the same format as the
inputs. The FMULS instruction incorporates the shift operation in the same number of
cycles as MULS.
The multiplicand Rd and the multiplier Rr are two registers containing signed fractional
numbers where the implicit radix point lies between bit 6 and bit 7.
The 16-bit signed fractional product with the implicit radix point between bit 14 and bit
15 is placed in R1 (high byte) and R0 (low byte).
Note that when multiplying 0x80 (-1) with 0x80 (-1), the result of the shift operation is
0x8000 (-1). The shift operation thus gives a twos complement overflow. This must be
checked and handled by software.
This instruction is not available in all devices.
Refer to the device specific instruction
Dr. Gy set summary.
örgy Schuster AVR assembler
Notation of operands
Notation of operands
Basics The Program and Data Addressing Modes
Directives Data transfer instructions
Expression Arithmetic instructions
Instruction set Logic instructions
Bit and bit-test instructions
Branch Instruction
Arithmetic instructions
FMULS
Operation: R1:R0 ← Rd x Rr
Syntax: FMULS Rd,Rr
Operands: 16≤d≤23 16≤r≤23
Flags: Z, C
Words: 1
Cycles: 2
Arithmetic instructions
FMULSU
This instruction performs 8-bit 8-bit 16-bit signed multiplication and shifts the result
one bit left.
Let (N.Q) denote a fractional number with N binary digits left of the radix point, and Q
binary digits right of the radix point.
A multiplication between two numbers in the formats (N1.Q1) and (N2.Q2) results in
the format ((N1+N2).(Q1+Q2)).
A left shift is required for the high byte of the product to be in the same format as the
inputs. The FMULSU instruction incorporates the shift operation in the same number of
cycles as MULSU.
The MSB of the multiplication before shifting must be taken into account, and is found
in the carry bit.
The multiplicand Rd and the multiplier Rr are two registers containing fractional
numbers where the implicit radix point lies between bit 6 and bit 7. The multiplicand Rd
is a signed fractional number, and the multiplier Rr is an unsigned fractional number.
The 16-bit signed fractional product with the implicit radix point between bit 14 and bit
15 is placed in R1 (high byte) and R0 (low byte).
This instruction is not available in all devices.
Refer to the device specific instruction
Dr. Gy set summary.
örgy Schuster AVR assembler
Notation of operands
Notation of operands
Basics The Program and Data Addressing Modes
Directives Data transfer instructions
Expression Arithmetic instructions
Instruction set Logic instructions
Bit and bit-test instructions
Branch Instruction
Arithmetic instructions
FMULSU
Operation: R1:R0 ← Rd x Rr
Syntax: FMULSU Rd,Rr
Operands: 16≤d≤23 16≤r≤23
Flags: Z, C
Words: 1
Cycles: 2
Arithmetic instructions
CP
This instruction performs a compare between two registers Rd and Rr. None of the
registers are changed. All conditional branches can be used after this instruction.
Operation: Rd-Rr
Syntax: CP Rd,Rr
Operands: 0 ≤ d ≤ 31 0 ≤ r ≤ 31
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
CPI
This instruction performs a compare between register Rd and a constant. The register
is not changed. All conditional branches can be used after this instruction.
Operation: Rd-K
Syntax: CPI Rd,K
Operands: 0 ≤ d ≤ 31 0 ≤ K ≤ 255
Flags: H, S, V, N, Z, C
Words: 1
Cycles: 1
Arithmetic instructions
CPC
This instruction performs a compare between two registers Rd and Rr and also takes
into account the previous carry. None of the registers are changed. All conditional
branches can be used after this instruction.
Operation: Rd-Rr-C
Syntax: CPC Rd,Rr
Operands: 0 ≤ d ≤ 31 0 ≤ r ≤ 31
Flags: H, S, V, N, Z, C
Logic instructions
AND
Performs the logical AND between the contents of register Rd and register Rr and
places the result in the destination register Rd.
Operation: Rd ← Rd ∧ Rr
Syntax: AND Rd,Rr
Operands: 0≤d≤31 0≤r≤31
Flags: S, N, Z
Words: 1
Cycles: 1
Logic instructions
ANDI
Performs the logical AND between the contents of register Rd and a constant and
places the result in the destination register Rd.
Operation: Rd ← Rd ∧ k
Syntax: ANDI Rd,k
Operands: 0≤d≤31 0≤k≤255
Flags: S, N, Z
Words: 1
Cycles: 1
Logic instructions
OR
Performs the logical OR between the contents of register Rd and register Rr and
places the result in the destination register Rd.
Operation: Rd ← Rd ∨ Rr
Syntax: OR Rd,Rr
Operands: 0≤d≤31 0≤r≤31
Flags: S, N, Z
Words: 1
Cycles: 1
Logic instructions
ORI
Performs the logical OR between the contents of register Rd and a constant and places
the result in the destination register Rd.
Operation: Rd ← Rd ∨ k
Syntax: ANDI Rd,k
Operands: 16≤d≤31 0≤k≤255
Flags: S, N, Z
Words: 1
Cycles: 1
Logic instructions
EOR
Performs the logical EOR between the contents of register Rd and register Rr and
places the result in the destination register Rd.
Operation: Rd ← Rd ⊕ Rr
Syntax: EOR Rd,Rr
Operands: 0≤d≤31 0≤r≤31
Flags: S, N, Z
Words: 1
Cycles: 1
Logic instructions
COM
This instruction performs a Ones Complement of register Rd.
Operation: Rd ← ¬Rd
Syntax: COM Rd
Operands: 0≤d≤31
Flags: S, N, Z
Words: 1
Cycles: 1
SWAP
Swaps high and low nibbles in a register.
Operation: Rd(7:4)⇄Rd(3:0)
Syntax: SWAP Rd
Operands: 0≤d≤31
Flags: no effects on flags
Words: 1
Cycles: 1
BSET
Sets a single Flag or bit in SREG.
Operation: SREG(s)←1
Syntax: BSET s
Operands: 0≤s≤7
Flags: I, T, H, S, V, N, C, Z just the addressed one, the others
remain unchanged
Words: 1
Cycles: 1
BCLR
Clears a single Flag in SREG.
Operation: SREG(s)←0
Syntax: BCLR s
Operands: 0≤s≤7
Flags: I, T, H, S, V, N, C, Z just the addressed one, the others
remain unchanged
Words: 1
Cycles: 1
SBI
Sets a specified bit in an I/O Register.
This instruction operates on the lower 32 I/O Registers addresses 0-31.
Operation: IO(A,b)←1
Syntax: SBI A,b
Operands: 0≤A≤31, 0≤b≤7
Flags: no effects on flags
Words: 1
Cycles: 1
CBI
Clears a specified bit in an I/O Register.
This instruction operates on the lower 32 I/O Registers addresses 0-31.
Operation: IO(A,b)←0
Syntax: CBI A,b
Operands: 0≤A≤31, 0≤b≤7
Flags: no effects on flags
Words: 1
Cycles: 1
BST
Stores bit b from Rd to the T Flag in SREG (Status Register).
Operation: T←Rd(b)
Syntax: BST Rd,b
Operands: 0≤Rd≤31, 0≤b≤7
Flags: T
Words: 1
Cycles: 1
BLD
Copies the T Flag in the SREG (Status Register) to bit b in register Rd.
Operation: Rd(b)←T
Syntax: BLD Rd,b
Operands: 0≤Rd≤31, 0≤b≤7
Flags: no effects on the flags
Words: 1
Cycles: 1
SEC CLC
Sets the Carry Flag (C) in SREG (Status Clears the Carry Flag (C) in SREG (Status
Register). Register).
SEN CLN
Sets the Negative Flag (N) in SREG Clears the Negative Flag (N) in SREG
(Status Register). (Status Register).
SEZ CLZ
Sets the Zero Flag (Z) in SREG (Status Clears the Zero Flag (Z) in SREG (Status
Register). Register).
SEI CLI
Sets the Interrupt Enable Flag (I) in SREG Clears the Interrupt Enable Flag (I) in
(Status Register). SREG (Status Register).
SES CLS
Sets the Signed Flag (S) in SREG (Status Clears the Signed Flag (S) in SREG
Register). (Status Register).
SEV CLV
Sets the Overflow Flag (V) in SREG Clears the Overflow Flag (V) in SREG
(Status Register). (Status Register).
SET CLT
Sets the T Flag (T) in SREG (Status Clears the T Flag (V) in SREG (Status
Register). Register).
SEH CLH
Sets the Half Carry (H) in SREG (Status Clearss the Half Carry (H) in SREG (Status
Register). Register).
Branch Instructions
JMP
Jump to an address within the entire Program memory. See also RJMP.
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: PC←K
Syntax: JMP K
Operands: 0≤K≤4M
Flags: no effects on flags
Words: 2
Cycles: 3
Branch Instructions
RJMP
Relative jump to an address within PC - 2K +1 and PC + 2K (words).
For AVR microcontrollers with Program memory not exceeding 4K words (8K bytes) this
instruction can address the entire memory from every address location. See also JMP.
Operation: PC←PC+K+1
Syntax: RJMP K
Operands: -2K≤K≤2K
Flags: no effects on flags
Words: 1
Cycles: 2
Branch Instructions
IJMP
Indirect jump to the address pointed to by the Z (16 bits) Pointer Register in the
Register File.
The Z-pointer Register is 16 bits wide and allows jump within the lowest 64K words
(128K bytes) section of Program memory.
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: PC←Z
Syntax: IJMP
Operands: none
Flags: no effects on flags
Words: 1
Cycles: 2
Branch Instructions
CALL
Calls to a subroutine within the entire Program memory.
The return address (to the instruction after the CALL) will be stored onto the Stack.
The Stack Pointer uses a post-decrement scheme during CALL.
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: PC←K
STACK←PC+2
SP←SP-2
Syntax: CALL K
Operands: 0≤K≤64K
Flags: no effects on flags
Words: 2
Cycles: 4
Branch Instructions
RCALL
Relative call to an address within PC - 2K + 1 and PC + 2K (words).
The return address (the instruction after the RCALL) is stored onto the Stack.
For AVR microcontrollers with Program memory not exceeding 4K words (8K bytes)
this instruction can address the entire memory from every address location.
The Stack Pointer uses a post-decrement scheme during RCALL.
Operation: PC←PC+K+1
STACK←PC+1
SP←SP-2
Syntax: CALL K
Operands: -2K≤K≤2K
Flags: no effects on flags
Words: 1
Cycles: 3
Branch Instructions
ICALL
Calls to a subroutine within the Program memory.
The return address (to the instruction after the CALL) will be stored onto the Stack.
See also RCALL.
The Stack Pointer uses a post-decrement scheme during CALL.
This instruction is not available in all devices.
Refer to the device specific instruction set summary.
Operation: PC←Z
STACK←PC+1
SP←SP-2
Syntax: ICALL
Operands: none
Flags: no effects on flags
Words: 1
Cycles: 3
Dr. György Schuster AVR assembler
Notation of operands
Notation of operands
Basics The Program and Data Addressing Modes
Directives Data transfer instructions
Expression Arithmetic instructions
Instruction set Logic instructions
Bit and bit-test instructions
Branch Instruction
Branch Instructions
RET
Returns from subroutine. The return address is loaded from the STACK.
The Stack Pointer uses a pre-increment scheme during RET
Branch Instructions
RETI
Returns from interrupt. The return address is loaded from the STACK and the Global
Interrupt Flag is set.
Note that the Status Register is not automatically stored when entering an interrupt
routine, and it is not restored when returning from an interrupt routine. This must be
handled by the application program. The Stack Pointer uses a pre-increment scheme
during RETI.
Branch Instructions
CPSE
This instruction performs a compare between two registers Rd and Rr, and skips the
next instruction if Rd = Rr.
Branch Instructions
SBRS SBRC
This instruction tests a single bit in a This instruction tests a single bit in a
register and skips the next instruction if the register and skips the next instruction if the
bit is set. bit is cleared.
Branch Instructions
SBIS SBIC
This instruction tests a single bit in an I/O This instruction tests a single bit in an I/O
Register and skips the next instruction if Register and skips the next instruction if the
the bit is set. This instruction operates on bit is cleared. This instruction operates on
the lower 32 I/O Registers addresses 0-31. the lower 32 I/O Registers addresses 0-31.
Special instructions
NOP
This instruction performs a single cycle No Operation.
Special instructions
NOP
This instruction performs a single cycle No Operation.
SLEEP
This instruction sets the circuit in sleep mode defined by the MCU Control Register.
Special instructions
NOP
This instruction performs a single cycle No Operation.
SLEEP
This instruction sets the circuit in sleep mode defined by the MCU Control Register.
WDR
This instruction resets the Watchdog Timer. This instruction must be executed within a
limited time given by the WD prescaler.
See the Watchdog Timer hardware specification.