Designed for the C language



The C language is the most used HLL in the world today for MCUs. Since most MCU architectures are developed with assembly programming in mind the support for typical C instructions are poor. ATMELís goal was to develop an architecture that was efficient both for the C language and assembly. With many C experts from C compiler suppliers in the design team, a very code efficient 8-bit micro with 16- bit support is developed.

When programming in C, one general rule is to use variables defined within a routine (local), instead of using global variables known in the whole program. Local variables will only
allocate RAM memory when executing the specific routine while global variables will occupy RAM all the time. To handle local variables fast and code efficient, a lot of general-purpose registers are needed. The AVR has 32 general-purpose registers, all of them in the Arithmetic Logic Unit (ALU) path allowing true single cycle instructions. Figure 2 shows how efficient many registers can be compared to traditional CISC architectures with one accumulator.
Three pairs of the 32 registers can be used as
16-bit pointers allowing indirect jumps and calls as well as many data memory accessing modes directly related to the C language. In addition to that the traditional stack for return address is available through the instruction set. Most MCU architectures have only 1-2 accumulators and 1-2 pointers.

Since pointers are very frequently used in C, the operations on the pointers are very important due to speed and code size. The AVR has addressing modes that directly pre- increments or post-decrements the different pointers when used to access data memory.

In addition to that, table lookup or stack operations can effectively be performed by using displacement (relative to the current pointer value) as shown in Figure 3. The displacement range is 0-64 bytes and detailed analysis shows that the range is sufficient for

most lookups in structures and tables. One very important note is that this fits into a single word instruction!
To be able to change the pointers more than by using the pre-decrement or post-increment modes addition and subtraction between a 16- bit pointer and a constant are implemented in one cycle and a single word. The three pointers can also be used as eight 8-bit or three 16-bit general purpose working registers.

In general it is important to use 8-bit numbers in an 8-bit MCU since all data memories are 8 bit wide. However, when using C, larger number like integers (16 bit), long (32 bit) and float (32 bits floating point) are frequently used. Traditionally, computing on such numbers generates very large code, but since the AVR is designed to handle it, the AVR code generated is extremely small.

Example 1 shows an example where a small part of a routine written in C is compiled to see how it is translated to assembly code.

Example 1:
void routine(void)
long n1, n2;
int n3;
if (n1 != n2) n3 +=5;

n1 and n2 are both 32-bits numbers that require 4 bytes each. n3 is an integer that requires two bytes. All three variables are defined as local variables within the routine.

In most MCUís the comparison from example 1 needs many more instructions since they miss Compare with Carry (CPC) and zero flag propagation. Zero flag propagation has a similar function as Carry propagation, to adjust the higher bytes in a number. The Zero propagation is implemented on compare and subtract instructions. A Compare instruction is generally a subtraction without storing of the result. The CPC is added to support comparisons of larger numbers than 8 bit.

Example 1 does also show how a Subtract Immediate (SUBI) and a Subtract Immediate with Carry (SBCI) can be used instead of addition (ADDI and ADCI) to compute n3 + 5 by subtracting the 2ís complement of 5. Since ADDI and SUBI are complementary instructions only one pair is implemented to leave decoding space for other important instructions.

All the discussed features are implemented as
a result of code benchmark and input from C compiler experts. The result is a very fast microcontroller with RISC performance and CISC code density.

 Home  | ©2008