Great People Talk About Ideas.. Average People Talk About Things, And Small People Talk About Other People.

Saturday, 26 January 2013

ADDRESSING MODES

Addressing Modes:


The CPU can access data in various ways, which are called addressing modes

  • Immediate
  • Register
  • Direct
  • Register indirect
  • Indexed

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
Examples:
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

We can use EQU directive to access immediate data

Count EQU 30
... ...
MOV R4,#COUNT            ;R4=1EH
MOV DPTR,#MYDATA    ;DPTR=200H
ORG 200H
MYDATA: DB “Student”

We can also use immediate addressing mode to send data to ports
MOV P1,#55H


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


 Direct Addressing Mode


It is most often used the direct addressing mode to access RAM locations 30 – 7FH
  • The entire 128 bytes of RAM can be accessed
  • 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
  • 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

SFR 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

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
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=R6

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

Example:

Write a program to copy the value 55H into RAM memory locations 40H to 41H using
(a) direct addressing mode, (b) register indirect addressing mode without a loop, and (c) with a loop
Solution:
(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
INC R0                                ;increment R0 pointer
DJNZ R2,AGAIN                ;loop until counter = zero



  • The advantage is that it makes accessing data dynamic rather than static as in direct addressing mode
    •  Looping is not possible in direct addressing mode
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

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

  • R0 and R1 are the only registers that can be used for pointers in register indirect addressing mode


 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


Example Program:
In this program, assume that the word “ABC” 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 “ABC” is stored after this program is run.
Solution:
ORG 0000H                         ;burn into ROM starting at 0
MOV DPTR,#200H            ;DPTR=200H look-up table addr
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 point 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 point 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
MYDATA:DB “ABC”
END                                                       ;end of program

Look-up Table

The look-up table allows access to elements of a frequently used table with minimum operations
Example:
Write a program to get the x value from P1 and send x2 to P2, continuously

Solution:
ORG 0
MOV DPTR,#300H           ;LOAD TABLE ADDRESS
MOV A,#0FFH                  ;A=FF
MOV P1,A                         ;CONFIGURE P1 INPUT PORT
BACK:
MOV A,P1                         ;GET X
MOV A,@A+DPTR           ;GET X SQAURE 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

BIT ADDRESSES

  • Many microprocessors allow program to access registers and I/O ports in byte size only
    • However, in many applications we need to check a single bit
  • One unique and powerful feature of the 8051 is single-bit operation
    • Single-bit instructions allow the programmer to set, clear, move, and complement individual bits of a port, memory, or register
    • It is registers, RAM, and I/O ports that need to be bit-addressable
      • ROM, holding program code for execution, is not bit-addressable

Example:
A switch is connected to pin P1.7. Write a program to check the status of the switch and make the following decision.
(a) If SW = 0, send “0” to P2
(b) If SW = 1, send “1“ to P2
Solution:
SW EQU P1.7
MYDATA EQU P2
HERE:    MOV C,SW
JC OVER
MOV MYDATA,#’0’
SJMP HERE
OVER: MOV MYDATA,#’1’
SJMP HERE
END

Example:
Assume that the on-chip ROM has a message. Write a program to copy it from code space into the upper memory space starting at address 80H. Also, as you place a byte in upper RAM, give a copy to P0.
Solution:
ORG 0
MOV DPTR,#MYDATA
MOV R1,#80H                    ;access the upper memory
B1:      CLR A
MOVC A,@A+DPTR         ;copy from code ROM
MOV @R1,A                       ;store in upper memory
MOV P0,A                           ;give a copy to P0
JZ EXIT                                  ;exit if last byte
INC DPTR                             ;increment DPTR
INC R1                                   ;increment R1
SJMP B1                               ;repeat until last byte
EXIT: SJMP $                       ;stay here when finished
;---------------
ORG 300H
MYDATA: DB “The Promise of World Peace”,0
END


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...