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. Atmel was acquired by Microchip Technology in 2016. 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:
  • PC: 16- or 22-bit program counter
  • SP: 8- or 16-bit stack pointer
  • SREG: 8-bit status register
  • RAMPX, RAMPY, RAMPZ, RAMPD and EIND: 8-bit segment registers that are prepended to 16-bit addresses in order to form 24-bit addresses; only available in parts with large address spaces.

    Status register

The status register bits are:
  1. C Carry flag. This is a borrow flag on subtracts.
  2. Z Zero flag. Set when an arithmetic result is zero, and cleared when it is non-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.
There are two special cases which exist to facilitate multi-byte arithmetic:
  • The INC and DEC instructions do not modify the carry flag, so they may be used to loop over arbitrary-precision arithmetic operands.
  • The CPC, SBC and SBCI instructions do not set the Z flag when the result is zero, but only clear it if the result is non-zero. For fixed precision multi-byte comparisons, implemented with an unrolled CP; CPC; CPC; CPC sequence, this produces a zero flag which is set only if the entire difference is zero.

    Addressing

The following address spaces are available:
  • The general purpose registers are addressed by their numbers, although the full 5-bit number is not stored in instructions that can only operate on a subset of those registers.
  • I/O registers have a dedicated 6-bit address space, the lower half of which is bit-addressable; some parts have I/O registers outside this address space, which are called "extended I/O" and are only accessible as memory-mapped I/O in the data address space.
  • The data address space maps the 32 general-purpose registers, all the I/O registers, and the RAM; it can be addressed either directly or indirectly through the X, Y and Z pointer registers, prepended if necessary by RAMPX, RAMPY and RAMPZ respectively.
  • Program memory has a separate address space, addressed as 16-bit words for the purpose of fetching instructions
  • For the purpose of fetching constant data, program memory is addressed bytewise through the Z pointer register, prepended if necessary by RAMPZ.
  • The EEPROM is memory-mapped in some devices; in others, it is not directly addressable and is instead accessed through address, data and control I/O registers.
  • The general purpose registers, the status register and some I/O registers are bit-addressable, with bit 0 being the least significant and bit 7 the most significant.
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 " " through " ", 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
SREG
SP::
EIND
RAMPZ
RAMPY
RAMPX
RAMPD

A typical ATmega memory map may look like:
Data addressI/O addressContents
Registers R0 – R31
I/O registers
I/O registers
Extended I/O registers
Internal SRAM

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

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 long word, 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:
  • Rd and Rr are registers in the range R0–R31
  • Rdh and Rrh are registers in the range R16–R31
  • Rdq and Rrq are registers in the range R16–R23
  • Rp is a register pair R25:R24, R27:R26, R29:R28 or R31:R30
  • XYZ is a pointer register, X or Y or Z
  • XYZ+ is a pointer register, X or Y or Z, postincremented
  • -XYZ is a pointer register, X or Y or Z, predecremented
  • YZ is a pointer register, either Y or Z
  • Z+ is pointer register Z, postincremented
  • s is a bit number in the status register
  • b is a bit number in a general-purpose or I/O register
  • K6 is a 6-bit immediate unsigned constant
  • K8 is an 8-bit immediate constant; since it is used only in 8-bit operations, its signedness is irrelevant
  • IO5 is a 5-bit I/O address covering the bit-addressable part of the I/O address space, i.e. the lower half
  • IO6 is a 6-bit I/O address covering the full I/O address space
  • D16 is a 16-bit data address covering 64 KiB; in parts with more than 64 KiB data space, the contents of the RAMPD segment register is prepended
  • P22 is a 22-bit program address covering 222 16-bit words
  • S7 and S12 are 7-bit and 12-bit signed displacements, in units of words, relative to the program address stored in the program counter
ArithmeticBit & OthersTransferJump
Call
Branch

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:
  • core cpu features, added on more capable CPU cores
  • memory addressing features, added on all models with memory large enough to require them
  • optional features, a few peripherals that may or may not be present on a particular model.
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.