Module 3
AVR ATMega32
Addressing Modes
Addressing Modes
➢ The CPU can access data in various ways. The data could be in a register,
or in memory, or provided as an immediate value. These various ways of
accessing data are called addressing modes.
➢ The various addressing modes of a microprocessor are determined when
it is designed, and therefore cannot be changed by the programmer.
➢ There are 5 basic addressing modes for the AVR
o Register Direct
• Single Register
• Two Registers
o I/O Direct
o Immediate
o Data Direct
o Data Indirect
• Data Indirect with Post Increment
• Data Indirect with Pre Decrement
• Data Indirect with Displacement
20112012-I Module 3/2
Addressing Modes: Instruction Format
20112012-I Module 3/3
Addressing Modes: Register Direct-Single
➢ Instructions can operate on any of the 32 registers
▪ The group of 32 registers are referred to as the Register File
➢ The microcontroller:
▪ Reads the data in the register
▪ Operates on the data in the register
▪ Stores the results back in the register
12 bits 4 bits
GPRs
Op. Code Rd 0
Examples:
d
INC R16
CLR R22
31
20112012-I Module 3/4
Addressing Modes: Register Direct-Single
20112012-I Module 3/5
Addressing Modes: Register Direct-Two Registers
o Instructions can operate on any of the 32 registers
• One of theses register is the source register Rs and one is the destination
register Rd
6 bits 5 bits 5 bits
GPRs
Op. Code Rr Rd 0
31
20112012-I Module 3/6
Addressing Modes: Register Direct-Two Registers
20112012-I Module 3/7
Addressing Modes: Register Direct-Two Registers
20112012-I Module 3/8
Addressing Modes: Register Direct-Two Registers
20112012-I Module 3/9
Addressing Modes: Register Direct-Two Registers
20112012-I Module 3/10
Addressing Modes: I/O Indirect
I/O Memory
15 5 0 0
Op. Code Rr/Rd A
63
20112012-I Module 3/11
Addressing Modes: I/O Indirect
20112012-I Module 3/12
Addressing Modes: I/O Indirect
20112012-I Module 3/13
Addressing Modes: I/O Indirect
20112012-I Module 3/14
Addressing Modes: I/O Indirect
20112012-I Module 3/15
Addressing Modes: I/O Indirect
20112012-I Module 3/16
Addressing Modes: Immediate
• LDI Rd,K 4 bits 8 bits 4 bits
GPRs
– LDI R19,25 Op. Code Immediate Rd 0
• SUBI Rd,K d
– SUBI R23,5 ;R23 = R23 – 5
31
• ANDI Rd,K
– ANDI R21,0x15
20112012-I Module 3/17
Addressing Modes: Immediate
20112012-I Module 3/18
Addressing Modes: Immediate
20112012-I Module 3/19
Addressing Modes: Data Direct
31 20 19 16 Data Space
Op. Code Rr/Rd
o LDS Rd, address Data Address
0
• LDS R19, 0x313 15 0
o STS address, Rs
• STS 0x95, R19
Note: RAMEND has been used to
represent the highest location in
RAMEND
data space.
20112012-I Module 3/20
Addressing Modes: Data Direct
20112012-I Module 3/21
Addressing Modes: Data Direct
20112012-I Module 3/22
Addressing Modes: Data Indirect
— In AVR devices without SRAM, Data Indirect Addressing is called Register
Indirect Addressing. Register Indirect Addressing is a subset od Data
Indirect Addressing since the data space from 0 to 31 is the Register File
20112012-I Module 3/23
Addressing Modes: Data Indirect
Data Space
15 0 0
X, Y, OR Z - REGISTER
Note: RAMEND has been used to represent
the highest location in data space. RAMEND
20112012-I Module 3/24
Addressing Modes: Data Indirect
20112012-I Module 3/25
Addressing Modes: Data Indirect
20112012-I Module 3/26
Addressing Modes: Data Indirect
20112012-I Module 3/27
Addressing Modes: Data Indirect
20112012-I Module 3/28
Addressing Modes: Data Indirect
20112012-I Module 3/29
Addressing Modes: Data Indirect
20112012-I Module 3/30
Addressing Modes:Data Indirect with Post Increment
• Post increment is available with X, Y, or Z
– LD Rd, X+
• Load indirect with post-increment
– ST Z+,Rr
• Store indirect with post-increment
– Do not use the same registers for pointers and data as
such operations are undefined
15 0 Data Space
X, Y, OR Z - REGISTER 0
1 +
RAMEND
20112012-I Module 3/31
Addressing Modes:Data Indirect with Post Increment
• Typically used in a looping structure to
sequentially access memory by increasing
addresses
– Set Y to first byte
– Access repeatedly using post-increment to point to the
next byte after each access
– LD Rd, Y+
Y:
4C 3F 20 A0 18 3C 2F
20112012-I Module 3/32
Addressing Modes:Data Indirect with Pre Decrement
• Typically used in a looping structure to sequentially
access memory by decreasing addresses
15 0 Data Space
• Set Y to 1 + address of X, Y, OR Z - REGISTER 0
last first byte
• Access repeatedly
-1 +
using pre-decrement to
point to the byte
accessed
RAMEND
LD Rd, -Y
Y:
4C 3F 20 A0 18 3C 2F
20112012-I Module 3/33
Addressing Modes:Data Indirect with Displacement
Indirect with displacement is available only with Y and Z
o The displacement q is limited to 0-63
• The displacement is unsigned (positive)
o LDD Rd, Y+q
• LoaD indirect wit Displacement
15 0 Data Space
o STD Z+q, Rr Y OR Z - REGISTER 0
• STore indirect with Displacement
+
Y:
15
Op. Rr/Rd q
4C 3F 20 A0 18 3C 2F
15 10 65 0
+4
RAMEND
20112012-I Module 3/34
Addressing Modes: Data Indirect Summary
20112012-I Module 3/35
Addressing Modes: Examples-1
20112012-I Module 3/36
Addressing Modes: Examples-2
20112012-I Module 3/37
Addressing Modes: Examples-2
LDI R19, 0x5 ;R19 = 5 (R19 for counter)
LDI R16, 0x55 ;load R16 with value 0x55 (value to be copied)
LDI YL ,LOW($140) ;load the low byte of Y with value 0x40
LDI YH, HIGH($140) ;load the high byte of Y with value 0x1
L1: ST Y+ ,R16 ;copy R16 to memory location Y
DEC R19 ;decrement the counter
BRNE L1 ;loop until counter = zero
20112012-I Module 3/38
Addressing Modes: Examples-3
20112012-I Module 3/39
ASCII Table
20112012-I Module 3/40
Addressing Modes: Examples-3
20112012-I Module 3/41