AVR ARCHITECTURE AND ASSEMBLY LANGUAGE PROGRAMMING
CPUs use registers to store data temporarily.
1
To program in Assembly language, we must understand the registers and
architecture of a given CPU and the role they play in processing data
Registers are used to store information temporarily. That
information could be:
A byte of data to processed
Address pointing to data to be fetched
Majority of registers in AVR are 8 bits
TMP 2026
AVR ARCHITECTURE AND ASSEMBLY LANGUAGE PROGRAMMING
The 8 bits of a register are shown in the diagram below. These range from
2 the MSB (most-significant bit) D7 to the LSB (least-significant bit) D0.
With an 8-bit data type, any data larger than 8 bits must be broken into 8-
bit chunks before it is processed.
In AVR there are 32 general purpose registers. They are R0–R31 and are
located in the lowest location of memory address. All of these registers are
8 bits
TMP 2026
LDI instruction
The general purpose registers in AVR are the same as the accumulator in
3 other microprocessors. They can be used by all arithmetic and logic
instructions.
To understand the use of the general purpose registers, we will show it in the
context of two simple instructions: LDI and ADD.
1. LDI instruction
The LDI instruction copies 8-bit data into the general purpose registers. It has
the following format:
LDI Rd,K ;load Rd (destination) with Immediate value K ; d must be between 16 and 31
K is an 8-bit value that can be 0–255 in decimal, or 00–FF in hex, and Rd
is R16 to R31 (any of the upper 16 general purpose registers).
The I in LDI stands for ”immediate.” If we see the word “immediate” in any
instruction, we are dealing with a value that must be provided right there
with the instruction.
TMP 2026
LDI instruction
The following instruction loads the R20 register with a value of 0x25 (25 in
4 hex).
LDI R20,0x25 ;load R20 with 0x25 (R20 = 0x25)
The following instruction loads the R31 register with the value 0x87 (87 in
hex).
LDI R31,0x87 ;load 0x87 into R31 (R31 = 0x87)
The following instruction loads R25 with the value 0x15 (15 in hex and 21
in decimal).
LDI R25,0x79 ;load 0x79 into R25 (R25 = 0x79)
Note: We cannot load values into registers R0 to R15 using the LDI instruction.
For example, the following instruction is not valid:
LDI R5,0x99 ;invalid instruction
TMP 2026
LDI instruction
Notice the position of the source and destination operands. As you can
5 see, the LDI loads the right operand into the left operand. In other words,
the destination comes first.
To write a comment in Assembly language we use ‘;’. It is the same as ‘//‘
in C language, which causes the remainder of the line of code to be
ignored.
For instance, in the above examples the expressions mentioned after ‘;’ just
explain the functionality of the instructions to you, and do not have any
effects on the execution of the instructions.
TMP 2026
LDI instruction
When programming the GPRs of the AVR microcontroller with an
6 immediate value, the following points should be noted:
1. If we want to present a number in hex, we put a dollar sign ($) or a 0x in
front of it. If we put nothing in front of a number, it is in decimal. For
example,
“LDI R16,50”, R16 is loaded with 50 in decimal,
“LDI R16,0x50”, R16 is loaded with 50 in hex.
2. If values 0 to F are moved into an 8-bit register such as GPRs, the rest of the
bits are assumed to be all zeros. For example, in “LDI R16,0x5” the result
will be R16 = 0x05; that is, R16 = 00000101 in binary.
3. Moving a value larger than 255 (FF in hex) into the GPRs will cause an error.
LDI R17, 0x7F2 ;ILLEGAL $7F2 > 8 bits ($FF)
TMP 2026
ADD instruction
2. ADD Instruction
7
The ADD instruction has the following format:
ADD Rd,Rr ;ADD Rr to Rd and store the result in Rd
The ADD instruction tells the CPU to add the value of Rr to Rd and put the
result back into the Rd register.
To add two numbers such as 0x25 and 0x34, one can do the following:
LDI R16,0x25 ;load 0x25 into R16
LDI R17,0x34 ;load 0x34 into R17
ADD R16,R17 ;add value R17 to R16 (R16 = R16 + R17)
Executing the above lines results in R16 = 0x59 (0x25 + 0x34 = 0x59)
TMP 2026
AVR Data Memory
There are two kinds of memory space in AVR microcontroller
Code memory
Data memory
Programmes are stored in code memory and data in data memory
TMP 2026
AVR Data Memory
Data memory is
composed of three parts:
GPR (General purpose
Registers)
9 I/O memory and
Internal data SRAM
TMP 2026
GPR (General purpose register)
The GPRs use 32 bytes of data memory space. Why?
The address locations range is $00-$1F regardless of the AVR chip
10
TMP 2026
I/O Memory (SFRs)
I/O memory is dedicated to specific functions such as status
register, timers, Analog- to – digital converters, I/O ports, serial
communication etc. with registers 8 bits in size
The function of each I/O memory location is fixed at design time as
it is used for control of microcontroller peripheral
The number of locations in the data memory set aside for I/O
memory depends on pin numbers and peripheral functions
supported by the chip
11
TMP 2026
I/O Memory (SFRs)
All AVRs have at least 64bytes of I/O memory locations which is
called standard I/O memory
In some AVR with more than 32 pins, there is also some extended
I/O memory containing registers for controlling the extra ports and
extra peripheral.
I/O registers are called SFRs ( special function registers) since each
one is dedicated to specific function.
12
TMP 2026
I/O Memory (SFRs)
13
TMP 2026
Internal Data SRAM (Scratch pad)
Internal data SRAM is widely used for storing data and parameters
by AVR programmers and C compilers
Each location of the SRAM can be accessed directly by its address
Each location is 8 bits and can be used to store any data we want
14
Size of the SRAM can vary from chip to chip
TMP 2026
Internal Data SRAM (Scratch pad)
NOTE
Each location in the data memory has a unique address called data
memory and each I/O register has a relative address in comparison
to the I/O memory called I/O address
15
TMP 2026
16
TMP 2026
The status Register
The AVR has an 8 bit register called status register which indicate
some conditions called conditional flags that result when an
instruction is executed.
The conditional flags can be used to perform conditional branch as
17 will be shown later in our study
TMP 2026
The status Register
C, the Carry flag: This flag is set whenever there is a carry out
18 from the D7 bit and is affected after an 8 – bit addition or
subtraction.
Z, the Zero flag: the zero flag reflects the result of an arithmetic or
logic operation. If the result is zero, then Z= 1 and Z= 0 if the result
TMP 2026
is not zero.
The status Register
N, the Negative flag: Binary representation of signed numbers uses
D7 as sign bit.
The negative flag reflects the result of an arithmetic operation.
19 If the D7 bit of the result is zero, then N=0 and the result is
positive.
If D7 bit is one, then N = 1and the result negative. The negative and
V flag bits are used for the signed number arithmetic operations.
TMP 2026
The status Register
V, the Overflow flag: This flag is set whenever the result of a
signed number is too large, causing the high order bit to overflow
into the sign bit.
In general, the carry flag is used to detect errors in unsigned
arithmetic operations while the overflow flag is used to detect
20 errors in signed arithmetic operations.
The V and N flags bits are used for signed number arithmetic
operations,
TMP 2026
The status Register
S, the Sign flag: This flag is the result of Exclusive –Oring of N
and V flags.
21
TMP 2026
The status Register
H, the Half carry flag: If there is a carry from D3 and D4 during
and ADD or SUB operation, this bit is set otherwise it is cleared.
This flag is used by instructions that perform BCD (binary coded
decimal arithmetic.
T, the Bit Copy storage (Temporary) flag: It is used when one
22 wishes to copy a bit of data from one General purpose register to
another.
I, the Global Interrupt Enable flag: Is responsible for enabling
and disabling the interrupts globally.
TMP 2026
Internal SRAM Vs EEPROM
The EEPROM in AVR is used for storing data that should rarely be
changed and should not be lost when power is off.
Internal data SRAM on the other hand is used for storing data and
parameter that are frequently changed.
23
TMP 2026
Code Memory and Programme Counter
The code memory space is made up of the program counter (PC)
and the program ROM space (Program memory).
Programme Counter is used by the CPU to point to the address of
the next instruction to be executed.
24
Its is the most important register in AVR microcontroller
TMP 2026
AVR ARCHITECTURE AND ASSEMBLY PROGRAMMING
Code Memory and Programme Counter
The purpose of the program counter is to hold/store the address of
the next instruction to be executed by the microcontroller's
microprocessor.
The size (width) of the program counter of a microcontroller is
25 measured in bits and is directly related to the size of the
microcontroller's program memory.
The wider the PC, the more memory locations a CPU can access.
TMP 2026
Code Memory and Programme Counter
Instructions are stored in the program memory of a microcontroller
and thus the PC width is directly related to the size of the
microcontroller program memory.
The size or width of the program counter (PC) is basically the
26 smallest possibly number of bits necessary to address the
microcontroller program memory based on its organisation. If N is
the width of the PC, then N would be related to the size of the
program memory by the following equation:
TMP 2026
Code Memory and Programme Counter
2N = Size of program memory
log (Size of program memory)
N =
log (2)
27
TMP 2026
Using Instructions with Data Memory
AVR allows direct access to other locations in the data memory.
The instruction is LDS
LDS (LoaD direct from data space)
28 LDS Rd, K ; load Rd with contents of location K
; K is the address between $0000-$FFFF
The LDS instruction tells the CPU to load (copy) one byte from an
address in the data memory to the GPR
TMP 2026
Using Instructions with Data Memory
The GPR will have the same value as the location in the data
memory if LDS is executed.
Data can be in GPR, internal data SRAM or I/O memory
29
TMP 2026
Using Instructions with Data Memory
STS (STore direct from data Space)
STS K, Rr ; Store register content into location K
; K is the address between $0000-$FFFF
30 The STS instruction tells the CPU to store (copy) the contents of
the GPR to an address location in the data memory space.
TMP 2026
Using Instructions with Data Memory
NOTE
One cannot copy (store) an immediate value directly into the
SRAM location in the AVR. It can only be done via GPRs
31
TMP 2026
Using Instructions with Data Memory
IN instruction (IN from I/O location)
IN Rd, A ; load an I/O location to GPR
; (d is between 0-31 and A is between 0-63)
IN instruction tells the CPU to load one byte from an I/O register to
32 GPR.
When instruction is executed, the GPR will have the same value as
the I/O register.
TMP 2026
Using Instructions with Data Memory
OUT instruction (OUT to I/O location)
OUT A, Rr ; Store register to I/O location to GPR
; (r is between 0-31 and A is between 0-63)
The out instruction tells the CPU to store the GPR to the I/O
33 register and after its execution, the I/O register will have the same
value as the GPR
Don’t copy an immediate value to an I/O register nor to an SRAM
location
IN R0, PINC
TMP 2026
OUT PORTA, RO
Using Instructions with Data Memory
MOV instruction (OUT to I/O location)
MOV Rd, Rr ; Copy from Rr to Rd
; Rd and Rr can be any GPR
This instruction is used to copy data memory among GPR register
34 of R0-R31
TMP 2026