Atmel AVR instruction set


The Atmel AVR instruction set is the machine language for the Atmel AVR, a modified Harvard architecture 8-bit RISC single chip microcontroller which was developed by Atmel in 1996. The AVR was one of the first microcontroller families to use on-chip flash memory for program storage.

Processor registers

There are 32 general-purpose 8-bit registers, R0–R31. All arithmetic and logic operations operate on those registers; only load and store instructions access RAM.
A limited number of instructions operate on 16-bit register pairs. The lower-numbered register of the pair holds the least significant bits and must be even-numbered. The last three register pairs are used as pointer registers for memory addressing. They are known as X, Y and Z. Postincrement and predecrement addressing modes are supported on all three. Y and Z also support a six-bit positive displacement.
Instructions which allow an immediate value are limited to registers R16–R31 or to register pairs R25:R24–R31:R30. Some variants of the MUL operation are limited to eight registers, R16 through R23.

Special purpose registers

In addition to these 32 general-purpose registers, the CPU has a few special-purpose registers:
The status register bits are:
  1. C Carry flag. This is a borrow flag on subtracts. The INC and DEC instructions do not modify the carry flag, so they may be used to loop over multi-byte arithmetic operations.
  2. Z Zero flag. Set to 1 when an arithmetic result is zero.
  3. N Negative flag. Set to a copy of the most significant bit of an arithmetic result.
  4. V Overflow flag. Set in case of two's complement overflow.
  5. S Sign flag. Unique to AVR, this is always N⊕V, and shows the true sign of a comparison.
  6. H Half-carry flag. This is an internal carry from additions and is used to support BCD arithmetic.
  7. T Bit copy. Special bit load and bit store instructions use this bit.
  8. I Interrupt flag. Set when interrupts are enabled.

    Addressing

The following address spaces are available:
The first 64 I/O registers are accessible through both the I/O and the data address space. They have therefore two different addresses. These are usually written as "0x00 " through "0x3F ", where the first item is the I/O address and the second, in parentheses, the data address.
The special-purpose CPU registers, with the exception of PC, can be accessed as I/O registers. Some registers may not be present on machines with less than 64 KiB of addressable memory.
RegisterI/O addressData address
SREG0x3F0x5F
SP0x3E:0x3D0x5E:0x5D
EIND0x3C0x5C
RAMPZ0x3B0x5B
RAMPY0x3A0x5A
RAMPX0x390x59
RAMPD0x380x58

A typical ATmega memory map may look like:
Data addressI/O addressContents
0x0000 – 0x001FRegisters R0 – R31
0x0020 – 0x003F0x00 – 0x1FI/O registers
0x0040 – 0x005F0x20 – 0x3FI/O registers
0x0060 – 0x00FFExtended I/O registers
0x0100 – RAMENDInternal SRAM

where RAMEND is the last RAM address. In parts lacking extended I/O the RAM would start at 0x0060.

Instruction timing

Arithmetic operations work on registers R0–R31 but not directly on RAM and take one clock cycle, except for multiplication and word-wide addition which take two cycles.
RAM and I/O space can be accessed only by copying to or from registers. Indirect access is possible through registers X, Y, and Z. All accesses to RAM takes two clock cycles. Moving between registers and I/O is one cycle. Moving eight or sixteen bit data between registers or constant to register is also one cycle. Reading program memory takes three cycles.

Instruction list

Instructions are one 16-bit word long, save for those including a 16-bit or 22-bit address, which take two words.
There are two types of conditional branches: jumps to address and skips. Conditional branches can test an ALU flag and jump to specified address. Skips test an arbitrary bit in a register or I/O and skip the next instruction if the test was true.
In the following:
ArithmeticBit & OthersTransferJumpBranchCall
ADD Rd, Rr
ADC Rd, Rr
ADIW Rp+1:Rp, K6
SUB Rd, Rr
SUBI Rdh, K8
SBC Rd, Rr
SBCI Rdh, K8
SBIW Rp+1:Rp, K6
INC Rd
DEC Rd
AND Rd, Rr
ANDI Rdh, K8
OR Rd, Rr
ORI Rdh, K8
EOR Rd, Rr
COM Rd
NEG Rd
CP Rd, Rr
CPC Rd, Rr
CPI Rdh, K8
SWAP Rd
LSR Rd
ROR Rd
ASR Rd
MUL Rd, Rr
MULS Rdh, Rrh
MULSU Rdq, Rrq
FMUL Rdq, Rrq
FMULS Rdq, Rrq
FMULSU Rdq, Rrq
BSET s
BCLR s
SBI IO5, b
CBI IO5, b
BST Rd, b
BLD Rd, b
NOP
BREAK
SLEEP
WDR
MOV Rd, Rr
MOVW Rd+1:Rd, Rr+1:Rr
IN Rd, IO6
OUT IO6, Rr
PUSH Rr
POP Rr
LDI Rdh, K8
LDS Rd, D16
LD Rd, X
LDD Rd, YZ+K6
LD Rd, -XYZ
LD Rd, XYZ+
STS D16, Rr
ST X, Rr
STD YZ+K6, Rr
ST -XYZ, Rr
ST XYZ+, Rr
LPM
LPM Rd, Z
LPM Rd, Z+
ELPM
ELPM Rd, Z
ELPM Rd, Z+
SPM
RJMP S12
IJMP
EIJMP
JMP P22
CPSE Rd, Rr
SBRC Rr, b
SBRS Rr, b
SBIC IO5, b
SBIS IO5, b
BRBC s, S7
BRBS s, S7
RCALL S12
ICALL
EICALL
CALL P22
RET
RETI

Instruction set inheritance

Not all instructions are implemented in all Atmel AVR controllers. This is the case of the instructions performing multiplications, extended loads/jumps/calls, long jumps, and power control.
The optional instructions may be grouped into three categories:
While higher-end processors tend to have both more capable cores and more memory, the presence of one does not guarantee the presence of the other.

Core CPU instructions

Beginning with the original "classic" core, enhancements are organized into the following levels, each of which includes all the preceding:
  1. The "Classic" core has only the zero-operand form of the LPM instruction, which is equivalent to LPM r0,Z.
  2. "Classic plus" adds the MOVW instruction for moving register pairs, and the more general form of the LPM instruction which permit an arbitrary destination register and auto-increment of the Z pointer.
  3. "Enhanced" cores add the multiply instructions.
  4. The XMEGA cores do not add new instructions per se, but make some significant changes:
  5. * The memory map is reorganized, eliminating memory-mapping of the processor register file and expanding the I/O port range. Now the first 4K is special function registers, the second 4K is data flash, and normal RAM begins at 8K.
  6. * It is not necessary to explicitly disable interrupts before adjusting the stack pointer registers ; any write to SPL automatically disables interrupts for 4 clock cycles to give time for SPH to be updated.
  7. * Other multi-byte registers are provided with shadow registers to enable atomic read and write. When the lowest-order byte is read, the higher-order bytes are copied to the shadow registers, so reading them later produces a snapshot of the register at the time of the first read. Writes to low-order bytes are buffered until the highest-order byte is written, upon which the entire multi-byte register is updated atomically.
  8. Later XMEGA cores add four atomic read-modify-write instructions: exchange, load-and-set, load-and-clear, and load-and-toggle. These help coordinate with direct memory access peripherals, notably a USB controller.
Less capable than the "classic" CPU cores are two subsets: the "AVR1" core, and the "AVR tiny". Confusingly, "ATtiny" branded processors have a variety of cores, including AVR1, classic, classic+ and AVRtiny.
The AVR1 subset was not popular and no new models have been introduced since 2000. It omits all RAM except for the 32 registers mapped at address 0–31 and the I/O ports at addresses 32–95. The stack is replaced by a 3-level hardware stack, and the PUSH and POP instructions are deleted. All 16-bit operations are deleted, as are IJMP, ICALL, and all load and store addressing modes except indirect via Z.
A second, more successful attempt to subset the AVR instruction set is the "AVR tiny" core.
The most significant change is that the AVRtiny core omits registers R0–R15. The registers are also not memory-mapped, with I/O ports from 0–63 and general-purpose RAM beginning at address 64. The 16-bit arithmetic operations are omitted, as are the load/store with displacement addressing modes, but the predecrement and postincrement addressing modes are retained. The LPM instruction is omitted; instead program ROM is mapped to the data address space and may be accessed with normal load instructions.
Finally, the AVRtiny core deletes the 2-word LDS and STS instructions for direct RAM addressing, and instead uses the opcode space previously assigned to the load/store with displacement instructions for new 1-word LDS and STS instructions which can access the first 128 locations of general-purpose RAM, addresses 0x40 to 0xBF.

Memory addressing instructions

The smallest cores have ≤256 bytes of data address space and ≤8192 bytes of program ROM. These have only an 8-bit stack pointer, and only support the 12-bit relative jump/call instructions RJMP/RCALL.
Additional memory addressing capabilities are present as required to access available resources:
  1. Models with >256 bytes of data address space have a 16-bit stack pointer, with the high half in the SPH register.
  2. Models with >8 KiB of ROM add the 2-word JUMP and CALL instructions.
  3. Models with >64 KiB of ROM add the ELPM instruction and corresponding RAMPZ register. LPM instructions zero-extend the ROM address in Z; ELPM instructions prepend the RAMPZ register for high bits. This is not the same thing as the more general LPM instruction; there exist "classic" models with only the zero-operand form of ELPM. When auto-increment is available, it updates the entire 24-bit address including RAMPZ.
  4. models with >128 KiB of ROM have a 3-byte program counter. Subroutine calls and returns use an additional byte of stack space, there is a new EIND register to provide additional high bits for indirect jumps and calls, and there are new extended instructions EIJMP and EICALL which use EIND:Z as the destination address.
  5. models with >64 KiB of RAM address space extend the 16-bit RAM addressing limits with RAMPX, RAMPY, RAMPZ and RAMPD registers. These provide additional high bits for addressing modes which use the X, Y, or Z register pairs, respectively, or the direct addressing instructions LDS/STS. Unlike ROM access, there are no distinct "extended" instructions; instead the RAMP registers are used unconditionally.

    Optional feature instructions

Three instructions are present only on models which have the corresponding hardware facility
Architectures other than AVR1 are named according to avr-libc conventions.
FamilyMembersArithmeticBranchesTransfersBit-Wise
Minimal AVR1 CoreAT90S1200
ATtiny11
ATtiny12
ATtiny15
ATtiny28
ADD
ADC
SUB
SUBI
SBC
SBCI
AND
ANDI
OR
ORI
EOR
COM
NEG
SBR
CBR
INC
DEC
TST
CLR
SER
RJMP
RCALL
RET
RETI
CPSE
CP
CPC
CPI
SBRC
SBRS
SBIC
SBIS
BRBS
BRBC
BREQ
BRNE
BRCS
BRCC
BRSH
BRLO
BRMI
BRPL
BRGE
BRLT
BRHS
BRHC
BRTS
BRTC
BRVS
BRVC
BRIE
BRID
LD
ST
MOV
LDI
IN
OUT
LPM
SBI
CBI
LSL
LSR
ROL
ROR
ASR
SWAP
BSET
BCLR
BST
BLD
SEC
CLC
SEN
CLN
SEZ
CLZ
SEI
CLI
SES
CLS
SEV
CLV
SET
CLT
SEH
CLH
NOP
SLEEP
WDR
Classic Core up to 8K Program Space AT90S2313
AT90S2323
ATtiny22
AT90S2333
AT90S2343
AT90S4414
AT90S4433
AT90S4434
AT90S8515
AT90C8534
AT90S8535
ATtiny26
new instructions:
ADIW
SBIW
new instructions:
IJMP
ICALL
new instructions:
LD
LDD
LDS
ST
STD
STS
PUSH
POP
AVR2, with MOVW and LPM instructions
  • ATa5272
  • ATtiny13/a
  • ATtiny2313/a
  • ATtiny24/a
  • ATtiny25
  • ATtiny261/a
  • ATtiny4313
  • ATtiny43u
  • ATtiny44/a
  • ATtiny45
  • ATtiny461/a
  • ATtiny48
  • ATtiny828
  • ATtiny84/a
  • ATtiny85
  • ATtiny861/a
  • ATtiny87
  • ATtiny88
new instructions:
  • MOVW
  • LPM
  • Classic Core with up to 128K ATmega103 ATmega603
    AT43USB320
    AT76C711
    new instructions:
    JMP
    CALL
    new instructions:
    ELPM
    Enhanced Core with up to 8K ATmega8
    ATmega83
    ATmega85
    ATmega8515
    new instructions:
    MUL
    MULS
    MULSU
    FMUL
    FMULS
    FMULSU
    new instructions:
    MOVW
    LPM
    SPM
    Enhanced Core with up to 128K ATmega16
    ATmega161
    ATmega163
    ATmega32
    ATmega323
    ATmega64
    ATmega128
    AT43USB355
    AT94
    AT90CAN series
    AT90PWM series
    ATmega48
    ATmega88
    ATmega168
    ATmega162
    ATmega164
    ATmega324
    ATmega328
    ATmega644
    ATmega165
    ATmega169
    ATmega325
    ATmega3250
    ATmega645
    ATmega6450
    ATmega406
    ELPMX new instructions:
    BREAK
    Enhanced Core with up to 4M ATmega640
    ATmega1280
    ATmega1281
    ATmega2560
    ATmega2561
    new instructions:
    EIJMP
    EICALL
    XMEGA Core ATxmega seriesnew instructions:
    DES
    new instructions:

    XCH
    LAS
    LAC
    LAT
    Reduced AVRtiny Core ATtiny40
    ATtiny20
    ATtiny10
    ATtiny9
    ATtiny5
    ATtiny4
    Identical to classic core with up to 8K, with the following exceptions:
    LPM
    LDD
    STD
    LD
    LDS
    STS
    Reduced CPU register set

    Instruction encoding

    Bit assignments:
    The Atmel AVR uses many split fields, where bits are not contiguous in the instruction word. The load/store with offset instructions are the most extreme example where a 6-bit offset is broken into three pieces.