Addressing modes of 8051 Microcontroller
The way in which an operand is given to an instruction is known as addressing modes in 8051 microcontroller.
- Immediate addressing mode.
- Register addressing mode.
- Direct addressing mode.
- Indirect addressing mode.
- Indexed addressing mode.
Immediate addressing mode:
In this type of addressing mode, directly data is given to instruction to perform operation. Data and address of a location looks the same( both are numbers ). So a special character ‘#’ is used to differentiate data from addresses.
- MOV A , #16H ; this instruction means move data ( 16H ) to register A, location of 16H is not defined here.
- MOV DPTR #2000H
In any addressing mode, the processor fetches instruction , decodes the instruction and executes it. However in this type of addressing mode , processors immediately can fetch data ( operand ), so this is known as immediate addressing mode.
Here the MOV datatype is used which is used in internal RAM memory operations only. For other memories ( ROM, external RAM other data types are used )
Register addressing mode:
In this type of addressing mode, instead of giving the data directly, the register name is given. Data from one register is moved to other register, data doesn’t get erased from first register. Generally the data is stored in general purpose registers, thus it is named as register addressing mode.
MOV R1 , A ; it means access to the data is moved from register A to register R1. However the data is not removed from A register, it is just copied.
MOV R1 , R0 ; this instruction is not allowed.
Direct addressing mode:
In this type, the address of the data location is given in the instruction , This operation is only for internal RAM and SFRs because it provides an address of only 8-bit.
MOV A , 25H ; data from 25H location is given to register A.
Here # is not used, because it is an address.
MOV 30H , A ; move A register data to 30H location.
MOV 15H , 25H ; move data from 25H location to location 15H .
MOV 01H , 00H ; This instruction is similar to MOV R1 , R0 ;
Because R1 is located at 01H address and similarly others.
But here this instruction is allowed.
Indirect addressing mode:
This addressing mode is for both internal RAM and external RAM.
Indirect addressing mode in internal RAM:
MOV A , @R0 ;
This means copying the data from the location stored in R0.
R0 has data 10, which is address of 23.
It means copy 23 to register A.
MOV R0 , #10H ;
MOV A , R0 ;
These 2 instructions are expanded versions of the indirect addressing mode.
MOV R0 , #10H ; means 10H is data for R0 , not address.
MOV A , R0 ; copy the R0 data to register A
Why is indirect addressing mode when direct addressing mode is simple?
Simple things aren’t always best! Using direct addressing mode in 8051, we can perform single byte operations only. In microcontrollers we don’t perform single byte operations. Using indirect addressing mode we can perform operations like loops.
Let’s consider an example of an array which holds 100 values.
Array = 1
Array = 2
Array = 3
Array = 100
As shown in the picture, In indirect addressing mode we can set the value of i and increment it and then add total code in a loop. So indirect addressing mode is used to move multiple bytes of data.
Move the series of data from the address of a location ( 20H is the address ) to the location 50H .
MOV R0 , #20H ; assign first byte address of 20H location as R0
MOV R1 , #50H ; assign first byte address of 50H location as R1
MOV A , @R0 ; R0 contains location of data, that data( first byte of 20H) is moved to A
MOV @R1 , A ; first byte of 20H is moved to R1
Here R1 has values of 50H
INC R0 ; increment R0
INC R1 ; increment R1
Now put the whole program in a loop. About loops, it will be explained in upcoming tutorials.
Indirect addressing mode in external RAM:
MOVX A , @DPTR ; for external RAM, MOVX is used.
DPTR is a 16-bit address register, but the data in it is 8-bit only( data in DPTR is address of another data location ) .
Index addressing mode:
This addressing mode is the sum of two registers. ROM doesn’t only consist only of program memory, it also has bit data memory in some cases.
MOVC A , @A+DPTR ;
MOVC @A+DPTR , A ; this instruction is not allowed , because ROM is only readable memory, we cannot write in it.
E.G: Code for 8-segment displays never changes, that code is fixed and we are not interested in changing the code, so we store this data in ROM.
In this way there are fixed codes for every number in the 7 segment display.
Then these codes ( here 8 codes ) are stored in a table known as look up table .
Look up table:
|xxxx||Code for number 0|
|xxxx + 1||Code for number 1|
|xxxx + 8||Code for number 9|
xxxx is the base address that could be anything, and to access other codes we have to add another address to it.
Address of a particular code = base address + index of the code
This is the reason this type of addressing is known as index addressing mode. This article covered importance of Addressing modes of 8051 Microcontroller.
In the next tutorials, instruction set would be discussed.
Addressing mode of 8086