Sunteți pe pagina 1din 26

8051 MICROCONTROLLER

ADDRESSING MODES
ADDRESSING MODES

1. Immediate

2. Register

3. Direct

4. Register Indirect

5. Indexed
Immediate Addressing Modes

• Source operand is constant

• Operand comes immediately after opcode

• Immediate data must be preceded by “#” (pound


sign)

• Used to load information into any of the


registers, including DPTR register
Immediate Addressing Modes-
Examples (using registers)

INSTRUCTION COMMENT

MOV A, #25H ;load 25H into A

MOV R4, #62 ;load the decimal value 62 into R4

MOV B, #40H ;load 40H into B


Immediate Addressing Modes-
Examples (using DPTR)
• DPTR (16 bit register), can be accessed as 2 8-
bit registers, DPH & DPL
MOV DPTR, #2550H is same as MOV DPL, #50H
MOV DPH, #25H

Note: The following would produce an error


MOV DPTR, #68975 ; illegal !! Value >65535 FFFFH
Immediate Addressing Modes-
Examples (using ports & timers)
MOV P1, #55H ; 55H is sent to port1
MOV P1, #0FFH ; make P1= i/p

MOV TL0, #0F2H ;TL0 = F2H, the low byte


MOV TH0, #0FFH ;TH0 = FFH, the high byte
Register Addressing Modes
• Involves the use of registers to hold the data to be
manipulated
Note:
• The source & destination registers must match in
size (eg. MOV DPTR, A will give an error)
• We can move data between A and Rn (n= 0 to 7)
• Movement of data between Rn registers is not
allowed ( eg. MOV R4, R7 is invalid)
Register Addressing Modes-
Examples
INSTRUCTION COMMENT

MOV A, R0 ; copy the contents of R0 into A

MOV R2, A ; copy the contents of A into R2

ADD A, R5 ; add the contents of R5 to contents of A

ADD A, R7 ; add the contents of R7 to contents of A

MOV R6, A ; save accumulator in R6


Direct Addressing Mode

• Data is in a RAM memory location whose


address is known

• This address is given as part of the instruction

• The # sign distinguishes between immediate


and direct addressing modes
Direct Addressing Mode-
Examples (Accessing memory)
INSTRUCTION COMMENT

MOV R0, 40H ; save content of RAM location 40H in R0

MOV 56H, A ; save content of A in RAM location 56H

MOV R4, 7FH ; move contents of RAM location 7FH to


R4
Direct Addressing Mode-
Examples (Accessing registers)
INSTRUCTION COMMENT
MOV A,4 ; is same as
MOV A, R4 ; which means copy R4 into A

INSTRUCTION COMMENT
MOV A,7 ; is same as
MOV A, R7 ; which means copy R7 into A
Direct Addressing Mode-
Examples (Accessing registers)
INSTRUCTION COMMENT
MOV A,2 ; is same as
MOV A, R2 ; which means copy R2 into A

INSTRUCTION COMMENT
MOV A,0 ; is same as
MOV A, R0 ; which means copy R0 into A
Importance of # sign
INSTRUCTION COMMENT
MOV R2, #5 ; R2=05

MOV A, 2 ; copy R2 to A (A=R2=05)

MOV B, 2 ; copy R2 to B (B=R2=05)

MOV 7, 2 ; copy R2 to R7 (R7=R2=05)


; since “MOV R7, R2” is invalid
Example Pgm
Eg. Write code to send 55H to ports P1 and P2
using their names
INSTRUCTION COMMENT
MOV A, #55H ; A=55H

MOV P1,A ; P1=55H

MOV P2,A ; P2=55H


Direct Addressing Mode cont…
Note:
•RAM – 128 bytes
•Entire 128 bytes can be accessed using direct
addressing mode, but it is most often used to
access locations 30-7F
•Because, reg. bank locations are accessed by
reg. names of R0-R7, but no such name for
other RAM locations
•Only direct addressing mode is allowed for
pushing into stack (eg. PUSH A is invalid)
Direct Addressing Mode-
Examples (Accessing stack)
Eg. Show the code to push R5, R6 and A onto stack and then pop
them back into R2, R3 and B, where B=A, R2=R6 and R3=R5

INSTRUCTION COMMENT
PUSH 05 ; push R5 onto stack
PUSH 06 ; push R6 onto stack
PUSH 0E0H ; push register A onto stack
POP 0F0H ; pop top of stack into reg. B
; now reg. B = reg. A
POP 02 ;pop top of stack into R2
; now R2 = R6
POP 03 ;pop top of stack into R3
; now R3= R5
Register Indirect Addressing Mode
• In this, a register is used as a pointer to the data

• If the data is inside CPU, only registers R0 & R1


are used

• ie; R2-R7 cannot be used to hold the address of


an operand in RAM when using this mode

• When R0 &R1 are used as pointers, they must be


preceded by @ sign
Register Indirect Addressing Mode-
Examples

INSTRUCTION COMMENT
MOV A, @R0 ; move contents of RAM location whose
; address is held by R0 into A
MOV @R1, B ; move contents of B into RAM location
;whose address is held by R1
Eg: WAP to copy the value 55H into RAM memory
locations 40H, 41H, 42H,43H & 44H using:

a) Direct addressing mode


b) Register indirect addressing mode without a
loop
c) With a loop
(a) Direct addressing mode

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

MOV 42H, A ; copy A to RAM location 42H

MOV 43H, A ; copy A to RAM location 43H

MOV 44H, A ; copy A to RAM location 44H


(b) Register indirect addressing mode without a
loop
MOV A, #55H ; load A with value 55H
MOV R0, #40H ; load the pointer R0=40H
MOV @R0, A ; copy A to RAM location which R0 points to
INC R0 ; increment pointer. Now R0= 41H
MOV @R0, A ; copy A to RAM location which R0 points to
INC R0 ; increment pointer. Now R0= 42H
MOV @R0, A ; copy A to RAM location which R0 points to
INC R0 ; increment pointer. Now R0= 43H
MOV @R0, A ; copy A to RAM location which R0 points to
INC R0 ; increment pointer. Now R0= 44H
MOV @R0, A ; copy A to RAM location which R0 points to
a) Register indirect addressing mode with loop

MOV A, #55H ; load A with value 55H

MOV R0, #40H ; load the pointer R0=40H

MOV R2, #05 ; load counter, R2=5

AGAIN: MOV @R0, A ; copy A to RAM location which R0 points to

INC R0 ; increment pointer

DJNZ R2, AGAIN ; decrement R2 and jump to AGAIN if R2is not zero
Register Indirect Addressing Mode
cont…
• Advantage:
makes accessing data dynamic rather than static as in
the case of direct addressing mode

• Disadvantage:
o only R0 & R1 can be used as pointers
o Since R0 & R1 are 8 bits wide, their use is limited to
accessing any information in the internal RAM
(scratch pad memory of 30H-7FH, or SFR)
o For accessing externally connected RAM or on-chip
ROM, we need 16 bit pointer
o In such case DPTR register is used
Indexed Addressing Mode
• Used to access data elements located in program
(code)space ROM or external memory
• Widely used in accessing elements of look-up table
• The instruction used is MOVC A, @A+DPTR
• Uses MOVC instead of MOV
• “C” means code
• The contents of A are added to DPTR to form 16 bit
address of needed data
• Another reg. used in this mode is PC
Eg. for Indexed Addressing Mode
MOV DPTR, #200H ;DPTR=200H look-up table address

CLR A ;clear A (A=0)

MOVC A, @A+DPTR ;get the data element from code space

MOV R0, A ;save it in R0

INC DPTR ;DPTR=201 pointing to next element

CLR A ; clear A (A=0)

MOVC A, @A+DPTR ;get the next element from code space

MOV R1, A ;save it in R1


Eg. WAP to get the x value from P1 and send the
square of x to P2 continuously. Use look-up table
ORG 0
MOV DPTR, #300 ;Load look-up table address
MOV A, #0FFH ;A=FF
MOV P1, A ;configure P1 as i/p port
BACK: MOV A, P1 ;get x
MOVC A, @A+DPTR ;get x square from table
MOV P2, A ;issue it to P2
SJMP BACK ;keep doing it

ORG 300
XSQR_TABLE:
DB 0,1,4,9,16,25,36,49,64,81
END

S-ar putea să vă placă și