0% found this document useful (0 votes)
9 views18 pages

QP 6

The document discusses various addressing modes used by the CPU to access data, including immediate, register, direct, register indirect, and indexed addressing modes. It provides examples of how to use these modes for data manipulation in assembly language, particularly focusing on the 8051 microcontroller architecture. Additionally, it covers the use of special function registers (SFR) and includes practical programming examples for data transfer and manipulation in RAM and ROM.

Uploaded by

Riyaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views18 pages

QP 6

The document discusses various addressing modes used by the CPU to access data, including immediate, register, direct, register indirect, and indexed addressing modes. It provides examples of how to use these modes for data manipulation in assembly language, particularly focusing on the 8051 microcontroller architecture. Additionally, it covers the use of special function registers (SFR) and includes practical programming examples for data transfer and manipulation in RAM and ROM.

Uploaded by

Riyaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Addressing Modes

❑ The CPU can access data in various ways,


which are called addressing modes
➢ Immediate
➢ Register

➢ Direct
➢ Register indirect
➢ Indexed

Dr. K. Ghosh 60
Immediate Addressing Mode

❑ The source operand is a constant


➢ The immediate data must be preceded by the pound sign, “#”
➢ Can load information into any registers, including 16-bit DPTR
register
▪ DPTR can also be accessed as two 8-bit registers, the high
byte DPH and low byte DPL

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
MOV DPL,#21H ;This is the same
MOV DPH,#45H ;as above
;illegal!! Value > 65535 (FFFFH)
MOV DPTR,#68975
Dr. K. Ghosh 61
Register Addressing Mode

❑ Use registers to hold the data to be manipulated


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

❑ The source and destination registers must match in size


➢ MOV DPTR,A will give an error
MOV DPTR,#25F5H
MOV R7,DPL
MOV R6,DPH

❑ The movement of data between Rn registers is not allowed


➢ MOV R4,R7 is invalid
Dr. K. Ghosh 62
Direct Addressing Mode

❑ It is most often used the direct addressing mode to


access RAM locations 30 – 7FH

Direct addressing mode

➢ The register bank locations are accessed


by the register names
MOV A,4 ;is same as
MOV A,R4 ;which means copy R4 into A

❑ Contrast this with immediate addressing mode


Register addressing mode

➢ There is no “#” sign in the operand


MOV R0,40H ;save content of 40H in R0
MOV 56H,A ;save content of A in 56H
63
SRF Registers and their Addresses

❑ The SFR (Special Function Register) can be accessed by


their names or by their addresses

MOV 0E0H,#55H ;is the same as


MOV A,#55h ;load 55H into A
MOV 0F0H,R0 ;is the same as
MOV B,R0 ;copy R0 into B

❑ The SFR registers have addresses between 80H and


FFH
➢ Not all the address space of 80 to FF is used by SFR
➢ The unused locations 80H to FFH are reserved and must not
be used by the 8051 programmer

64
Example

Write code to send 55H to ports P1 and P2, using


(a) their names (b) their addresses

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

❑ Only direct addressing mode is allowed for pushing or popping


the stack
➢ PUSH A is invalid
➢ Pushing the accumulator onto the stack must be coded as PUSH
0E0H
Example
Show the code to push R5 and A onto the stack and then pop them back them into R2
and B, where B = A and R2 = R5
Solution:
PUSH 05 ;push R5 onto stack
PUSH 0E0H ;push register A onto stack
POP 0F0H ;pop top of stack into B
;now register B = register A
POP 02 ;pop top of stack into R2
;now R2=R5

Dr. K. Ghosh 66
Register Indirect Addressing Mode

❑ A register is used as a pointer to the data


➢ Only register R0 and R1 are used for this purpose
➢ R2 – R7 cannot be used to hold the address of an operand
located in RAM
❑ When R0 and R1 hold the addresses of RAM locations, they
must be preceded by the “@” sign

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

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

Write a program to copy a block of 10 bytes of data from


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

Dr. K. Ghosh 70
Register Indirect Addressing Mode contd…

❑ 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

Dr. K. Ghosh 71
Indexed Addressing Mode and On-chip
ROM Access

❑ Indexed addressing mode is widely used in


accessing data elements of look-up table entries
located in the program ROM

❑ The instruction used for this purpose is


MOVC A,@A+DPTR
➢ Use instruction MOVC, “C” means code
➢ The contents of A are added to the 16-bit register
DPTR to form the 16-bit address of the needed data

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

;Data is burned into code space starting at 200H


ORG 200H
DB “VIT" 73
END ;end of program
Example

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.

Solution: ;(a) This method uses a counter


ORG 0000
MOV DPTR,#250H ;load ROM pointer
MOV R0,#40H ;load RAM pointer
MOV R2,#5 ;load counter
BACK: CLR A ;A = 0
MOVC A,@A+DPTR ;move data from code space
MOV @R0,A ;save it in RAM
INC DPTR ;increment ROM pointer
INC R0 ;increment RAM pointer
DJNZ R2,BACK ;loop until counter=0
HERE: SJMP HERE

;----------On-chip code space used for storing data


ORG 250H
DB “INDIA"
END
Dr. K. Ghosh 75
Example

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

You might also like