You are on page 1of 14

Chapter 5: 8051 Addressing Modes  depends mostly on source operand speciation:

- CPU can access data using following addressing modes:


o Immediate  MOV A, #data  example: MOV A, #25  25H  A
o Register  MOV A, Rn (R0-R7 only)  example: MOV A, R3  byte data content of R3  A
o Memory  get (read/write) date from memory (RAM) locations with physical address
 Direct  MOV A, direct  MOV A,B or MOV A, 30H  after exe. data contents of P.A.=30HA
 Register Indirect  MOV A, @Ri (i=0 or 1)  ex: MOV A, @R0 data pointed by PA is stored in R0  A
 Indexed indirect mem. address of source operand is “in @DPTR” or “split in two parts (@A+DPTR)”
- Source operand is a constant: (1) The immediate data must be preceded by the pound sign, “#”. (2) Can load
information into any registers, including 16-bit DPTR register (two 8-bit registers, DPH and DPL)
Example 1: MOV A, #25H ;load 25H into A
MOV R4, #62 ;load 62 into R4
MOV B, #40H ;load 40H into B
MOV DPTR, #4521H ;DPTR=4512H  illegal if Value > 65535 or FFFFH
MOV DPL, #21H ;This is the same
MOV DPH, #45H ;as above ;
- The EQU directive (no machine code) to access immediate data from memory storage
Example 2 Count EQU 30
... ...
MOV R4, #COUNT ;R4=1EH =30D
MOV DPTR, #MYDATA ;DPTR=200H
... ...
ORG 200H
MYDATA: DB “America” ; America is initially stored at physical address starting from 200H
- Using immediate addressing mode, we can also access or send data to 8051 ports
Example 3  MOV P1, #55H or MOV 090, #55H  as Physical address of Port-1 is 90H (next page)
- Using Register Addressing mode, we can hold the date to be manipulated
Example 3 MOV A, R0 ;copy contents of R0 into A
MOV R2, A ;copy contents of A into R2
ADD A, R5 ;add contents of R5 to A
ADD A, R7 ;add contents of R7 to A
MOV R6, A ;save accumulator in R6
- Register Add. mode, Source destination registers must match in size  MOV DPTR, A  Error/invalid
Movement of data between Rn registers is not allowed MOV R1, R7  Invalid
Example 4: MOV DPTR, #25F5H
MOV R7, DPL
MOV R6, DPH
- Direct addressing mode is often used to access 128 (7FH) bytes of RAM locations with P.A.= 30H –7FH.
Example 4: Using Direct add. Mode  MOV A, 04H OR using Register add. Mode  MOV A, R4
- The SFR (Special Function Register) can be accessed by their names OR by their addresses (80H to FFH)
Example 5: MOV 0E0H, #55H  is the same as  MOV A, #55h  see table in next page
Example 6: MOV 0F0H, R0  is the same as  MOV B, R0

1
The physical address linked
with different SFR’s are listed
in table-5. So
MOV A, #25H
is the same as
MOV 0E0H , #25H
(try this with pinnacle)

• D.PTR (data pointer), and


30-7f RAM
PC (program count) are
locations 
16-bit registers of 8051 MC. 20-2F  bit scratch pad
P.C. points to the next addressable  stack …..
instruction to be executed..
Thus, most of 8051 can
access 64K byte (0-FFFFH)

90
- See example 7 for accessing
port using Direct add-mode

- Only Direct addressing


mode is allowed for
PUSHing or POPing

- PUSH A  Invalid

- PUSH 0E0H  Valid

2
- See example 8 for PUSH and pop
- In Register indirect addressing mode: register is used as a pointer to the data. Only register R0 and R1 are used
for this purpose and R2 –R7 cannot be used to hold the address of an operand located in RAM,
Example 9: MOV A, @R0 ; move contents of RAM, whose address is held by R0 into A
MOV @R1, B ;move contents of B into RAM, whose address is held by R1
Example 10: Write a program to copy the value of RAM PA=34h into ex RaM memory locations PA= 40H
using
(a) Direct addressing mode, (b) Register indirect addressing mode without a loop, and
(c) Using Register Indirect Memory addressing mode with a loop:
Solutions (a) MOV A, #55H ;load A with value 55H
MOV 40H, A ;copy A to RAM location 40H
MOV 41H, A ;copy A to RAM location 41H
(b) MOV A, #55H ;load A with value 55H
MOV R0, #40H ;load the pointer. R0=40H
MOV @R0, A ;copy A to RAM R0 points to
INC R0 ;increment pointer. Now R0=41h
MOV @R0, A ;copy A to RAM R0 points to
(c) MOV A, #55H ;A=55H
MOV R0, #40H ;load pointer.R0=40H,
MOV R2, #02 ;load counter, R2=3
AGAIN: MOV @R0, A ;copy 55 to RAM R0 points to p.a=40h
INC R0 ;increment R0 pointer
DJNZ R2, AGAIN ;loop until counter = zero

- The advantage of using Register direct Memory addressing mode is it allows data access in a dynamic rather
than static method, as in Direct addressing mode  Looping is not possible in Direct addressing mode
Example 11: Write a program to copy a block of 10 (Dec) bytes of data from RAM addresses: 35H to 60H
Solution: MOV R0,#35H ;source pointer
MOV R1,#60H ;destination pointer
MOV R3,#10 ;counter
BACK: MOV A,@R0 ;get a byte from source
MOV @R1,A ;copy it to destination
INC R0 ;increment source pointer
INC R1 ;increment destination pointer
DJNZ R3,BACK ;keep doing for ten bytes

- Home work 1: Write an efficient program that will clear 17D RAM locations starting from P.A.=25H
- R0 and R1 are the only registers that can be used for pointers in register indirect addressing mode
- Since R0 and R1 are 8 bits wide, their use is limited to access any information in the internal RAM
- Whether accessing externally connected RAM or on-chip ROM, we need 16-bit pointer. In such case, the DPTR
register is used

3
- Indexed addressing mode is widely used in accessing data elements of look-up table entries located in the
program ROM (internal and/or external)
If data
ROM
with
only
Table
of
data
DB…
then
use
MOVX

- MOV DPTR, #1234H…..MOVX A, @ DPTR  ext. RAM data byte-contents of P.A.=1234H is moved to ‘A’
 this is different from indirect memory addressing mode of RAM as contents of @DPTR is loaded in Register A
- Example
- MOV A,#0H ; MOV DPTR, #34H ; MOVC A,@A+DPTR ‘A’ is loaded with content of ROM P.A.=1234H
- MOV DPTR, #34H; MOVX@ DPTR, A  ‘A’ is loaded with content of ext. RAM with P.A.=1234H

Internal ROM MOV A,#0H


MOV DPTR, #02H
MOVC A,@A+DPTR A=90H
Note A is loaded from int. ROM
MOVX A,@DPTR A=56H
Note A is loaded from ext. RAM

(remember that from ROM we can only “read data”)


Note that for external ROM and RAM, P0 and P2 are used to send (by 8051) physical-address and send/receive data byte.
- MOVC A, @A+DPTR is also used to copy code or data byte-contents of external ROM with P.A  (A+DPTR).
- Remember, “DB directive” stored data in internal ROM in addition to all the machine codes of the program.

Example1: Write a program to copy the value of RAM PA=34h into ext- RAM PA= 40H
Solution: MOV DPTR, #0040h…. MOV A,34H….MOVX@DPTR,A
(remember that from ROM we can only “read data”)
Example2: In 8053, write a program to copy the value of RAM PA=42h into ext- ROM PA= 50H
Solution: MOV DPTR, #0000h…. MOV A,34H….MOVC @A+DPTR,A (will not work, as cannot write)
Note this is applicable for internal ROM-less microcontroller 8053

4
Example 12: In this program, assume that the word “USA”is
Physical address ASCII Hex code
burned into internal ROM locations starting at 200H. And
200H U 55H
that the program is burned into ROM locations starting at 0.
Analyze how the program works and state where “USA”is 201H S 53H
202H A 41H
stored after this program is run.

Solution: ORG 0000H ;burn into ROM starting at 0


MOV DPTR, #200H ;DPTR=200H look-up table address
CLR A ;clear A(A=0)
MOVC A,@ A+DPTR ; get char from code space  DPRT = 200H  after exe. A=55H = ’U’ASCI
MOV R0,A ; save it in R0  after execution R0=55H
INC DPTR ; DPTR=201H point to next char
CLR A ; clear A(A=0)
MOVC A, @A+DPTR ; get the next char  DPTR = 201H  after exe. A = 53H = ’S’ASCI
MOV R1, A ; save it in R1  after execution R1=53H
INC DPTR ; DPTR=202 point to next char
CLR A ; clear A(A=0)
MOVC A, @A+DPTR ; get the next char  DPTR = 202H  after exe. A = 41H = ’A’ASCI
MOV R2, A ; save it in R2
Here: SJMP HERE ; stay here ; Data is burned into ROM code space starting at 200H
ORG 200H
MYDATA:DB “USA”
END ;end of program

Example 13: Write an efficient program to get (or input)


Physical address Hex
byte data (ranging from 0H to 4H) using P1. Then use the 300H 00H
pre-stored table beside to send (or output) x2 using P2 301H 01H
continuously. Use Index addressing mode and the given 302H 04H
table below to convert x to x2. Remember you have to 303H 09H
304H 10H
store this table using “DB” using your program

Solution: ORG 300H ; Point to memory location with physical address (PA) 300H
DB 00h, 01h, 04h, 09h, 10h ; To initialize the values of x2 and store them from PA=300H
MOV P1, #0FFH ; Set P1 as input port
L3: MOV A,P1 ; Take input ‘x’ from ‘P1’ and store the byte in register ‘A’. The allowed range of inputted
data is from 00H to 04H (means, x=00H, x=01 H, x=02 H, x=03 H, x=04 H).
MOV DPTR, #300H ; DPTR=300H look-up table address  note that address > FFH
MOVC A, @A+DPTR ; get the NUMBER STORED IN ADDRESS (A+DPTR) = 300H ;
MOV P2, A ; Output the value of ‘A’ through P2  which is equal to squire (x2) of the
5
SJMP L3 ; inputted data from P1 (x)

- Review: In many applications, the size of program code does not leave any room to share the
64K-byte code space with data
� The 8051 has another 64K bytes of memory space set aside exclusively for data storage
� This data memory space is referred to as external memory and it is accessed only by the
MOVX instruction
� The 8051 has a total of 128K bytes of memory space
� 64K bytes of code and 64K bytes of data
� The data space cannot be shared between code and data

6
Physical address for
6th BIT location of
byte with P.A of 24H
(like 24.6, but not- allowed)

7
8
9
REVIEW:

10
11
Remember 6th and 7th BITs are
part of BYTE-data P.A of 20H

12
13
14

You might also like