Addressing Modes in 8051

Introduction:

The CPU can access the data in various ways. The data could be in a register, or in memory or be 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.

The importance of addressing modes are

1. It helps to reduce the instruction length.

2. It helps to compare different instruction sets.

3. It helps to make instruction set rich.

Types Of Addressing Modes:

The 8051 provides total of five distinct addressing modes. They are as follows:

1. Immediate

2. Register

3. Direct

4. Register Indirect

5. Indexed

Immediate Addressing Mode:

In this addressing mode, the source operand is a constant. In immediate addressing mode, as the name implies, when the instruction is assembled, the operand comes immediately after the opcode. Notice that the immediate data must be preceded by the pound sign, '#'. This addressing mode can be used to load information into any of the registers, including DPTR register.   

MOV A, #25H ;load 25H into A 

MOV R4, #62 ;load the decimal value 62 into R4

MOV B, #40H ;load 40H into B

MOV DPTR, #4521H ;DPTR=4521H

Although the DPTR register is 16-bit, it can also be accessed as two 8-bit registers, DPH and DPL, where DPH is high byte and DPL is low byte. Look at the follwong code.

MOV DPTR, #2550H 

is
same as :

MOV DPL, #50H

MOV DPH, #25H

We can use the EQU directive to access immediate data as shown below.

COUNT EQU 30 

MOV R4, #COUNT ;R4=1E(30=1EH)

MOV DPTR, #MYDATA ;DPTR=200H

ORG 200H
 
MYDATA: DB "America"

Notice that we can also use immediate addressing mode to send data to 8051 ports. For example "MOV P1, #55H" is a valid instruction.

Register Addressing Mode:

Register addressing mode involves the use of registers to hold the data to be manipulated. Examples of register addressing mode as follow.

MOV A,R0 ;copy the content of R0 into A 

MOV R2,A ;copy the contents of A into R2

ADD A,R5 ;add the contents of R5 to contents of A

ADD A,R7 ;add the contents of R7 to contents of A

MOV R6,A ;save accumulator in R6

It should be noted that source and destination registers must match in size. In other words, coding "MOV DPTR,A" will give an error, since the sorce is an 8-bit register and the destination is a 16-bit register.

MOV DPTR, #2575H 

MOV R7,DPL

MOV R6,DPH

Notice that we can move the data between the accumulator and Rn(for n=0-7) but movement of data between Rn register is not allowed. For example, the instruction "MOV R4, R7" is invalid

Direct Addressing Mode:

This addressing mode used to access the data stored either in RAM or registers of the 8051. Here the address describes where the data is mentioned in the instruction. The importance of this addressing mode is used for general purpose program. The SFRs are accessed by only direct addressing mode.

MOV R0,40H     ;move content of RAM location 40H in R0

MOV 56H,A       ;move content of A in RAM location 56H

MOV R4,7FH     ;move content of RAM location 7FH to R4 

Another major use of direct addressing mode is stack. In the 8051 family, only direct addressing mode is allowed for push and pop operations on the stack.

Register Indirect Addressing Mode:

In the register indirect addressing mode, a register used as a pointer to the data. If the data is inside the CPU, only registers R0 and R1 are used for this purpose. In other words, R0-R7 cannot be used to hold the address of an operand located in RAM when using this addressing mode. When R0 and R1 are used as pointers, that is, when they hold the addresses of RAM locations, they must be preceded by the "@" sign.

MOV A,@R0  ;move content of the RAM location whose address is held by R0 into A

MOV @R1,B  ;move contents of B into RAM location whose address is held by R1

One of  the advantage of register indirect addressing mode is that it makes accessing data dynamic rather than static as in the case of direct addressing mode.

Indexed Addressing Mode:

Indexed addressing mode is widely used in accessing data elements of look-up table entries located in the program ROM space of the 8051. The instruction used for this purpose is "MOVC A,@A+DPTR". The 16-bit register DPTR and register A are used to form the address of the data element stored stored in on-chip ROM. Because the data elements are stored in the program space ROM of the 8051, the instruction MOVC is used instead of MOV. In this instruction the content of A are added to the 16-bit register DPTR to form the 16-bit address of the needed data.