Rd:Destination (and source) register in the Register File
Rr:Source register in the Register File
R:Result after instruction is executed
K:Constant data
k:Constant address
b:Bit in the Register File or I/O Register (3-bit)
s:Bit in the Status Register (3-bit)
X,Y,Z:Indirect Address Register
(X=R27:R26, Y=R29:R28 and Z=R31:R30)
8-bit
Instruction Set
A:I/O location address
q:Displacement for direct addressing (6-bit)
Rev. 0856I–AVR–07/10
I/O Registers
RAMPX, RAMPY, RAMPZ
Registers concatenated with the X-, Y-, and Z-registers enabling indirect addressing of the whole data space on MCUs with
more than 64K bytes data space, and constant data fetch on MCUs with more than 64K bytes program space.
RAMPD
Register concatenated with the Z-register enabling direct addressing of the whole data space on MCUs with more than 64K
bytes data space.
EIND
Register concatenated with the Z-register enabling indirect jump and call to the whole program space on MCUs with more
than 64K words (128K bytes) program space.
Stack
STACK: Stack for return address and pushed registers
SP:Stack Pointer to STACK
Flags
⇔:Flag affected by instruction
0:Flag cleared by instruction
1:Flag set by instruction
-:Flag not affected by instruction
2
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
The Program and Data Addressing Modes
The AVR Enhanced RISC microcontroller supports powerful and efficient addressing modes for access to the Program
memory (Flash) and Data memory (SRAM, Register file, I/O Memory, and Extended I/O Memory). This section describes
the various addressing modes supported by the AVR architecture. In the following figures, OP means the operation code
part of the instruction word. To simplify, not all figures show the exact location of the addressing bits. To generalize, the
abstract terms RAMEND and FLASHEND have been used to represent the highest location in data and program space,
respectively.
Note:Not all addressing modes are present in all devices. Refer to the device spesific instruction summary.
Register Direct, Single Register Rd
Figure 1. Direct Single Register Addressing
The operand is contained in register d (Rd).
Register Direct, Two Registers Rd and Rr
Figure 2. Direct Register Addressing, Two Registers
Operands are contained in register r (Rr) and d (Rd). The result is stored in register d (Rd).
0856I–AVR–07/10
3
I/O Direct
OPRr/Rd
16
31
150
Data Address
0x0000
RAMEND
20 19
Data Space
Figure 3. I/O Direct Addressing
Operand address is contained in 6 bits of the instruction word. n is the destination or source register address.
Note:Some complex AVR Microcontrollers have more peripheral units than can be supported within the 64 locations reserved in the
opcode for I/O direct addressing. The extended I/O memory from address 64 to 255 can only be reached by data addressing,
not I/O addressing.
Data Direct
Figure 4. Direct Data Addressing
A 16-bit Data Address is contained in the 16 LSBs of a two-word instruction. Rd/Rr specify the destination or source
register.
4
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
Data Space
0x0000
RAMEND
Y OR Z - REGISTER
OPqRr/Rd
0
05610
15
15
Data Space
0x0000
X, Y OR Z - REGISTER
015
RAMEND
Data Indirect with Displacement
Figure 5. Data Indirect with Displacement
Operand address is the result of the Y- or Z-register contents added to the address contained in 6 bits of the instruction
word. Rd/Rr specify the destination or source register.
Data Indirect
Figure 6. Data Indirect Addressing
Operand address is the contents of the X-, Y-, or the Z-register. In AVR devices without SRAM, Data Indirect Addressing is
called Register Indirect Addressing. Register Indirect Addressing is a subset of Data Indirect Addressing since the data
space form 0 to 31 is the Register File.
0856I–AVR–07/10
5
Data Indirect with Pre-decrement
Data Space
0x0000
X, Y OR Z - REGISTER
015
-1
RAMEND
Data Space
0x0000
X, Y OR Z - REGISTER
015
1
RAMEND
Figure 7. Data Indirect Addressing with Pre-decrement
The X,- Y-, or the Z-register is decremented before the operation. Operand address is the decremented contents of the X-,
Y-, or the Z-register.
Data Indirect with Post-increment
Figure 8. Data Indirect Addressing with Post-increment
The X-, Y-, or the Z-register is incremented after the operation. Operand address is the content of the X-, Y-, or the Z-register prior to incrementing.
6
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
FLASHEND
0x0000
LSB
FLASHEND
0x0000
1
LSB
Program Memory Constant Addressing using the LPM, ELPM, and SPM Instructions
Figure 9. Program Memory Constant Addressing
Constant byte address is specified by the Z-register contents. The 15 MSBs select word address. For LPM, the LSB selects
low byte if cleared (LSB = 0) or high byte if set (LSB = 1). For SPM, the LSB should be cleared. If ELPM is used, the
RAMPZ Register is used to extend the Z-register.
Program Memory with Post-increment using the LPM Z+ and ELPM Z+ Instruction
Figure 10. Program Memory Addressing with Post-increment
Constant byte address is specified by the Z-register contents. The 15 MSBs select word address. The LSB selects low byte
if cleared (LSB = 0) or high byte if set (LSB = 1). If ELPM Z+ is used, the RAMPZ Register is used to extend the Z-register.
0856I–AVR–07/10
7
Direct Program Addressing, JMP and CALL
FLASHEND
3116
OP6 MSB
16 LSB
PC
210
150
0x0000
FLASHEND
PC
150
0x0000
Figure 11. Direct Program Memory Addressing
Program execution continues at the address immediate in the instruction word.
Indirect Program Addressing, IJMP and ICALL
Figure 12. Indirect Program Memory Addressing
Program execution continues at address contained by the Z-register (i.e., the PC is loaded with the contents of the Zregister).
8
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
FLASHEND
1
0x0000
Relative Program Addressing, RJMP and RCALL
Figure 13. Relative Program Memory Addressing
Program execution continues at address PC + k + 1. The relative address k is from -2048 to 2047.
WDRWatchdog Reset(see specific descr. for WDR)None1
Notes:1. This instruction is not available in all devices. Refer to the device specific instruction set summary.
2. Not all variants of this instruction are available in all devices. Refer to the device specific instruction set summary.
3. Cycle times for Data memory accesses assume internal memory accesses, and are not valid for accesses via the external
RAM interface.
4. One extra cycle must be added when accessing Internal SRAM.
5. Number of clock cycles for Reduced Core tinyAVR.
#Clocks
XMEGA
0856I–AVR–07/10
15
ADC – Add with Carry
Description:
Adds two registers and the contents of the C Flag and places the result in the destination register Rd.
Operation:
(i)Rd ← Rd + Rr + C
Syntax:Operands:Program Counter:
(i)ADC Rd,Rr0 ≤ d ≤ 31, 0 ≤ r ≤ 31PC ← PC + 1
16-bit Opcode:
000111rdddddrrrr
Status Register (SREG) Boolean Formula:
ITHSVNZC
––⇔⇔⇔⇔⇔⇔
H:Rd3•Rr3+Rr3•R3
+R3•Rd3
Set if there was a carry from bit 3; cleared otherwise
S: N ⊕ V, For signed tests.
V:Rd7•Rr7•R7
+Rd7•Rr7•R7
Set if two’s complement overflow resulted from the operation; cleared otherwise.
N:R7
Set if MSB of the result is set; cleared otherwise.
Z:R7
• R6 •R5• R4 •R3 •R2 •R1 •R0
Set if the result is $00; cleared otherwise.
C:Rd7•Rr7+Rr7•R7
+R7•Rd7
Set if there was carry from the MSB of the result; cleared otherwise.
R (Result) equals Rd after the operation.
Example:
; Add R1:R0 to R3:R2
add r2,r0; Add low byte
adcr3,r1; Add with carry high byte
Words: 1 (2 bytes)
Cycles: 1
16
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
ADD – Add without Carry
Description:
Adds two registers without the C Flag and places the result in the destination register Rd.
Operation:
(i)Rd ← Rd + Rr
Syntax:Operands:Program Counter:
(i)ADD Rd,Rr0 ≤ d ≤ 31, 0 ≤ r ≤ 31PC ← PC + 1
16-bit Opcode:
000011rdddddrrrr
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––⇔⇔⇔⇔⇔⇔
H:Rd3•Rr3+Rr3•R3
+R3•Rd3
Set if there was a carry from bit 3; cleared otherwise
S: N ⊕ V, For signed tests.
V:Rd7•Rr7•R7
+Rd7•Rr7•R7
Set if two’s complement overflow resulted from the operation; cleared otherwise.
N:R7
Set if MSB of the result is set; cleared otherwise.
Z:R7
• R6 •R5• R4 •R3 •R2 •R1 •R0
Set if the result is $00; cleared otherwise.
C:Rd7 •Rr7 +Rr7 •R7
+ R7 •Rd7
Set if there was carry from the MSB of the result; cleared otherwise.
R (Result) equals Rd after the operation.
Example:
addr1,r2; Add r2 to r1 (r1=r1+r2)
addr28,r28; Add r28 to itself (r28=r28+r28)
Words: 1 (2 bytes)
Cycles: 1
0856I–AVR–07/10
17
ADIW – Add Immediate to Word
Description:
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:
(i)Rd+1:Rd ← Rd+1:Rd + K
Syntax:Operands:Program Counter:
(i)ADIW Rd+1:Rd,Kd ∈ {24,26,28,30}, 0 ≤ K ≤ 63PC ← PC + 1
16-bit Opcode:
10010110KKddKKKK
Status Register (SREG) and Boolean Formula:
ITHSVNZC
–– –⇔⇔⇔⇔⇔
S: N ⊕ V, For signed tests.
V:Rdh7
• R15
Set if two’s complement overflow resulted from the operation; cleared otherwise.
N:R15
Set if MSB of the result is set; cleared otherwise.
Set if there was carry from the MSB of the result; cleared otherwise.
R (Result) equals Rdh:Rdl after the operation (Rdh7-Rdh0 = R15-R8, Rdl7-Rdl0=R7-R0).
Example:
adiw r25:24,1 ; Add 1 to r25:r24
adiw ZH:ZL,63 ; Add 63 to the Z-pointer(r31:r30)
Words: 1 (2 bytes)
Cycles: 2
18
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
AND – Logical AND
Description:
Performs the logical AND between the contents of register Rd and register Rr and places the result in the destination register Rd.
Operation:
(i)Rd ← Rd • Rr
Syntax:Operands:Program Counter:
(i)AND Rd,Rr0 ≤ d ≤ 31, 0 ≤ r ≤ 31PC ← PC + 1
16-bit Opcode:
001000rdddddrrrr
Status Register (SREG) and Boolean Formula:
ITHSVNZC
–– –⇔0⇔⇔ –
S: N ⊕ V, For signed tests.
V:0
Cleared
N:R7
Set if MSB of the result is set; cleared otherwise.
Z:R7
•R6 •R5 •R4 •R3• R2 •R1 •R0
Set if the result is $00; cleared otherwise.
R (Result) equals Rd after the operation.
Example:
andr2,r3; Bitwise and r2 and r3, result in r2
ldir16,1; Set bitmask 0000 0001 in r16
andr2,r16; Isolate bit 0 in r2
Words: 1 (2 bytes)
Cycles: 1
0856I–AVR–07/10
19
ANDI – Logical AND with Immediate
Description:
Performs the logical AND between the contents of register Rd and a constant and places the result in the destination register Rd.
Operation:
(i)Rd ← Rd • K
Syntax:Operands:Program Counter:
(i)ANDI Rd,K16 ≤ d ≤ 31, 0 ≤ K ≤ 255PC ← PC + 1
16-bit Opcode:
0111KKKKddddKKKK
Status Register (SREG) and Boolean Formula:
ITHSVNZC
–– –⇔
0
⇔⇔ –
S: N ⊕ V, For signed tests.
V:0
Cleared
N:R7
Set if MSB of the result is set; cleared otherwise.
Z:R7
•R6• R5•R4 •R3• R2• R1• R0
Set if the result is $00; cleared otherwise.
R (Result) equals Rd after the operation.
Example:
andi r17,$0F; Clear upper nibble of r17
andi r18,$10; Isolate bit 4 in r18
andi r19,$AA; Clear odd bits of r19
Words: 1 (2 bytes)
Cycles: 1
20
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
b7-------------------b0C
ASR – Arithmetic Shift Right
Description:
Shifts all bits in Rd one place to the right. Bit 7 is held constant. Bit 0 is loaded into the C Flag of the SREG. This operation
effectively divides a signed value by two without changing its sign. The Carry Flag can be used to round the result.
Operation:
(i)
Syntax:Operands:Program Counter:
(i)ASR Rd0 ≤ d ≤ 31PC ← PC + 1
16-bit Opcode:
1001010ddddd0101
Status Register (SREG) and Boolean Formula:
ITHSVNZC
–– –⇔⇔⇔⇔⇔
S: N ⊕ V, For signed tests.
V:N ⊕ C (For N and C after the shift)
N:R7
Set if MSB of the result is set; cleared otherwise.
Z:R7
•R6 •R5• R4 •R3 •R2• R1• R0
Set if the result is $00; cleared otherwise.
C:Rd0
Set if, before the shift, the LSB of Rd was set; cleared otherwise.
R (Result) equals Rd after the operation.
Example:
ldir16,$10; Load decimal 16 into r16
asrr16; r16=r16 / 2
ldir17,$FC; Load -4 in r17
asrr17; r17=r17/2
Words: 1 (2 bytes)
Cycles: 1
0856I–AVR–07/10
21
BCLR – Bit Clear in SREG
Description:
Clears a single Flag in SREG.
Operation:
(i)SREG(s) ← 0
Syntax:Operands:Program Counter:
(i)BCLR s0 ≤ s ≤ 7PC ← PC + 1
16-bit Opcode:
100101001sss1000
Status Register (SREG) and Boolean Formula:
ITHSVNZC
⇔⇔⇔⇔⇔⇔⇔⇔
I:0 if s = 7; Unchanged otherwise.
T:0 if s = 6; Unchanged otherwise.
H:0 if s = 5; Unchanged otherwise.
S:0 if s = 4; Unchanged otherwise.
V:0 if s = 3; Unchanged otherwise.
N:0 if s = 2; Unchanged otherwise.
Z:0 if s = 1; Unchanged otherwise.
C:0 if s = 0; Unchanged otherwise.
Example:
bclr0; Clear Carry Flag
bclr 7; Disable interrupts
Words: 1 (2 bytes)
Cycles: 1
22
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
BLD – Bit Load from the T Flag in SREG to a Bit in Register
Description:
Copies the T Flag in the SREG (Status Register) to bit b in register Rd.
Operation:
(i)Rd(b) ← T
Syntax:Operands:Program Counter:
(i)BLD Rd,b0 ≤ d ≤ 31, 0 ≤ b ≤ 7PC ← PC + 1
16 bit Opcode:
1111100ddddd0bbb
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
; Copy bit
bstr1,2; Store bit 2 of r1 in T Flag
bldr0,4; Load T Flag into bit 4 of r0
Words: 1 (2 bytes)
Cycles: 1
0856I–AVR–07/10
23
BRBC – Branch if Bit in SREG is Cleared
Description:
Conditional relative branch. Tests a single bit in SREG and branches relatively to PC if the bit is cleared. This instruction
branches relatively to PC in either direction (PC - 63 ≤ destination ≤ PC + 64). The parameter k is the offset from PC and is
represented in two’s complement form.
Operation:
(i)If SREG(s) = 0 then PC ← PC + k + 1, else PC ← PC + 1
Syntax:Operands:Program Counter:
(i)BRBC s,k0 ≤ s ≤ 7, -64 ≤ k ≤ +63PC ← PC + k + 1
PC ← PC + 1, if condition is false
16-bit Opcode:
111101kkkkkkksss
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
cpir20,5; Compare r20 to the value 5
brbc 1,noteq; Branch if Zero Flag cleared
...
noteq:nop; Branch destination (do nothing)
Words: 1 (2 bytes)
Cycles: 1 if condition is false
2 if condition is true
24
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
BRBS – Branch if Bit in SREG is Set
Description:
Conditional relative branch. Tests a single bit in SREG and branches relatively to PC if the bit is set. This instruction
branches relatively to PC in either direction (PC - 63 ≤ destination ≤ PC + 64). The parameter k is the offset from PC and is
represented in two’s complement form.
Operation:
(i)If SREG(s) = 1 then PC ← PC + k + 1, else PC ← PC + 1
Syntax:Operands:Program Counter:
(i)BRBS s,k0 ≤ s ≤ 7, -64 ≤ k ≤ +63PC ← PC + k + 1
PC ← PC + 1, if condition is false
16-bit Opcode:
111100kkkkkkksss
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
bstr0,3; Load T bit with bit 3 of r0
brbs 6,bitset ; Branch T bit was set
...
bitset: nop; Branch destination (do nothing)
Words: 1 (2 bytes)
Cycles: 1 if condition is false
2 if condition is true
0856I–AVR–07/10
25
BRCC – Branch if Carry Cleared
Description:
Conditional relative branch. Tests the Carry Flag (C) and branches relatively to PC if C is cleared. This instruction branches
relatively to PC in either direction (PC - 63 ≤ destination ≤ PC + 64). The parameter k is the offset from PC and is represented in two’s complement form. (Equivalent to instruction BRBC 0,k).
Operation:
(i)If C = 0 then PC ← PC + k + 1, else PC ← PC + 1
Syntax:Operands:Program Counter:
(i)BRCC k-64 ≤ k ≤ +63PC ← PC + k + 1
PC ← PC + 1, if condition is false
16-bit Opcode:
111101kkkkkkk000
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
addr22,r23; Add r23 to r22
brcc nocarry; Branch if carry cleared
...
nocarry: nop; Branch destination (do nothing)
Words: 1 (2 bytes)
Cycles: 1 if condition is false
2 if condition is true
26
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
BRCS – Branch if Carry Set
Description:
Conditional relative branch. Tests the Carry Flag (C) and branches relatively to PC if C is set. This instruction branches relatively to PC in either direction (PC - 63 ≤ destination ≤ PC + 64). The parameter k is the offset from PC and is represented
in two’s complement form. (Equivalent to instruction BRBS 0,k).
Operation:
(i)If C = 1 then PC ← PC + k + 1, else PC ← PC + 1
Syntax:Operands:Program Counter:
(i)BRCS k -64 ≤ k ≤ +63PC ← PC + k + 1
PC ← PC + 1, if condition is false
16-bit Opcode:
111100kkkkkkk000
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
cpir26,$56; Compare r26 with $56
brcs carry; Branch if carry set
...
carry: nop; Branch destination (do nothing)
Words: 1 (2 bytes)
Cycles: 1 if condition is false
2 if condition is true
0856I–AVR–07/10
27
BREAK – Break
Description:
The BREAK instruction is used by the On-chip Debug system, and is normally not used in the application software. When
the BREAK instruction is executed, the AVR CPU is set in the Stopped Mode. This gives the On-chip Debugger access to
internal resources.
If any Lock bits are set, or either the JTAGEN or OCDEN Fuses are unprogrammed, the CPU will treat the BREAK instruction as a NOP and will not enter the Stopped mode.
This instruction is not available in all devices. Refer to the device specific instruction set summary.
Operation:
(i)On-chip Debug system break.
Syntax:Operands:Program Counter:
(i)BREAKNonePC ← PC + 1
16-bit Opcode:
1001010110011000
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Words: 1 (2 bytes)
Cycles: 1
28
AVR Instruction Set
0856I–AVR–07/10
AVR Instruction Set
BREQ – Branch if Equal
Description:
Conditional relative branch. Tests the Zero Flag (Z) and branches relatively to PC if Z is set. If the instruction is executed
immediately after any of the instructions CP, CPI, SUB or SUBI, the branch will occur if and only if the unsigned or signed
binary number represented in Rd was equal to the unsigned or signed binary number represented in Rr. This instruction
branches relatively to PC in either direction (PC - 63 ≤ destination ≤ PC + 64). The parameter k is the offset from PC and is
represented in two’s complement form. (Equivalent to instruction BRBS 1,k).
Operation:
(i)If Rd = Rr (Z = 1) then PC ← PC + k + 1, else PC ← PC + 1
Syntax:Operands:Program Counter:
(i)BREQ k-64 ≤ k ≤ +63PC ← PC + k + 1
PC ← PC + 1, if condition is false
16-bit Opcode:
111100kkkkkkk001
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
cpr1,r0; Compare registers r1 and r0
breq equal; Branch if registers equal
...
equal: nop; Branch destination (do nothing)
Words: 1 (2 bytes)
Cycles: 1 if condition is false
2 if condition is true
0856I–AVR–07/10
29
BRGE – Branch if Greater or Equal (Signed)
Description:
Conditional relative branch. Tests the Signed Flag (S) and branches relatively to PC if S is cleared. If the instruction is executed immediately after any of the instructions CP, CPI, SUB or SUBI, the branch will occur if and only if the signed binary
number represented in Rd was greater than or equal to the signed binary number represented in Rr. This instruction
branches relatively to PC in either direction (PC - 63 ≤ destination ≤ PC + 64). The parameter k is the offset from PC and is
represented in two’s complement form. (Equivalent to instruction BRBC 4,k).
Operation:
(i)If Rd ≥ Rr (N ⊕ V = 0) then PC ← PC + k + 1, else PC ← PC + 1
Syntax:Operands:Program Counter:
(i)BRGE k-64 ≤ k ≤ +63PC ← PC + k + 1
PC ← PC + 1, if condition is false
16-bit Opcode:
111101kkkkkkk100
Status Register (SREG) and Boolean Formula:
ITHSVNZC
––––––––
Example:
cpr11,r12; Compare registers r11 and r12
brge greateq; Branch if r11 ≥ r12 (signed)
...
greateq: nop; Branch destination (do nothing)
Words: 1 (2 bytes)
Cycles: 1 if condition is false
2 if condition is true
30
AVR Instruction Set
0856I–AVR–07/10
Loading...
+ 130 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.