keywords: 8051, microcontroller
8051 Addressing Modes:
When instructions
operate on data, the question arises: "Where are the data?" The
answer to this question lies in the 8051's "addressing modes." An "addressing mode" refers to how
you are addressing a given memory location. The8051 instructions use eight
addressing modes. These are:
- Register
- Immediate
- Direct
- Indirect
Each of these addressing modes provides important
flexibility.
Register Addressing:
The
register banks, which contains registers R0 through R7, can be accessed by
instructions whose opcodes carry a 3-bit register specification. Instructions
that access the registers this way make efficient use of code, since this mode
eliminates an address byte. When the instruction is executed, one of four banks
is selected at execution time by the row bank select bits in PSW.
MOV A, Rn ; copy contents of Rn into A
MOV A,
#n ; copy the immediate data byte n to
the register A.
MOV Rn,
A ; copy contents of A
into Rn.
MOV Rn,
#n ; copy the immediate data byte n to
the register Rn.
MOV DPTR,
#nn ; copy the immediate 2 byte data nn to
the register DPTR.
*Note: The source
and destination registers must match in size.
Immediate Addressing:
When
a source operand is a constant rather than a variable, then the constant can be
incorporated into the instruction as a byte of "immediate" data. An
additional instruction byte contains the value.
In
assembly language, immediate operands are preceded by a number sign (#). The
operand may be a numeric constant, a symbolic variable, or an arithmetic
expression using constants, symbols, and operators. The assembler computes the
value and substitutes the immediate data into the instruction. For example, the
instruction
MOV A, #25H; load 25H into A
MOV R4, #62; load 62 into R4
MOV DPTR, #4521H; DPTR=4512H
Direct Addressing:
It
differs from register addressing in that any byte within the first 256
addresses can be accessed by specifying 8-bit address. When using this mode,
there is few things watch out for. The first RAM addresses (080h to 0FFh, if
the device you are using has RAM at these locations) cannot be accessed by
direct memory addressing.
MOV R0, 40H; save content of 40H in R0
MOV 56H, A; save content of A in 56H
Indirect Addressing:
As the name indicates it is an indirect
mode of addressing. It uses R0 or R1 as an 8-bit index register to access a
byte in the first 256 addresses of the data space. The register indirect
addressing mode is identified by the symbol '@' before either R0 or R1. Using
any other bank (R2 to R7) will result in an error.
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
External Data Addressing:
Pointer used for external data addressing can
be either R0/R1 (256 byte access) or DPTR (64kbyte access).
For example -
For example -
MOVX A, @R1; Moves
content of 8-bit address pointed by R0 to A
MOVX A, @DPTR; Moves
content of 16-bit address pointed by DPTR to A
External Code Addressing:
Sometimes
we may want to store non-volatile data into the ROM e.g. look-up tables. Such
data may require reading the code memory. This may be done as follows -
MOVC A, @A+DPTR; Moves
content of address pointed by A+DPTR to A
MOVC A, @A+PC; Moves
content of address pointed by A+PC to A
keywords: 8051, microcontroller
No comments:
Post a Comment