0% found this document useful (0 votes)
13 views7 pages

Chap#5. Addressing Modes

The document discusses various addressing modes used in computer instruction sets, detailing modes such as Register, Immediate, Direct, Indirect, External Direct, External Indirect, Relative, Absolute, Long, and Indexed addressing. Each mode is explained with examples, highlighting their specific applications and advantages in programming, particularly in assembly language. Understanding these modes is crucial for efficient software development and addressing limitations in command usage.
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)
13 views7 pages

Chap#5. Addressing Modes

The document discusses various addressing modes used in computer instruction sets, detailing modes such as Register, Immediate, Direct, Indirect, External Direct, External Indirect, Relative, Absolute, Long, and Indexed addressing. Each mode is explained with examples, highlighting their specific applications and advantages in programming, particularly in assembly language. Understanding these modes is crucial for efficient software development and addressing limitations in command usage.
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/ 7

Addressing Modes

48
Addressing Modes
(04 hours)

5.1 Different Addressing Modes


When instructions operate on Data, the question arises: “Where are the Data?” The
answer of this question lies in the “Addressing Modes”. There are several possible addressing
modes and there are several possible answers to the question, Addressing modes are an integral
part of each computer’s instruction set. They allow specifying the source or destination of data in
different ways, depending on the programming situation. In this section we will examine different
addressing modes and give examples of each.
1. Register
2. Immediate
3. Direct
4. Indirect
5. External Direct
6. External Indirect
7. Relative
8. Absolute
9. Long
10. Indexed
An "addressing mode" refers to how you are addressing a given memory location.

5.1.1 Register Addressing


Register addressing contains registers as operator or operand. This mode involves
the use of registers to hold the data to be manipulated. Examples of register addressing
mode follow.
MOV A, R1 ; Load (Contents of) R1 into the Accumulator
MOV R2, A ; Load (Contents of) Accumulator into the R2
ADD A, R5 ; ADD (Contents of) Accumulator with (Contents of) R5
MOV R2, DPL ; Load (Contents of) Data Pointer Low into the R2
MOV R3, DPH ; Load (Contents of) Data Pointer High into the R3
It is also important that the source and destination registers must be equal in size. For
example we cannot write the following instruction due to difference in size.
MOV A, DPTR : Wrong instruction as DPTR is of 16 bit while A is of 8 bit

49
MOV R2, R5 : Wrong due to rule. You must not need this instruction
Note: If the instruction just contains A (Accumulator) register, we will not consider the
instruction in Register addressing mode. Also it is important that in 8051 we have 8 registers except A & B
registers. While in PIC Micro-Controller Accumulator is known as Work Register and whole RAM and
Ports are known as File Register

5.1.2 Immediate Addressing


In this addressing mode, the source operand is a constant. In immediate
addressing mode, as the name implies, when the instruction is assembled, the operand
comes immediately after the op-code. Notice that the immediate data must be preceded
by the pound sign, “#”. This addressing mode can be used to load information into any of
the registers, including the DPTR register. Immediate addressing is so-named because the
value to be stored in memory immediately follows the operation code in memory. That is to say,
the instruction itself dictates what value will be stored in memory. For example, the instruction:
MOV A, #20h ; Load (Hexadecimal) 20 into the Accumulator
This instruction uses Immediate Addressing because the Accumulator will be loaded with
the value that immediately follows; in this case 20 (hexadecimal). Immediate addressing is very
fast since the value to be loaded is included in the instruction. However, since the value to be
loaded is fixed at compile-time it is not very flexible.

5.1.3 Direct Addressing


Direct addressing is so-named because the value to be stored in memory is obtained by
directly retrieving it from another memory location. For example:
MOV A, 30h
This instruction will read the data out of Internal RAM address 30 (hexadecimal) and store it in
the Accumulator.
Direct addressing is generally fast since, although the value to be loaded isn’t included in
the instruction, it is quickly accessible since it is stored in the 8051s Internal RAM. It is also
much more flexible than Immediate Addressing since the value to be loaded is whatever is found
at the given address--which may be variable.
Also, it is important to note (in 8051) that when using direct addressing any instruction
which refers to an address between 00h and 7Fh is referring to Internal Memory. Any instruction
which refers to an address between 80h and FFh is referring to the SFR (Special Function
Registers) control registers that control the 8051 microcontroller itself.

50
TIP: (The obvious question that may arise is, "If direct addressing an address from 80h through
FFh refers to SFRs, how can I access the upper 128 bytes of Internal RAM that are available on the 8052?"
The answer is: You can’t access them using direct addressing. As stated, if you directly refer to an address
of 80h through FFh you will be referring to an SFR. However, you may access the 8052s upper 128 bytes
of RAM by using the next addressing mode, "indirect addressing.”)

5.1.4 Indirect Addressing


Indirect addressing is a very powerful addressing mode which in many cases provides an
exceptional level of flexibility. Indirect addressing is also the only way to access the extra 128
bytes of Internal RAM found on an 8052. Indirect addressing appears as follows:
MOV A,@R0
This instruction causes the 8051 to analyze the value of the R0 register. The 8051 will then load
the Accumulator with the value from Internal RAM which is found at the address indicated by R0.
For example, let’s say R0 holds the value 40h and Internal RAM address 40h holds the
value 67h. When the above instruction is executed the 8051 will check the value of R0. Since R0
holds 40h the 8051 will get the value out of Internal RAM address 40h (which holds 67h) and
store it in the Accumulator. Thus, the Accumulator ends up holding 67h.
Indirect addressing always refers to Internal RAM; it never refers to any SFR. Thus, SFR
99h can be used to write a value to the serial port. Thus one may think that the following would
be a valid solution to write the value 1 to the serial port:
MOV R0,#99h ;Load the address of the serial port
MOV @R0,#01h ;Send 01 to the serial port -- WRONG!!
This is not valid. Since indirect addressing always refers to Internal RAM these two
instructions would write the value 01h to Internal RAM address 99h on an 8052. On an 8051
these two instructions would produce an undefined result since the 8051 only has 128 bytes of
Internal RAM.

5.1.5 External Direct


External Memory is accessed using a suite of instructions which use what we call
"External Direct" addressing. We call it this because it appears to be direct addressing, but it is
used to access external memory rather than internal memory. There are only two commands that
use External Direct addressing mode:
MOVX A, @DPTR
MOVX @DPTR, A

51
As you can see, both commands utilize DPTR. In these instructions, DPTR must first be
loaded with the address of external memory that you wish to read or write. Once DPTR holds the
correct external memory address, the first command will move the contents of that external
memory address into the Accumulator. The second command will do the opposite: it will allow
you to write the value of the Accumulator to the external memory address pointed to by DPTR.

5.1.6 External Indirect


External memory can also be accessed using a form of indirect addressing which I call
External Indirect addressing. This form of addressing is usually only used in relatively small
projects that have a very small amount of external RAM. An example of this addressing mode is:
MOVX @R0, A
Once again, the value of R0 is first read and the value of the Accumulator is written to
that address in External RAM. Since the value of @R0 can only be 00h through FFh the project
would effectively be limited to 256 bytes of External RAM. There are relatively simple
hardware/software tricks that can be implemented to access more than 256 bytes of memory using
External Indirect addressing; however, it is usually easier to use External Direct addressing if
your project has more than 256 bytes of External RAM.

5.1.7 Relative Addressing


Relative addressing is used only with certain jump instructions. A relative address (or off-
set) is an 8 bit signed (positive or negative) value, which is added to the program counter to form
the address of the next instruction executed. As we have described the address is signed so the
jump will be at ahead if address is +ve and jump is back if address is –ve. Important to remember
that 8 bit signed number can be -128 to 127. And 0 is neither +ve or –ve.
Also the relative offset is appended to the instruction as an additional byte. Prior to the addition
the program counter is incremented to the address following the jump instruction.
Above detail is not concerned to the programmer, since jump destinations are usually
specified as labels and the assembler determines the relative offset accordingly. Following are the
jump instructions that specify 8 bit signed offset addresses.
SJMP label ; Short JuMP to the given label
CJNE R2, #100, label ; Compare and Jump to the given label if R2 Not Equal to 100
DJNZ R7, label ; Decrement R7 and Jump to the given label if value of R7 is Not Zero
JNB P1.1, label ; Jump to label if Bit P1.1 is Not set
JNC label ; Short Jump to the given label if Carry bit Not set

52
5.1.8 Absolute Addressing
This type of addressing is specific to 8051 Micro-Controller. Absolute addressing is used
only with the ACALL and AJMP instructions. These two byte instructions allow branching
within the current 2K page of code memory by providing the 11 least-significant bits of the
destination address in the op-code. 3 bits in first byte (A10-A8) and 8 bits in 2nd Byte of the instruction
(A7-A0), Upper 5 bits in first Byte determines the 2K page. Thus the Upper 5 bits of program counter
does not change in response of this instruction, while lower 11 bits are changed with given 11 bits
from instruction as shown here.
11100001 => 1st Byte (A10-A8 + op-code)
nd
01000110 => 2 Byte (A7-A0)
So the lower 11 bits will be 11101000110. The upper 5 bits of program counter will not
change (remain same as previous) while lower 11 bits will be replaced by given 11 bits. Thus the
new value of program counter will be XXXXX11101000110 here XXXXX represents previous
address that is not updated in execution of instruction.
Remember that the main difference between Relative Addressing and Absolute Addressing is that
Relative Addressing has offset address that is added in current address of program counter, while in
Absolute Addressing the Program counter is updated with the 11bits given in the 2 Byte instructions.

5.1.9 Long Addressing


Long Addressing is used only with the LCALL and LJMP instructions. These 3-byte
instructions include a full 16 bit destination address as bytes 2 and 3 of the instruction. Advantage
is that the full 64K code space may be addressed and we are free to make call or jump in between
whole space of Memory, but the disadvantage is that the instructions are three bytes long and are
position dependent. Position dependent means that some times we use this addressing where we can use
Absolute Addressing or Relative Addressing modes. For example Program Counter is at 2000H and we
give instruction to call or jump to location 2040, this can be easily achieved by just Relative Addressing.
LCALL Location : Call the Location to address anywhere within 64K Code space.
LJMP Location : Jump the Location to address anywhere within 64K Code space.

5.1.10 Indexed Addressing


Indexed addressing uses a base register (either the program counter or the data pointer)
and an offset (the accumulator) in forming the effective address for a JMP or MOVC instruction.
Jump tables or look-up tables are easily using indexed addressing. Examples are as follows:
MOVC A, @A + (base-reg) : Move contents of Location adding A with present address to A
JMP @A + DPTR : Jump to the added location of Accumulator to Data pointer.

53
5.2 Application of Addressing mode in programming
To understand the advantages and applications of addressing modes in Micro-Controller
or Micro-Processor we need the clear view regarding different Addressing Modes. Also different
systems support different types of addressing modes. So if we prepare software for one type of
Micro-Controller, it may not work on other Micro-Controller.
The specific of each type of addressing modes are important for computer programmers
using assembly language, as assembly language is direct representation of the machine
instructions sent to the CPU. This is what makes assembly language much faster than all other
languages.
Addressing modes enables the programmer to make the software more efficient i.e. a
software developer may not use Long Addressing where he can use Absolute or Relative
addressing. Also by the understanding of addressing modes we get the limitation of commands.
For example CJNE, DJNZ, JNB, JNC, JB and JC commands have limitation of jump within ±127
offset of code space. So the programmer without knowledge about this limitation will not be able
to resolve the errors due to this limitation.
Conclusion of our discussion is that Addressing modes are much important in
programming, however it is affected in Assembly Language programming only. In higher level
languages the compiler is made enough efficient to manipulate the Addressing modes and
programmer has no concern with Addressing issues.

54

You might also like