Program and Data addressing modes in AVR
The Instruction set of a processor is like the vocabulary to command the processor.
Program and Data Addressing Modes:
The instructions can be categorized based on how they access data and operate upon it. This is called the program and data addressing modes of the processor.
The various AVR instructions can be categorized in about 10 different addressing modes.
Each instruction has an 2 parts:
Opcode: indicates to ALU what to do.
Operands: on which the opcode operates.
Example | Decription |
---|---|
INC R0 | ++i; |
DEC R5 | --j; |
LSL R9 | Shift R9, X<<1; |
Two registers are involved.
Rs: Source register
Rd: Destination register
Instruction reads the two registers and operates on their contents and stores the result back into the destination register.
Example | Description |
---|---|
ADD R1, R3 | i = i+j; //i+=j; |
SUB R5, R7 | i = i-j; |
SUBI R4, 8 ;Subtract Constant from Register
ADIW R26, 5 ;Add Immediate to Word
; R27:R26 ← R27:R26 + 5 --
like
i-=8; a+=29;
These instructions are used to access the I/O space, I/O Registers, but not Extended I/O Registers.
The I/O registers can be accessed using
in Rd, PORTADDRESS
out PORTADDRESS, Rs
Rd
, Rs
: any register from register file.PORTADDRESS
: any register fromC
unsigned char i = PINB; unsigned char k = 45; PORTC = k;
accomplished using
IN R10, PINB
if the input to PORTB is 0x03 and all the pins are configured as an inputs, R10 becomes 0x03
OUT PORTC, R1
the contents of R1 are used to set the output register of PORTC
Table with Register Addresses, and Corresponding Memory Address:
STS K, Rs
LDS RD, K
Note writing a constant to memory requires writing it to a register first
Similar to the data direct type.
But they are one word each, and a pointer register (X, Y, or Z) is used that has the base address of the data memory.
One variant is depicted in this this figure:
Look at Page 378 of data sheet
Examples:
Indirect
LD Rd, X
Indirect with post-increment
LD Rd, X+
Indirect with displacement (indirect with index)
LDD Rd, Y+q
Indirect with pre-decrement
ST -Y, Rs
The I/O ports can also be accessed using SRAM access commands, e.g. ST and LD.
Add 0x20 to the port number (the first 32 addresses are the registers!)
Example:
.DEF SomeRegister = R16
LDI ZH,HIGH(PORTB+32)
LDI ZL,LOW(PORTB+32)
LD SomeRegister,Z
For I/O registers located in extended I/O map,
I/O register direct commands like "IN", "OUT", "SBIS", "SBIC", "CBI", and "SBI" cannot be used.
Use direct (memory) and indirect (memory) instructions that allow access to extended I/O, typically "LDS" and "STS" combined with "SBRS", "SBRC", "SBR", and "CBR" if modifying a single pin
CALL k ;
CLC
RET
In these types of instructions, the Z register is used to point to
the program memory. (Up to 64 Kbytes of program memory)
IJMP
;Indirect Jump to (Z) PC ← Z
ICALL
;Indirect Call to (Z) PC ← Z,STACK = PC + 1
RCALL k
;Relative Subroutine Call PC ← PC + k + 1 , STACK=PC+1
RJMP k
;Relative Jump PC ← PC + k + 1