QP 6
QP 6
➢ Direct
➢ Register indirect
➢ Indexed
Dr. K. Ghosh 60
Immediate Addressing Mode
64
Example
Solution :
(a) MOV A,#55H ;A=55H
MOV P1,A ;P1=55H
MOV P2,A ;P2=55H
(b) From SRF table, P1 address=80H; P2 address=A0H
MOV A,#55H ;A=55H
MOV 80H,A ;P1=55H
MOV 0A0H,A ;P2=55H
Dr. K. Ghosh 65
Stack and Direct Addressing Mode
Dr. K. Ghosh 66
Register Indirect Addressing Mode
Dr. K. Ghosh 67
Example
Write a program to copy the value 55H into RAM memory locations 40H to 42H
using
(a) direct addressing mode, (b) register indirect addressing mode without a loop,
and (c) with a loop
Solution MOV A,#55H ;load A with value 55H
MOV 40H,A ;copy A to RAM location 40H Looping is not
(a) MOV 41H,A ;copy A to RAM location 41H
possible in Direct
Addressing Modes
(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
INC R0 ;increment pointer. Now R0=42h
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=2
AGAIN: MOV @R0,A ;copy 55 to RAM R0 points to
INC R0 ;increment R0 pointer
DJNZ R2,AGAIN ;loop until counter = zero
68
Dr. K. Ghosh
Example
Example
Write a program to clear 16 RAM locations starting at RAM address
60H
Solution:
CLR A ;A=0
MOV R1,#60H ;load pointer. R1=60H
MOV R7,#16 ;load counter, R7=16
AGAIN: MOV @R1,A ;clear RAM R1 points to
INC R1 ;increment R1 pointer
DJNZ R7,AGAIN ;loop until counter=zero
Dr. K. Ghosh 69
Example
Dr. K. Ghosh 70
Register Indirect Addressing Mode contd…
Dr. K. Ghosh 71
Indexed Addressing Mode and On-chip
ROM Access
Dr. K. Ghosh 72
Example
In this program, assume that the word “VIT” is burned into ROM locations starting at
200H, and that the program is burned into ROM locations starting at 0. Analyze how the
program works and state where “VIT” is stored after this program is run.
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 the char from code space
MOV R0,A ;save it in R0
INC DPTR ;DPTR=201 pointing to next char
CLR A ;clear A(A=0)
MOVC A,@A+DPTR ;get the next char
MOV R1,A ;save it in R1
INC DPTR ;DPTR=202 pointing to next char
CLR A ;clear A(A=0)
MOVC A,@A+DPTR ;get the next char
MOV R2,A ;save it in R2
HERE:SJMP HERE ;stay here
In the above program ROM locations 200H - 202H have the following
contents.
200=(‘V’) 201=(‘I') 202=(‘T')
We start with DPTR = 200H, and A = 0. The instruction “MOVC A,
@A+DPTR” moves the contents of ROM location 200H (200H + 0 = 200H) to
register A. Register A contains 56H, the ASCII value for “V”. This is moved to
R0. Next, DPTR is incremented to make DPTR = 201H. A is set to 0 again to
get the contents of the next ROM location 201H, which holds character “I”.
After this program is run, we have R0 = 56H, R1 = 49H, and R2 = 54H, the
ASCII values for the characters “V”, “I” and “T”.
Dr. K. Ghosh 74
Example
Assuming that ROM space starting at 250H contains “INDIA”, write a program to
transfer the bytes into RAM locations starting at 40H.
Assuming that ROM space starting at 250H contains “INDIA”, write a program to
transfer the bytes into RAM locations starting at 40H.
Solution: ;(b) This method uses null char for end of string
ORG 0000
MOV DPTR,#250H ;load ROM pointer
MOV R0,#40H ;load RAM pointer
BACK: CLR A ;A=0
MOVC A,@A+DPTR ;move data from code space
JZ HERE ;exit if null character
MOV @R0,A ;save it in RAM
INC DPTR ;increment ROM pointer
INC R0 ;increment RAM pointer
SJMP BACK ;loop
HERE: SJMP HERE
;----------On-chip code space used for storing data
ORG 250H
DB “INDIA",0 ;notice null char for
;end of string
END
Dr. K. Ghosh 76
Example
Write a program to get the x value from P1 and send x2 to P2, continuously.
Solution:
ORG 0
MOV DPTR,#300H ;load look-up table address
MOV A,#0FFH ;A=FF
MOV P1,A ;configure P1 as input port
BACK: MOV A,P1 ;get X
MOVC A,@A+DPTR ;get X squared from table
MOV P2,A ;issue it to P2
SJMP BACK ;keep doing it
ORG 300H
XSQR_TABLE:
DB 0,1,4,9,16,25,36,49,64,81
END
Dr. K. Ghosh 77