What is Register?

Micro controller operations mostly involve processing data. Registers is used to do operations in CPU.

Registers are special storages in CPU with capacity of certain bits. In 8-bit AVR, a register is 8 bits wide which can hold data of 8-bits (1 byte). In nutshell, they do like this:

7 6 5 4 3 2 1 0

Note the numeration of these bits: the least significant bit starts with zero (20 = 1).

A register can either store numbers from 0 to 255 (positive number, no negative values), or numbers from -128 to +127 (whole number with a sign bit in bit 7), or a value representing an ASCII-coded character (e.g. ‘A’), or just eight single bits that do not have something to do with each other (e.g. for eight single flags used to signal eight different yes/no decisions).

The special character of registers, compared to other storage sites, is that

  • they can be used directly in assembler commands,
  • operations with their content require only a single command word,
  • they are connected directly to the central processing unit called the accumulator,
  • they are source and target for calculations.

Registers are commonly divided into three categories:

  1. General purpose registers
  2. Control & Status registers
  3. Segment registers

A general purpose registers are registers which is used to hold data for operations. They can be used as operand which will be operated by ALU (Arithmetic & Logic Unit), or as result of the operations.

The Control and Status registers are used to control the micro controller. They are used to determine the control flow of the program. Many instructions involve comparisons and mathematical calculations. Those operations will changes some part of flags. Other conditional instruction test the value of these status flags to take the control to other location.

Segment registers are registers which is used to point different segment in the memory. Segments are specific areas defined in a program for containing data or instruction.

Registers in AVR

General Purpose Register

AVR micro controllers have 32 general purpose 8-bit registers, named R0 to R31. All arithmetic operations operate on those registers, only load and store instructions access RAM. A limited number of instruction 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.

Control and Status Register

In addition to those 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

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.

The Carry flag is used for shift and rotate instructions.

If the Global Interrupt Flag is set, the mcu will perform the Interrupt Service Routine corresponding to the interrupt that occurred.

Just remember: They are important for mathematical operations, and changing them between calculating a value and comparing it with something else might be fatal. That’s why Interrupt Service routines should preserve the SREG and any other registers they use (unless these registers are unused in normal code). An interrupt might occur between comparing two values with each other and a following branch – the ISR might change status flags and corrupt the flags the branch relies on.

Segment Registers

AVR micro controller has some 8-bits segment registers, named: RAMPX, RAMPY, RAMPZ, RAMPD, and EIND. These registers are prepended to 16-bit addresses in order to form 24-bit address; only available in parts with large address spaces.

You should remember that AVR use modified Harvard architecture which separate the instructions and the data.It is different to Intel x86 where instruction and data are stored in same memory.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">