8051 Addressing modes

jojo December 22, 2011 38 Comments

Lets begin this article with a simple question. “What is an addressing mode ?. A simple question always has a simple answer too. Addressing mode is a way to address an operand.  Operand means the data we are operating upon (in most cases source data). It can be a direct address of memory, it can be register names, it can be any numerical data etc. I will explain this with a simple data move instruction of 8051.

MOV A,#6AH

Here the data 6A is the operand, often known as source data. When this instruction is executed, the data 6AH is moved to accumulator A. There are 5 different ways to execute this instruction and hence we say, we have got 5 addressing modes for 8051. They are 1) Immediate addressing mode 2) Direct addressing mode 3) Register direct addressing mode 4) Register indirect addressing mode 5) Indexed addressing mode.

Immediate Addressing Mode

Let’s begin with an example.

MOV A, #6AH

In general we can write MOV A, #data

This addressing mode is named as “immediate” because it transfers an 8-bit data immediately to the accumulator (destination operand).

The picture above describes the above instruction and its execution.  The opcode for MOV A, # data is 74H. The opcode is saved in program memory at 0202 address. The data 6AH is saved in program memory 0203. (See, any part of the program memory can be used, this is just an example) When the opcode 74H is read, the next step taken would be to transfer whatever data at the next program memory address (here at 0203) to accumulator A (E0H is the address of accumulator). This instruction is of two bytes and is executed in one cycle. So after the execution of this instruction, program counter will add 2 and move to o204 of program memory.

Note: The ‘#’ symbol before 6AH indicates that operand is a data (8 bit). If ‘#’ is not present then the hexadecimal number would be taken as address.

Direct Addressing Mode

This is another way of addressing an operand. Here the address of the data (source data ) is given as operand. Lets take an example.

MOV A, 04H

Here 04H is the address of register 4 of register bank#0. When this instruction is executed, what ever data is stored in register 04H is moved to accumulator. In the picture below we can see, register 04H holds the data 1FH. So the data 1FH is moved to accumulator.

Note: We have not used ‘#’ in direct addressing mode, unlike immediate mode. If we had used ‘#’, the data value 04H would have been transferred to accumulator instead 0f 1FH.

As shown in picture above this is a 2 byte instruction which requires 1 cycle to complete. Program counter will increment by 2 and stand in 0204. The opcode for instruction MOV A, address is E5H. When the instruction at 0202 is executed (E5H), accumulator is made active and ready to receive data. Then program control goes to next address that is 0203 and look up the address of the location (04H) where the source data (to be transferred to accumulator) is located. At 04H the control finds the data 1F and transfers it to accumulator and hence the execution is completed.

Register Direct Addressing Mode

In this addressing mode we use the register name directly (as source operand). An example is shown below.

MOV A, R4

At a time registers can take value from R0,R1…to R7. You may already know there are 32 such registers. So how you access 32 registers with just 8 variables to address registers? Here comes the use of register banks. There are 4 register banks named 0,1,2 and 3. Each bank has 8 registers named from R0 to R7. At a time only one register bank can be selected. Selection of register bank is made possible through a Special Function Register (SFR) named Processor Status Word (PSW). PSW is an 8 bit SFR where each bit can be programmed. Bits are designated from PSW.0 to PSW.7 Register banks are selected using PSW.3 and PSW.4 These two bits are known as register bank select bits as they are used to select register banks. A picture below shows the PSW register and the Register Bank Select bits with status.

So  in register direct addressing mode, data is transferred to accumulator from the register (based on which register bank is selected).

Take a look at the picture below.

So we see that opcode for MOV A, R4 is EC. The opcode is stored in program memory address 0202 and when it is executed the control goes directly to R4 of the respected register bank (that is selected in PSW). If register bank #0 is selected then the data from R4 of register bank #0 will be moved to accumulator. (Here it is 2F stored at 04 H). 04 H is the address of R4 of register bank #0. Movement of data (2F) in this case is shown as bold line. Now please take a look at the dotted line. Here 2F is getting transferred to accumulator from data memory location 0C H. Now understand that 0C H is the address location of Register 4 (R4) of register bank #1. Programmers usually get confused with register bank selection. Also keep in mind that data at R4 of  register bank #0 and register bank #1 (or even other banks) will not be same. So wrong selection of register banks will result in undesired output.

Also note that the instruction above is 1 byte and requires 1 cycle for complete execution. This means using register direct addressing mode can save program memory.

Register Indirect Addressing Mode

So in this addressing mode, address of the data (source data to transfer) is given in the register operand.

MOV A, @R0

Here the value inside R0 is considered as an address, which holds the data to be transferred to accumulator.

Example: If R0 holds 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. Got it? See  the picture below.

So the opcode for MOV A, @R0 is E6H. Assuming that register bank #0 is selected. So the R0 of register bank #0 holds the data 20H. Program control moves to 20H where it locates the data 2FH and it transfers 2FH to accumulator.

This is a single byte instruction and the program counter increments 1 and moves to 0203 of program memory.

Note: Only R0 and R1 are allowed to form a register indirect addressing instruction. In other words programmer can must make any instruction either using @R0 or @R1. All register banks are allowed.

Indexed Addressing Mode

Well lets see two examples first.

MOVC A, @A+DPTR and MOVC A, @A+PC

where DPTR is data pointer and PC is program counter (both are 16 bit registers). Lets take the first example.

MOVC A, @A+DPTR

What’s the first impression you have now? The source operand is @A+DPTR and we know we will get the source data (to transfer) from this location. It is nothing but adding contents of DPTR with present content of accumulator. This addition will result a new data which is taken as the address of source data (to transfer). The data at this address is then transferred to accumulator.  Take a look at the picture below.

 

The opcode for the instruction is 93H. DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits) and FE is located in DPL (lower 8 bits). Accumulator now has the value 02H. A 16 bit addition is performed and now 01FE H+02 H results in 0200 H. What ever data is in 0200 H will get transferred to accumulator. The previous value inside accumulator (02H) will get replaced with new data from 0200H. New data in the accumulator is shown in dotted line box.

This is a 1 byte instruction with 2 cycles needed for execution. What you infer from that? The execution time required for this instruction is high compared to previous instructions (which all were 1 cycle).

The other example MOVC A, @A+PC works the same way as above example. The only difference is, instead of adding DPTR with accumulator, here data inside program counter (PC) is added with accumulator to obtain the target address.

I hope you now have a clear cut understanding of 8051 addressing modes. If you have any doubts or need any additional clarifications, please post your comments below. I will answer them.

Comments
  • mohammad
    March 10, 2016

    superb article! Very clear explanation of 8051 addressing modes!

  • vishnu dinesh nair
    September 11, 2015

    The content given about addressing modes is very useful to me. Thanks alot

  • MANOHAR
    February 28, 2015

    tell me about the long and short indexed

  • kathir
    November 9, 2014

    And what about immediate,relative,absolute and long addressing?

  • raj jariwala
    October 2, 2014

    can you help me to solve indexed addressing mode..
    if we have take data at address 0300H then FINAL AnS is store in 0300h?

  • enis krasniqi
    September 2, 2014

    hi can you help me with this question

    ORG 100H
    MOV A,#01H
    RLC A
    INC A
    MOVC A,@A+PC
    LJMP 2330H
    DB 00H, 10H, 20H, 30H, 40H, 50H, 60H, 70H
    a)ater MOVC, A=00
    b)the contents of A do not change
    c) after excute MOVC, the A=10h
    d)after MOVC, the next instruction will be LJMP 2330H
    e)after MOVC,A=23H
    wich of this answer are true, please help me urgently
    Sincerely Enis Krasniqi

  • dinesh devireddy
    July 17, 2014

    i succesfully completed my seminar on addressing modes the content given about addressing modes ais very useful to me..!!!! Thanks alot once again

  • dinesh
    July 17, 2014

    i try to understand indexed addressing mode butican’t could you explain in breif

  • thara
    June 18, 2014

    thanz for ur explanation.

  • June 9, 2014

    sir i did,t understand indexed addressing mode.so can you please explain breifly

  • kavindra pal
    May 11, 2014

    tell m exact execution of indirect addresing mode,.,.

  • kavindra pal
    May 11, 2014

    tell m execution by indirect addressing mode,.,.,

  • kavita
    May 7, 2014

    please sir tell me acject addressing mode of 8051

  • shefali
    December 17, 2013

    in indexed addressing mode in the instruction
    movc a,@a+pc
    what does c mean im movc?

    • GIBIN
      January 4, 2014

      As u can check, MOVC instruction is used to moved a byte from the Code memory to Accumulator. Therefore C in MOVC stands for Code.

  • harith
    December 15, 2013

    nice work thanx

  • Raghavendra
    September 26, 2013

    how register bank is selected register direct addressing mode

  • nani
    August 18, 2013

    tht was excelent.. u shld have explained the figures sme more perfectly… so tht a fresher can easily understand

  • Ela
    August 9, 2013

    can a programmer make up a new addressing mode in 8051?

  • Akshay
    June 11, 2013

    nice explaination

  • gaurav
    April 10, 2013

    can you give alp for arrsy addition with explanation of each step

  • sandeep
    March 4, 2013

    why are we supposed to add accumulator with DPTR/PC? Is there any specific reason? (in the instruction MOVC A, @A+DPTR and MOVC A, @A+PC)…. what role does accumulator play in this instruction?

    • mohammad Shafiquddin
      July 19, 2013

      yes it will save time and this addressing mode widely used to access lookup table

  • February 12, 2013

    gud work

  • December 5, 2012

    nice work sir ,,,,
    thnk u very much…

  • dona
    December 4, 2012

    i made a mistake ,not immediate *

  • dona
    December 4, 2012

    And what about immediate,relative,absolute and long addressing?
    Thank you,

  • Anurag
    October 19, 2012

    yep its good

  • September 23, 2012

    register direct addressing mode and register indirect addressing modes are loooking to be similar. what will be the difference??

    • jojo
      September 23, 2012

      @Balalogeshwari – They are entirely different. Register indirect addressing mode can be compared to be sth similar to the concept of “pointers” in C and C++ You may read the article carefully once again.
      Consider MOV A, @R0

      Here the value inside R0 is considered as an address, which holds the data to be transferred to accumulator.
      If register R0 holds the address 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. Got it?

  • harsha
    August 11, 2012

    give info about diff. among cmos,nmos,pmos&bicmos

  • mugheer sheraz
    April 27, 2012

    a good wrk

  • pinky
    April 5, 2012

    i couln’t understand index addresing mode . ie. its execution towards the memory back .?….. and here is no relative addresing mode

  • justin c das
    February 8, 2012

    sir what is the use of 4 reg banks as we can only use one at a time.

  • justin c das
    February 8, 2012

    sir, accumulator is 1 byte memory how to move 0200 in it, as you said above in movc a,@a+dptr

  • jojo
    January 4, 2012

    Hello Akshara,

    Take a look at http://www.circuitstoday.com/category/555-timer-ic

    There you can find many articles and circuits related to 555 timer.

  • akshara
    December 23, 2011

    sir,please publish the article of cmos and 555 timer

Leave a Reply to justin c das Cancel reply

Your email address will not be published. Required fields are marked *