Addressing modes in 8051: The CPU can access data in various ways. The data could be in register or in memory or may be provided as an immediate value. These various ways of accessing data are called addressing modes and they are can’t be changed by the programmer. The 8051 provides a total of 5 addressing modes –
In this article we will discuss all these available addressing modes in 8051 microcontroller in details. There are some other addressing modes for computer architecture also available but 8051 only supports these 5 out of them.
Immediate Addressing Mode
In the immediate addressing mode, the source operand is always a constant. Here, when the instruction is assembled, the operand comes immediately after the opcode. The immediate data must be proceed by the sign “#”. This addressing mode can be used to load information into any of the available registers that 8051 has including the DPTR (Data Pointer). For example –
MOV A, #25H
MOV R4, #62H
MOV DPTR, #54H
Register Addressing Mode
It involves the use of registers to hold the data to be manipulated. The size of the source and the destination register must match here. In other words “MOV DPTR, A” will give you an error since the source is an 8-bit register and destination is a 16-bit register. For example –
MOV A, R0
MOV R2, A
ADD A, R7
Direct Addressing Mode
There are 128 bytes of RAM in 8051. The RAM has been assigned location 00 to 7FH. The following is a summery of allocation of these 128 bytes.
- RAM location 00 to 1FH are assigned to the register banks and stacks.
- RAM location 20 to 2FH are set aside as bit addressable space to save single bit data.
- RAM location 30 to 7FH are available as a place to save byte size data.
Using direct addressing mode we can access the entire 128 bytes of RAM. This is due to the fact that register bank locations are accessed by the register names of R0 to R7. In the direct addressing mode, the data resides in a RAM memory location whose address is given as a part of the instruction.
Register Indirect Addressing Mode
In this mode, a register is used as a pointer to the data. If the data is inside the CPU, we can use only registers R0 and R1 for this purpose. In other words, R2 to R7 can’t 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, they hold the address of RAM locations and must be proceeded by @ sign. example –
MOV A, @R0;
Move the content of RAM location whose address is hold by R0 into A.
MOV @R, B;
Move the content of B into RAM location whose address is hold by R1.
Index Addressing Mode
This is widely used in accessing data elements of lookup table entries located in the program ROM space of the 8051. The following instruction is used for this purpose:
"MOVC A, @ A + DPTR"
To form the address of the data element stored in on-chip ROM, the 16-bit register DPTR and Register A are used . Because the data elements are stored in the program space ROM of the 8051, it uses the instruction MOVC instead of MOV. The C means code. In this instruction, the contents of register A are added to the 16-bit DPTR register to form the 16-bit address of the needed data. example –
MOVC A, @ A + DPTR
MOV B, A