Wednesday, October 01, 2008

Addressing Modes of 8052

As is the case with all microcomputers from the PDP-8 onwards, the 8052 utilizes several memory addressing modes. An "addressing mode" refers to how you are accessing (.addressing.) a given memory location or data value. In summary, the addressing modes are listed below with an example of each:

Immediate Addressing MOV A,#20h
Direct Addressing MOV A,30h
Indirect Addressing MOV A,@R0
External Direct MOVX A,@DPTR
External Indirect MOVX A,@R0
Code Indirect MOVC A,@A+DPTR
Each of these addressing modes provides important flexibility to the programmer.

5.1 Immediate Addressing

Immediate addressing is so-named because the value to be stored in memory immediately follows the opcode in memory. That is to say, the instruction itself dictates what value will be stored in memory.
For example:

This instruction uses Immediate Addressing because the Accumulator (A) will be loaded with the value that immediately follows; in this case 20h (hexadecimal).
Immediate Addressing is very fast since the value to be loaded is included in the instruction. However, since the value to be loaded is fixed at compile-time it is not very flexible. It is used to load the same, known value every time the instruction executes.

5.2 Direct Addressing
Direct addressing is so-named because the value to be stored in memory is obtained by directly retrieving it from another memory location.
For example:
¼½ ¾.¿ ÀÂÁ,Ã Ä
This instruction will read the data out of Internal RAM address 30h (hexadecimal) and store it in the Accumulator (A).
Direct addressing is generally fast since, although the value to be loaded isn.t included in the instruction, it is quickly accessible since it is stored in the 8052.s Internal RAM. It is also much more flexible than Immediate Addressing since the value to be loaded is whatever is found at the given address--which may change.
Also, it is important to note that when using direct addressing any instruction that refers to an address between 00h and 7Fh is referring to Internal RAM. Any instruction that refers to an address between 80h and FFh is referring to the SFR control registers that control the 8052 itself.
The obvious question that may arise is, "If direct addressing an address from 80h through FFh refers to SFRs, how can I access the upper 128 bytes of Internal RAM that are available with the 8052?" The answer is: You can.t access them using direct addressing. As stated, if you directly refer to an address of 80h through FFh you will be referring to an SFR.
However, you may access the 8052.s upper 128 bytes of RAM by using the next addressing mode, "indirect addressing."

5.3 Indirect Addressing

Indirect addressing is a very powerful addressing mode that in many cases provides an exceptional level of flexibility. Indirect addressing is also the only way to access the upper 128 bytes of Internal RAM found on an 8052.
Indirect addressing appears as follows:
ÅÆ Ç.È É ÊËÌ
This instruction causes the 8052 to analyze the value of the R0 register. The 8052 will then load the Accumulator (A) with the value from Internal RAM that is found at the address indicated by R0.
For example, let.s suppose R0 holds the value 40h and Internal RAM address 40h holds the value 67h.
When the above instruction is executed the 8052 will check the value of R0. Since R0 holds 40h the 8052
will get the value out of Internal RAM address 40h (which holds 67h) and store it in the Accumulator.
Thus, the Accumulator ends up holding 67h.
Indirect addressing always refers to Internal RAM; it never refers to an SFR. In a prior example we mentioned that SFR 99h can be used to write a value to the serial port. Thus one may think that the following would be a valid solution to write the value .1. to the serial port:
ÍÎ Ï ÐÑGÒ Ó Ô Ô Õ Ö ×Ø Ù Ú.Û Õ Ü.Ù ÚÚ Ý Ü ÞÞ.Ø ß.Û Õ Ü2Þ Ü'Ý àÙá$â Ø Ý Û
ÍÎ Ï ãÐÑyÒ Ó Ñä Õ Öå Ü æÚ Ñä.Û Ø2Û'Õ Ü Þ'Ü Ý à Ùá.âØ Ý Û.ççéèêë ìí¬îî
This is not valid. Since indirect addressing always refers to Internal RAM these two instructions would write the value 01h to Internal RAM address 99h on an 8052.
On an 8051 these two instructions would produce an undefined result since the 8051 only has 128 bytes of Internal RAM.

5.4 External Direct
External memory is accessed using a suite of instructions that use what I call "External Direct" addressing. I call it this because the address to be accessed is contained directly in the DPTR register, but it accesses external memory.
There are only two commands that use External Direct addressing mode:
ïð ñò ó¬ô õ ö÷ øù
ïð ñò2õ ö÷ øù9ô ó
As you can see, both commands utilize DPTR. In these instructions, DPTR must first be loaded with the address of external memory that you wish to read or write. Once DPTR holds the correct external memory address, the first command will move the contents of that external memory address into the Accumulator.
For example, to read the contents of external RAM address 1516h, we.d execute the instructions:
úû ü2ýþ ÿ 􀀀 - - ! !" #" -
úû ü $%&(' ýþ ÿ 􀀀 ú # ) # !*# + - ,􀀀 %ú.- #/- 0 1 0 - #
The second command will do the opposite: it will allow you to write the value of the Accumulator to the external memory address pointed to by DPTR. For example, to write the contents of the Accumulator to external RAM address 1516 we.d execute the instructions:
2 3 465 7 8 9 :; < =< > ? @ A B C B D E E ? B B F E B G H I CI J J G B K K"E L"G B I J
2 3 4 M6N 5 7 8 9 :PO @P2 L Q BE ? B D L H E B H E K*L RB F E B G H I C,9 O 2.S H E L/I D D T U T C I E L G

5.5 External Indirect
External memory can also be accessed using a form of indirect addressing that I call External Indirect.

This form of addressing is usually only used in relatively small projects that have a very small amount of external RAM. An example of this addressing mode is:
V W X Y6Z [ \^]_
Once again, the value of R0 is first read and the value of the Accumulator is written to that address in External RAM. Since the value of @R0 can only be 00h through FFh the project would effectively be
limited to 256 bytes of External RAM. There are relatively simple hardware/software tricks that can be implemented to access more than 256 bytes of memory using External Indirect addressing; however, it is usually easier to use External Direct addressing if your project has more than 256 bytes of External RAM.

5.6 Code Indirect
Two additional 8052 instructions allow the developer to access the program code itself. This is useful for accessing data tables, strings, etc. The two instructions are:


Programming Tip: Technically, accessing external memory with the MOVX @DPTR instructions is indirect addressing since the address to be accessed is referred to indirectly by the DPTR register. However, to directly access a specific external RAM memory location the most direct way is to load DPTR with the address in question and
access it with the MOVX instruction. Thus while this approach is technically "indirect," I called it "External Direct" since it is the most direct method of accessing a specific external RAM memory location and to differentiate it from the following addressing mode (External Indirect) which is very similar to the "indirect addressing" mentioned in section 5.3 in its use of R0 and R1. For example, if we wanted to access the data stored in code memory at address 2021h, we.d execute the instructions:

The MOVC A,@A+DPTR moves the value contained in the code memory address that is pointed to by
adding DPTR to the Accumulator.

Please give your suggestion and follow us if you are interested, which encourage us to create new topics for you. And Thankyou for your support.