ADDRESSING MODES OF 8051

on Tuesday, 17 September 2013
The CPU can access data in many ways. The data could be in registers or any memory location or direct values. These different ways of accessing the data is nothing but Addressing modes.
We have 5 Addressing modes in 8051, they are
1. Immediate
2. Register
3. Direct
4. Register Indirect
5. Indexed

1.IMMEDIATE ADDRESSING MODE:
In this mode, source will be a constant. Name itself says that the data will be moved immediately. Look at the below example
Mov A,#30H                          
Mov R0,#20
Mov R3,#45H
Mov DPTR,#2520H             ;DPTR is a 16bit register

2. REGISTER ADDRESSING MODE:
In this mode the registers are used to hold the data. Examples are shown below
Mov A,B
Mov B,R0
Mov A,R5
Mov R4,A

Note that we cannot move data between Rn registers as a source and destination. Example Mov R1,R5 is invalid
In order to move data from R5 to R1 we can write in this way
Mov A,R5
Mov R1,A

Above two modes we have seen them before in our previous posts. Look at the new modes below.






3.DIRECT ADDRESSING MODE:
In direct mode, we access the data directly from RAM locations i.e we use RAM address to access the data.
Do you remember these points which we have discussed before
  • RAM locations 00-1F are kept aside for Register banks(bank0 to 3)
  • Locations 20-2F are kept aside for bit addressing
  • From 30 to 7F are kept aside for scratch pad( rough book) 
In order to access the Register Banks we have predefined name to access them i.e R0, R1, R2 and so on, just by changing the psw bits we can switch to various banks. But what should we need to do if we want to access bit address locations and scratch pad locations?? , here we use DIRECT ADDRESSING MODE.

Example:
Mov A,40h           
; note there is no # symbol,implies its an address. 
Data in that address will be moved(copied) in register A

Mov 45h, R5
; data in R5 will be moved to address 45h

Mov 21h, 45h
; data in 45h address will be moved to address 21h

Mov A,0 ; It is same as
Mov A,R0 ; because the address of R0 is 0

Mov A,R1; It is same as
Mov A,1h ; i.e R1 address is 1h

4. REGISTER INDIRECT ADDRESSING MODE:
In this mode register is used to point data. Lets see an example to understand better
Mov A, @R0

Note '@' symbol. Here the 'data of address in the register R0 will be moved to A' .
For example address 50h consists of the data 25h and we need to move it in accumulator register then

Mov R0,#50h        ; Here 50h is just a normal value
Mov A,@R0         ; but @ indicates that data in the 50h address

Example: Move 25h data into the location 50h using register indirect mode.
Mov R0,#50h  
Mov A,#25h
Mov @R0,A  
 ;@R0 indicates we are moving(copying) data contained in A into the address containing in R0

You would have got doubt that cant we move using direct addressing mode. Yes, we can move using direct addressing mode as shown below. its the similar that of above

Mov 50h,#25h ; moving 25h value into the address 50h

Then what is the advantage of Register Indirect Addressing mode to the Direct Addressing mode? Now lets have a look ...
Important Note: Only Register R0 and R1 are used for this purpose i.e Mov A,@R2 is invalid also Mov @R2,A is invalid, only we need to use R0 or R1 for this mode.

Advantage of Register Indirect Addressing Mode:
Access of data dynamically will be possible using this mode rather than static.Let us look at an example to understand it better

Example1: Write a Program to copy the value 25h into RAM memory locations 45h to 4A h using
a) Direct addressing mode
b)Register Indirect addressing mode without loop
c) with a loop

a)
 Mov A,#25h
 Mov 45h,A
 Mov 46h,A
 Mov 47h,A
 Mov 48h,A
 Mov 49h,A
 Mov 4Ah,A

b)
 Mov A,#25h                ; 25h which should be copied to Ram locations
 Mov R0,#45h              ; location address 45h
 Mov @R0,A               ; copying 25h i.e in A into address of 45h
 INC R0                       ; Incriment R0 i.e now R0=46h
 Mov @R0,A               ; copying 25h i.e in A into address of 46h
 INC R0                       ; Now R0=47h
 Mov @R0,A               ; copying 25h i.e in A into address of 47h
 INC R0                       ; Now R0=48h
 Mov @R0,A               ; copying 25h i.e in A into address of 48h
 INC R0                       ; Now R0=49h
 Mov @R0,A               ; copying 25h i.e in A into address of 49h
 INC R0                       ; Now R0=4Ah, note in hexadecimal after 49 we get 4A but not 50h
 Mov @R0,A               ; copying 25h i.e in A into address of 4Ah

c)
 Mov A,#25h                 ;A=25h
 Mov R0,#45h               ; R0 =45h
 Mov R4,#05                 ; R4=5 i.e for loop upto 5 i.e 45h to 4A h
 Back: Mov @R0, A       ; Mov 25h into the address contain in R0
           Inc R0                ; Incriment the R0 i.e 45h to 46h, 46h to 47h and so on..
           DJNZ R4, Back   ; loop until R4=0 i.e it will be zero after 5 iterations



Now look at all the 3 ways of programming, now it is clear that the last (c) is simple and short. So its the main advantage of Register Indirect Addressing Mode.

Example 2: Clear the 12 RAM locations starting from address 50h
Clearing is nothing but storing 0
Mov A,#00h
Mov R1,#50h
Mov R2,#15
Again: Mov @R1,A
           INC R1
           DJNZ R2, Again


5. Indexed Addressing Mode:
This mode is used in accessing data which are stored in the form of Look-up table which are stored in ROM location of 8051. Instruction used for this purpose is  MOVC A,@A+DPTR. As we know DPTR is of 16 bits and A is of 8 bits so it is necessary to use in the way said before.

Example 1: Assuming that ROM space starting at 250h contains "YES" , write a program to transfer the bytes into RAM location starting at 40h.

                             ORG 00h
                             Mov DPTR,#DATA            ;load Rom pointer
                             Mov R0,#40h        
                             Mov R2,#03                       ;load counter
                Back:    CLR A
                             MovC A,@A+DPTR         ; move data from code space
                             Mov @R0,A                      ;save it in RAM
                             INC DPTR                         ; points to next location
                             INC R0                        
                             DJNZ R2, Back              
                Here:     SJMP Here                        ;infinite loop

;------------------------------------on chip space-------------------------------
                ORG 250h                                     ;address from 250h
     Data    DB      "YES"                                 ;DATA
                END                                              ;End



AUTHOR: YOUSTRON SIC





0 comments:

Post a Comment