4.6 Register Usage

The assembly generated from C source code by the compiler will use certain registers in the AVR register set. Some registers are assumed to hold their value over a function call.

The call-used registers, r18-r27 and r30-r31, can be allocated by the compiler for values within a function. Functions do not need to preserve the content of these registers. These registers may be used in hand-written assembler subroutines. Since any C function called by these routines can clobber these registers, the calling routine must ensure they are saved as restored as appropriate.

The call-saved registers, r2-r17 and r28-r29, can also be allocated by the compiler for local data; however, C functions must preserve these registers. Hand-written assembler subroutines are responsible for saving and restoring these registers when necessary. The registers must be saved even when the compiler has assigned them for argument passing.

The temporary register, r0, can be clobbered by C functions, but they are saved by interrupt handlers.

The compiler assumes that the Zero register, r1, always contains the value zero. It can be used in hand-written assembly routine for intermediate values, but must be cleared after use (e.g using clr r1). Be aware that multiplication instructions return their result in the r1-r0 register pair. Interrupt handlers save and clear r1 on entry, and restore r1 on exit (in case it was non-zero).

All registers that have been used by an interrupt routine are save and restored by the interrupt routine (see 4.8.4 Context Switching).

The registers that have a dedicated function throughout the program are tabulated below.

Table 4-7. Registers with Dedicated Use
Register name Applicable devices
r0 Temporary register
r1 (__zero_reg__) Zero register (holds 0 value)
r28, r29 Frame pointer (Y pointer)