EE-222 Microprocessor Systems
Introduction to CPU Architecture
Week2-Lecture 2/3
Dr. Sara Shakil Qureshi
AVR CPU & Registers
3
AVR CPU & Registers
• To do programming, its essential to understand the
working of CPU and AVR registers.
4
CPU Registers Special
• AVR’s CPU has Purpose
▫ 1: General Purpose Registers
Registers
▫ 2: Program Counter (PC) –
Address of next instruction
▫ 3: Stack Pointer (SP) – Points
to top of stack
▫ 4: Status Register (SREG) –
contains info about result of
last executed instruction
▫ 5: Instruction Register
(IR) – Holds fetched
instruction
5
6
AVR: General Purpose
Registers
• 32 8-bit Regs.
• R0-R31
▫ R0-R15
Different
▫ R16-R31 Stores
both data
and
addresses
General RAM EEPROM Timers
Purpose
PROGRAM
Registers
ROM
Program CPU Data
Bus Bus
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
7
General Purpose Registers & ALU
• The fast-access Register File contains:
▫ 32 x 8-bit general purpose working registers
▫ with a single clock cycle access time.
• This allows single-cycle Arithmetic Logic Unit (ALU) operation.
• In a typical ALU operation:
▫ two operands are output from the Register File
▫ the operation is executed,
▫ and the result is stored back in the Register File.
All in one clock cycle.
8
Stack Pointer
AVR Programming
10
11
Why Assembly Language for
AVR??
• Access to actual registers in microcontroller.
▫ Machine language uses ‘0’ & ‘1’.
▫ Compare to assembly language? Mnemonic codes
12
Assembly Language – Pros &
Cons
Advantages Disadvantages
Access registers and Time consuming to write
memory easily code
Efficient use of memory Lack of portability
No need of compiler to Limited algorithm design
optimize code
13
Loading values General purpose
registers ldi is part of immediate instructions that
operate on a register and a constant value.
LDI (Load Immediate)
- Only works with R16-R31
LDI Rd, k -d between 16-31
Its equivalent in high level elanguages: s R 0 to Rd
e gist r .
=k in t or u ct ion
v alues DI instr
Order of the
ot lo
ad instruction
g t h eL and operands.
C ann u sin
Instruction
R15 followed by destination and then the
input.
load r16 with the value 85.
https://ww1.microchip.com/downloads/en/devicedoc/AVR-Instruction-Set-Manual-DS40002198A.pdf
14
Try
Arithmetic Operations ADC &
• ADD SUBC
▫ Takes two registers as input, compute their sum and store
value in first.
Result stored Comments
back in r16
• SUB
▫ Takes two registers as input, compute their difference and
store value in first.
Explore
15
Write a program that calculates 19+95?
16
Write a program that calculates 19+95+5?
17
http://www.rjhcoding.com/avr-asm-regist
18
Arithmetic Operations
• INC & DEC
▫ Increase or decrease the value in a register by 1. i.e. INC Rd
Rd=Rd+1
▫ Work on any of the 32 registers.
Try
ADIW &
• ADIW & SBIW SBIW
▫ Operates on 16-bit values i.e. words.
▫ Can be used on registers R24-R31.
▫ Work on register pairs for which the lower part is an
even number i.e. r25:r24.
Home Task
19
General Purpose Registers Rules
• Moving Larger Value will cause an error
• Values moved between 0 – F will cause the rest
of the bits to be ZERO
• Hex numbers requires 0x in front
AVR Data Memory
21
Data Address Space
Two Kinds of
Memory Spaces: RAM EEPROM Timers
General
• Code Memory Purpose
PROGRAM
• Data Memory ROM
Registers
Program CPU Data
Bus Bus
Data Data
R0
Address R1
Address
R2
$0000
General $0000
General Interrupt Other
OSC Ports
...
Purpose Purpose Unit Peripherals
...
...
R31
$001F Registers $001F Registers
$0020 $0020
Standard I/O I/O Address Standard I/O I/O
Registers $00 Registers PINS
...
...
$01
(SFRs) (SFRs)
$005F $005F
...
$0060 $0060
$3E
Extended SREG $3F Extended
...
I/O Memory I/O Memory
$00FF $01FF
$0100 $0200
Internal Internal
Data memory composed
...
...
SRAM SRAM
$21FF
External
$2200
External
of:
$FFFF
SRAM
$FFFF
SRAM • GPRs
ATmega328
ATmega640/V
ATmega1280/V
• I/O Memory
ATmega64
ATmega128
ATmega1281/V
ATmega2560/V
• Internal data SRAM
ATmega2561/V
22
AVR Data Memory: Data Memory
Space
• Data memory composed of:
▫ GPRs
▫ I/O Memory
▫ Internal data SRAM
23
AVR Data Memory Space: GPRs
• GPRs:
▫ Take 32 bytes of data memory
▫ Address location $00 - $1F (always)
24
AVR Data Memory Space: I/O
Memory
• I/O Memory (SFR, Special Function Registers):
▫ Dedicated to specific functions suchI/O
as:
memory fixed by
Status register designer at time of
Timers design
As used for control of the
Serial communication etc microcontroller or
peripherals.
▫ I/O Memory locations depends on the number of pins
and peripheral functions supported by a device
At least all the families have 64 bytes (Standard I/O
Memory)
25
AVR Data Memory Space: I/O
Memory
26
AVR Data Memory Space: Internal
SRAM
• Internal SRAM:
▫ aka Scratch Pad memory
▫ Used for storing data and parameters.
To store data brought into CPU via I/O and serial
ports.
SRAM
vs.
EEPROM??
AVR Data Address Space
Address Name Address Name Address Name
Mem. I/O Mem. I/O Mem. I/O
$20 $00 - $36 $16 TIFR1 $4C $2C SPCR0
$21 $01 - $37 $17 TIFR2 $4D $2D SPSR0
$22 $02 $38 $18 - General RAM EEPROM Timers
- $4E $2E SPDR0
$23 $03 PINB $39 $19 - $4F $2F - Purpose
Be careful about the
$3A $1A - PROGRAM
$24 $04 DDRB $50 $30 ACSR Registers
$25 $05 PORTB $3B $1B PCIFR ROM$51 $31 DWDR
$26 $06 PINC $3C $1C EIFR $52 $32 -
Program
use of registers
CPU
with
$27 $07 DDRC $3D $1D EIMSK $53 $33 SMCR Data
$28 $08 PORTC $3E $1E GPIOR0 $54 Bus
$34 MCUSR Bus address bus
$29 $09 PIND $3F $1F EECR $55 $35 MCUCR data bus
control bus
the instructions…
$2A $0A EEDR
$2B $0B
DDRD
PORTD
$40
$41
$20
$21
Data
EEARL
$56
$57
$36
$37
-
SPMCSR
$2C $0C - $42 $22 Bus
EEARH $58 $38 -
$2D
Data $0D - $43 $23 GTCCR Data $59 $39 -
R0
Address$0E
$2E - TCCR0A Address $5A
$44 R1 $24 $3A -
$2F
$0000 $0F
General - $45 R2 $25 TCCR0B $0000 $5B $3B -
General Interrupt Other
...
$30 $10Purpose - $46 $26 TCNT0 $5C
Purpose $3C -OSC Ports
...
...
$31
$001F $11
Registers- $47
R31
$27 OCR0A $001F Registers
$5D $3D SPL Unit Peripherals
$32 $12 - $48 $28 OCR0B $5E $3E SPH
$0020 $0020
$33 Standard I/O
$13 - $49
I/O Address
$29 $00 - Standard
$5F I/O $3F SREG
Registers Registers I/O
$34 $14
Example: Add contents of location
Store 0x90
0x53 to contents of
...
...
- $4A $2A $01 GPIOR1 (SFRs)
(SFRs)
$35
$005F $15 TIFR0 $4A $2A GPIOR2 $005F Example: into
PINS
the SPH
...
$0060
Example:
location 0x95
$0060
What does
andLDS
STS the
store following
the
(Load
(Store
register. result
direct
The instruction
in
from
direct to
addresslocation
data
data
of do?
0x313.
space)
space)
SPH is 0x5E
Example: Write a program that stores 55 into location
$3E
Extended SREG
Example: Write
$3F
I/O Memory a program that copies the contents of
Extended
...
I/O Memory
Solution:
0x80
LDSof$01FF
location RAM.
R20,2
0x80 of RAM STSinto
Rd,location
addr 0x81.
$00FF
$0100 $0200 LDS addr,Rd ; ;Rd =
Internal
LDS
Internal
R20, 0x90 Solution:
[addr] ;R20 = [0x90]
Solution: [addr]=Rd
...
...
SRAM SRAM
Answer: $21FF
LDS External
R21, 0x95
$2200
LDI =;R21
Example: R20,= 0x53
[0x95] ;R20 = 0x53
External Solution:
LDI R20, 55 ;R20 55
$FFFF
SRAM It copies
ADD R20,the contents
SRAM
$FFFF R21 STS of;R20
R2
0x5E,into
= R20
R20R20;
+ R21as 2 is the
;SPH = R20
ATmega328
STS
LDS
address 0x80,
R20,
of0x313,
R2. R20
ATmega640/V R20
0x80 LDS
STS
;[0x80] R1,
=
;R20 0x60
0x60,R15
R20
= = 55
[0x80] ;
STS ATmega1280/V ;[0x313] = R20
ATmega64
ATmega128
ATmega1281/V
STS ATmega2560/V
0x81, R20 [0x60] =;[0x81]
R15 = R20 = [0x80]
ATmega2561/V
AVR Data Address Space
General RAM EEPROM Timers
Purpose
PROGRAM
Registers
ROM
Program CPU Data
Bus Bus
Data Data
R0
Address R1
Address
R2
$0000 General $0000 General
...
Purpose Purpose Interrupt Other
...
Example: Write a program that adds thePorts
contentsPeripherals
of the
...
R31
$001F Registers $001F OSC
Registers
Unit
address bus
$0020
Standard I/O PINC $0020
I/OStandard
register
I/O to the IN
Using (IN
Names
contents
data from IO
bus of PIND location)
of I/Oandregisters
stores the
OUT (OUT bus to I/O location)
I/O Address
$00 control
Registers
Data
Registers
...
...
(SFRs) result$005F
$01
in location
(SFRs) 0x90 of the SRAM I/O
$005F
Bus PINS
...
$0060 $0060
IN Rd,IOaddress ;Rd =
Solution:
$3E
Example:
OUT
[addr]IOAddr,Rd ;
Extended SREG $3F Extended
...
I/O Memory I/O Memory
[addr]=Rd
$00FF $01FF
$0100 INInternal OUT =SPH,R12
R20,PINC ;R20
$0200 PINC ;OUT 0x3E,R12
Internal
Example:
...
...
SRAM SRAM
IN R21,PIND ;R21
$21FF IN =R15,SREG
Example:PIND ;IN R15,0x3F
$2200
External External
ADDSRAM R20,R21 ;R20IN= R1,
R20 0x3F
+ ;R1 = SREG
R21
$FFFF
SRAM
$FFFF OUT 0x3F,R12 ;SREG =
ATmega328
ATmega640/V
STS R12 IN R17,0x3E
0x90,R20 ;[0x90]
ATmega1280/V = R20 ;R17
ATmega64
ATmega128
ATmega1281/V
ATmega2560/V = SPH
ATmega2561/V OUT 0x3E,R15 ;SPH =
R15
29
More ALU Instructions Puts result
back into
the same
• MOV Rd, Rr ;Rd=Rr (0<d,r<31) register
30
Conclusion
• Introduction to assembly language
▫ Pros & Cons
▫ AVR Instruction set
• AVR Data memory space
31
Reading Material
• Textbook:
▫ Chapter 5 - Complete
▫ Chapter 2, Section 2.4-2.7
32
Questions?