Addressing Modes in 8051
The operands may be an immediate value, a value in registers or in memory. The way in which the operands are fetched by thee 8051 microcontroller is called as the addressing modes. 8051 supports the following types of addressing modes.
- Immediate addressing mode
- Direct addressing mode
- Register direct addressing mode
- Register indirect addressing mode
- Indexed addressing mode
Immediate Addressing Mode
In this addressing mode, an immediate 8-bit is part of the instruction.
Here source is an immediate data, Destination can be a register or register indirect or direct memory
MOV @Rn, #data
MOV A, #data ; Instruction size is 2 byte, 1-byte opcode(74) and 1-byte data
MOV direct, #data
MOV DPTR,#data ; 3-byte instruction
The #data is an eight bit constant and may be character, string or numeric value of decimal, hex, binary, octal etc.
When this instruction is executed, accumulator will be set for receiving an the constant from the immediate next memory location. Figure 7.1 shows the operation on MOV instruction with immediate addressing. This assume that opcode 74 and immediate data 34h is lying at memory location 202 and 203. As the instruction is fetched for execution PC will point to next memory location 204 as shown in figure
Direct Addressing Mode
This addressing mode is used to transfer a data byte from memory to registers or port and vice versa. The address of the operand (source / or the destination) is given in the instruction, the other operand may be a register, a variable or a port.
- MOV Register/port, Memory
- MOV Memory, Register/port
MOV A, 05h ; two byte instruction
MOV A, direct
It should also be noted that the source address 05 happen to be the address of register R5 in bank-0, so data from R5 is copied in Accumulator
Figure 7.2 shows the execution of the instruction. Assuming that opcode E5 and address 05 are stored in memory 202 and 203. As the instruction is fetched for execution, PC will point to next memory location i.e. 204.
Register Direct Addressing Mode
This addressing mode allows the programmer to reference the registers by their names instead of their memory addresses. Before we can use the register, programmer should be careful in first selecting the correct register bank and then naming the register as an incorrect bank selection may lead to wrong interpretations.
Both in the direct addressing mode and the direct register addressing, the register bank can be selected by setting the bit PSW.3 and PSW.4. The values of the bits PSW.3 and PSW.4 allows one to select register banks as shown in following table:
|PSW.4 (RS1)||PSW.3 (RS0)||Register Bank||Register Bank Status|
MOV A, R4 ; one byte instruction with opcode E5
MOV A, Rn ; general format
The direct register addressing instruction are one byte long, it implies that the named register is implied in the opcode of the instruction itself. So, in register direct addressing mode, data is transferred to accumulator from the register (based on which register bank is selected). Figure 7.3 shows the execution of instruction of this type
Register Indirect Addressing Mode
In this addressing mode, the referred register in the instruction contain the memory address from which the data is to be fetched.
MOV A, @R0 ; This is one byte instruction with op-code as E6
This instruction is also one byte instruction. Here, register R0 contain an address which holds the data to be transferred to accumulator. Figure 7.4 shows the execution of this instruction. Suppose R0 contain the value 20H, and we have a data 2F H stored at the address 20H, then the value 2FH will get transferred to accumulator after executing this instruction.
Indexed Addressing Mode
This addressing mode is used to fetch an instruction or an operand from external program or data memory. It allows data or the code be referred by indexing.
To point to external data memory : @A + DPTR
To point to external Program memory : @A + PC
MOVC A, @A+DPTR ; source operand is at address given by DPTR + Acc
MOVC A, @A+PC ; instruction is at address given by PC + Acc
Figure 7.5 shows the execution of instruction using external program or data memory