Trisha Shetty (Editor)

Atmel AVR instruction set

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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.

Contents

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 (R27:R26), Y (R29:R28) and Z (R31:R30). 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 (8-bit operations) or to register pairs R25:R24–R31:R30 (16-bit operations ADIW and SBIW). 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 bits

    The status register bits are:

    1. C Carry flag. This is a borrow flag on subtracts.
    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. 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 general purpose registers are addressed by their numbers (0–31), 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 (including those also accessible through the I/O address space), 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 (flash) 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 "0x00 (0x20)" through "0x3F (0x5F)", 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 (RAMPX, RAMPY) may not be present on machines with less than 64 KiB of addressable memory.

    A typical ATmega memory map may look like:

    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 (ADIW and SBIW) which take two cycles.

    RAM and I/O space can be accessed only by copying to or from registers. Indirect access (including optional postincrement, predecrement or constant displacement) 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 (LPM) 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 (BRxx) can test an ALU flag and jump to specified address. Skips (SBxx) test an arbitrary bit in a register or I/O and skip the next instruction if the test was true.

    In the following:

  • Rd is a register in the range R0-R31 or R16-R31 (depending on instruction)
  • Rr is a register in the range R0-R31
  • s is a bit number in the status register (0 = C, 1 = Z, etc., see the list above)
  • b is a bit number in a general-purpose or I/O register (0 = least significant, 7 = most significant)
  • K6 is a 6-bit immediate unsigned constant (range: 0–63)
  • 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 (range: 0–31)
  • IO6 is a 6-bit I/O address covering the full I/O address space (range: 0–63)
  • 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 (i.e. 8 MiB)
  • S7 and S12 are 7-bit (resp. 12-bit) signed displacements relative to the program address stored in the program counter
  • 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.

    Instruction encoding

    Bit assignments:

  • rrrrr = Source register
  • rrrr = Source register (R16-R31)
  • rrr = Source register (R16-R23)
  • RRRR = Source register pair (R0:R1 ... R30:R31)
  • ddddd = Destination register
  • dddd = Destination register (R16-R31)
  • ddd = Destination register (R16-R23)
  • DDDD = Destination register pair (R0:R1 ... R30:R31)
  • pp = Register pair, W, X, Y or Z
  • y = Y/Z register pair bit (0=Z, 1=Y)
  • u = FMUL(S(U)) signed with 0=signed or 1=unsigned
  • s = Store/load bit (0=load, 1=store)
  • c = Call/jump (0=jump, 1=call)
  • cy = With carry (0=without carry 1=with carry)
  • e = Extend indirect jump/call address with EIND (0=0:Z, 1=EIND:Z)
  • q = Extend program memory address with RAMPZ (0=0:Z, 1=RAMPZ:Z)
  • aaaaaa = I/O space address
  • aaaaa = I/O space address (first 32 only)
  • bbb = Bit number
  • B = Bit value
  • kkkkkk = 6-bit unsigned constant
  • KKKKKKKK = 8-bit constant
  • 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.

    References

    Atmel AVR instruction set Wikipedia